diff options
| author | Joas Schilling <nickvergessen@gmx.de> | 2014-05-31 01:35:10 +0200 | 
|---|---|---|
| committer | Joas Schilling <nickvergessen@gmx.de> | 2014-05-31 01:35:10 +0200 | 
| commit | 9c2687ddc9ba7071f7c98c118203f96a412e3d1b (patch) | |
| tree | eb5914ab23e704d17fde617841fedb7616ab1bdc | |
| parent | f4dcb69dae9964c7d6ca9d06828d773a63a3abed (diff) | |
| parent | 91305a43af126bc404b9fa42572eb8b23009a209 (diff) | |
| download | forums-9c2687ddc9ba7071f7c98c118203f96a412e3d1b.tar forums-9c2687ddc9ba7071f7c98c118203f96a412e3d1b.tar.gz forums-9c2687ddc9ba7071f7c98c118203f96a412e3d1b.tar.bz2 forums-9c2687ddc9ba7071f7c98c118203f96a412e3d1b.tar.xz forums-9c2687ddc9ba7071f7c98c118203f96a412e3d1b.zip  | |
Merge pull request #2494 from Nicofuma/ticket/12589
[ticket/12589] Search directly in $directory if it's an absolute sub-path
* Nicofuma/ticket/12589:
  [ticket/12589] Add test searching in a non absolute directory
  [ticket/12589] Fix tests
  [ticket/12589] Search directly in $directory if it's an absolute sub-path
| -rw-r--r-- | phpBB/phpbb/extension/finder.php | 81 | ||||
| -rw-r--r-- | tests/extension/finder_test.php | 16 | 
2 files changed, 64 insertions, 33 deletions
diff --git a/phpBB/phpbb/extension/finder.php b/phpBB/phpbb/extension/finder.php index 71a5542b67..6f2408094e 100644 --- a/phpBB/phpbb/extension/finder.php +++ b/phpBB/phpbb/extension/finder.php @@ -465,6 +465,10 @@ class finder  			}  			else if ($directory && $directory[0] === '/')  			{ +				if (!$is_dir) +				{ +					$path .= substr($directory, 1); +				}  				$directory_pattern = '^' . preg_quote(str_replace('/', DIRECTORY_SEPARATOR, $directory) . DIRECTORY_SEPARATOR, '#');  			}  			else @@ -477,45 +481,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(); - -				if ($file_info->isDir() == $is_dir) +				$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) +					$filename = $file_info->getFilename(); + +					if ($file_info->isDir() == $is_dir)  					{ -						$relative_path = $iterator->getInnerIterator()->getSubPath() . DIRECTORY_SEPARATOR . basename($filename) . DIRECTORY_SEPARATOR; -						if ($relative_path[0] !== DIRECTORY_SEPARATOR) +						if ($is_dir)  						{ -							$relative_path = 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 = $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; +							}  						} -					} -					else -					{ -						$relative_path = DIRECTORY_SEPARATOR . $iterator->getInnerIterator()->getSubPathname(); -					} -					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, +							); +						}  					}  				}  			} diff --git a/tests/extension/finder_test.php b/tests/extension/finder_test.php index 4ef9411557..8cc5146b33 100644 --- a/tests/extension/finder_test.php +++ b/tests/extension/finder_test.php @@ -132,6 +132,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  | 
