From f7896f46a5d93ce9119db71e75d77cc1af5bc139 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 24 May 2014 17:57:01 +0200 Subject: [PATCH 1/3] [ticket/12589] Search directly in $directory if it's an absolute sub-path PHPBB3-12589 --- phpBB/phpbb/extension/finder.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/extension/finder.php b/phpBB/phpbb/extension/finder.php index 6cc6e1808a..09c4f6d3e0 100644 --- a/phpBB/phpbb/extension/finder.php +++ b/phpBB/phpbb/extension/finder.php @@ -463,6 +463,7 @@ class finder } else if ($directory && $directory[0] === '/') { + $path .= substr($directory, 1); $directory_pattern = '^' . preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR, '#'); } else @@ -494,7 +495,11 @@ class finder if ($is_dir) { $relative_path = $iterator->getInnerIterator()->getSubPath() . DIRECTORY_SEPARATOR . basename($filename) . DIRECTORY_SEPARATOR; - if ($relative_path[0] !== DIRECTORY_SEPARATOR) + if ($directory && $directory[0] === '/') + { + $relative_path = str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR . $relative_path; + } + else if ($relative_path[0] !== DIRECTORY_SEPARATOR) { $relative_path = DIRECTORY_SEPARATOR . $relative_path; } @@ -502,6 +507,14 @@ class finder else { $relative_path = DIRECTORY_SEPARATOR . $iterator->getInnerIterator()->getSubPathname(); + if ($directory && $directory[0] === '/') + { + $relative_path = str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR . $relative_path; + } + else + { + $relative_path = DIRECTORY_SEPARATOR . $relative_path; + } } if ((!$suffix || substr($relative_path, -strlen($suffix)) === $suffix) && From 930c5d85edc773bd21db85f9f811f55d727885e4 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 24 May 2014 18:58:52 +0200 Subject: [PATCH 2/3] [ticket/12589] Fix tests PHPBB3-12589 --- phpBB/phpbb/extension/finder.php | 86 ++++++++++++++++---------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/phpBB/phpbb/extension/finder.php b/phpBB/phpbb/extension/finder.php index 09c4f6d3e0..ff7bfad4ca 100644 --- a/phpBB/phpbb/extension/finder.php +++ b/phpBB/phpbb/extension/finder.php @@ -463,7 +463,10 @@ class finder } else if ($directory && $directory[0] === '/') { - $path .= substr($directory, 1); + if (!$is_dir) + { + $path .= substr($directory, 1); + } $directory_pattern = '^' . preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR, '#'); } else @@ -476,57 +479,56 @@ class finder } $directory_pattern = '#' . $directory_pattern . '#'; - $iterator = new \RecursiveIteratorIterator( - new \phpbb\recursive_dot_prefix_filter_iterator( - new \RecursiveDirectoryIterator( - $path, - \FilesystemIterator::SKIP_DOTS - ) - ), - \RecursiveIteratorIterator::SELF_FIRST - ); - - foreach ($iterator as $file_info) + if (is_dir($path)) { - $filename = $file_info->getFilename(); + $iterator = new \RecursiveIteratorIterator( + new \phpbb\recursive_dot_prefix_filter_iterator( + new \RecursiveDirectoryIterator( + $path, + \FilesystemIterator::SKIP_DOTS + ) + ), + \RecursiveIteratorIterator::SELF_FIRST + ); - if ($file_info->isDir() == $is_dir) + foreach ($iterator as $file_info) { - if ($is_dir) + $filename = $file_info->getFilename(); + + if ($file_info->isDir() == $is_dir) { - $relative_path = $iterator->getInnerIterator()->getSubPath() . DIRECTORY_SEPARATOR . basename($filename) . DIRECTORY_SEPARATOR; - if ($directory && $directory[0] === '/') + if ($is_dir) { - $relative_path = str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR . $relative_path; - } - else if ($relative_path[0] !== DIRECTORY_SEPARATOR) - { - $relative_path = DIRECTORY_SEPARATOR . $relative_path; - } - } - else - { - $relative_path = DIRECTORY_SEPARATOR . $iterator->getInnerIterator()->getSubPathname(); - if ($directory && $directory[0] === '/') - { - $relative_path = str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR . $relative_path; + $relative_path = $iterator->getInnerIterator()->getSubPath() . DIRECTORY_SEPARATOR . basename($filename) . DIRECTORY_SEPARATOR; + if ($relative_path[0] !== DIRECTORY_SEPARATOR) + { + $relative_path = DIRECTORY_SEPARATOR . $relative_path; + } } else { - $relative_path = DIRECTORY_SEPARATOR . $relative_path; + $relative_path = $iterator->getInnerIterator()->getSubPathname(); + if ($directory && $directory[0] === '/') + { + $relative_path = str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR . $relative_path; + } + else + { + $relative_path = DIRECTORY_SEPARATOR . $relative_path; + } } - } - if ((!$suffix || substr($relative_path, -strlen($suffix)) === $suffix) && - (!$prefix || substr($filename, 0, strlen($prefix)) === $prefix) && - (!$directory || preg_match($directory_pattern, $relative_path))) - { - $files[] = array( - 'named_path' => str_replace(DIRECTORY_SEPARATOR, '/', $location . $name . substr($relative_path, 1)), - 'ext_name' => $ext_name, - 'path' => str_replace(array(DIRECTORY_SEPARATOR, $this->phpbb_root_path), array('/', ''), $file_info->getPath()) . '/', - 'filename' => $filename, - ); + if ((!$suffix || substr($relative_path, -strlen($suffix)) === $suffix) && + (!$prefix || substr($filename, 0, strlen($prefix)) === $prefix) && + (!$directory || preg_match($directory_pattern, $relative_path))) + { + $files[] = array( + 'named_path' => str_replace(DIRECTORY_SEPARATOR, '/', $location . $name . substr($relative_path, 1)), + 'ext_name' => $ext_name, + 'path' => str_replace(array(DIRECTORY_SEPARATOR, $this->phpbb_root_path), array('/', ''), $file_info->getPath()) . '/', + 'filename' => $filename, + ); + } } } } From 91305a43af126bc404b9fa42572eb8b23009a209 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 26 May 2014 18:30:04 +0200 Subject: [PATCH 3/3] [ticket/12589] Add test searching in a non absolute directory PHPBB3-12589 --- tests/extension/finder_test.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/extension/finder_test.php b/tests/extension/finder_test.php index d0ca5956b4..0c01feb1a6 100644 --- a/tests/extension/finder_test.php +++ b/tests/extension/finder_test.php @@ -128,6 +128,22 @@ class phpbb_extension_finder_test extends phpbb_test_case ); } + public function test_non_absolute_directory_get_classes() + { + $classes = $this->finder + ->directory('type/') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + '\vendor2\foo\sub\type\alternative', + '\vendor2\foo\type\alternative', + ), + $classes + ); + } + public function test_sub_directory_get_classes() { $classes = $this->finder