[feature/extension-manager] Add support for directories to the extension finder

PHPBB3-10323
This commit is contained in:
Nils Adermann 2011-08-29 22:21:20 -04:00
parent fe4b8818ec
commit 0ea4de4171
2 changed files with 59 additions and 6 deletions

View file

@ -216,14 +216,38 @@ class phpbb_extension_finder
return $classes; return $classes;
} }
/**
* Finds all directories matching the configured options
*
* @param bool $cache Whether the result should be cached
* @return array An array of paths to found directories
*/
public function get_directories($cache = true)
{
return $this->find($cache, true);
}
/** /**
* Finds all files matching the configured options. * Finds all files matching the configured options.
* *
* @param bool $cache Whether the result should be cached * @param bool $cache Whether the result should be cached
* @return array An array of found class names * @return array An array of paths to found files
*/ */
public function get_files($cache = true) public function get_files($cache = true)
{ {
return $this->find($cache, false);
}
/**
* Finds all file system entries matching the configured options
*
* @param bool $cache Whether the result should be cached
* @param bool $is_dir Whether the found items should be directories
* @return array An array of paths to found items
*/
protected function find($cache = true, $is_dir = false)
{
$this->query['is_dir'] = $is_dir;
$query = md5(serialize($this->query)); $query = md5(serialize($this->query));
if (!defined('DEBUG') && $cache && isset($this->cached_queries[$query])) if (!defined('DEBUG') && $cache && isset($this->cached_queries[$query]))
@ -265,18 +289,33 @@ class phpbb_extension_finder
} }
// match only first directory if leading slash is given // match only first directory if leading slash is given
$directory_pattern = ($directory && $directory[0] === '/') ? '#^' : '#' . DIRECTORY_SEPARATOR; if ($directory === '/')
$directory_pattern .= preg_quote($directory . DIRECTORY_SEPARATOR, '#') . '#'; {
$directory_pattern = '^' . preg_quote(DIRECTORY_SEPARATOR, '#');
}
else if ($directory && $directory[0] === '/')
{
$directory_pattern = '^' . preg_quote($directory . DIRECTORY_SEPARATOR, '#');
}
else
{
$directory_pattern = preg_quote(DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR, '#');
}
$directory_pattern = '#' . $directory_pattern . '#';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($iterator as $file_info) foreach ($iterator as $file_info)
{ {
if (!$file_info->isDir()) if ($file_info->isDir() == $is_dir)
{ {
$relative_path = $iterator->getInnerIterator()->getSubPathname(); $relative_path = ($is_dir) ? $iterator->getInnerIterator()->getSubPath() . DIRECTORY_SEPARATOR:
$iterator->getInnerIterator()->getSubPathname();
$item_name = ($is_dir) ? basename($iterator->getInnerIterator()->getSubPath()) :
$file_info->getFilename();
if ((!$suffix || substr($relative_path, -strlen($suffix)) === $suffix) && if ((!$suffix || substr($relative_path, -strlen($suffix)) === $suffix) &&
(!$prefix || substr($file_info->getFilename(), 0, strlen($prefix)) === $prefix) && (!$prefix || substr($item_name, 0, strlen($prefix)) === $prefix) &&
(!$directory || preg_match($directory_pattern, DIRECTORY_SEPARATOR . $relative_path))) (!$directory || preg_match($directory_pattern, DIRECTORY_SEPARATOR . $relative_path)))
{ {
$files[] = str_replace(DIRECTORY_SEPARATOR, '/', $location . $name . $relative_path); $files[] = str_replace(DIRECTORY_SEPARATOR, '/', $location . $name . $relative_path);

View file

@ -55,6 +55,18 @@ class phpbb_extension_finder_test extends phpbb_test_case
); );
} }
public function test_prefix_get_directories()
{
$dirs = $this->finder
->directory('/type')
->get_directories();
sort($dirs);
$this->assertEquals(array(
'ext/foo/type/',
), $dirs);
}
public function test_prefix_get_classes() public function test_prefix_get_classes()
{ {
$classes = $this->finder $classes = $this->finder
@ -144,6 +156,7 @@ class phpbb_extension_finder_test extends phpbb_test_case
'suffix' => '_class.php', 'suffix' => '_class.php',
'prefix' => false, 'prefix' => false,
'directory' => false, 'directory' => false,
'is_dir' => false,
); );
$this->assertEquals($expected_files, $files); $this->assertEquals($expected_files, $files);
@ -162,6 +175,7 @@ class phpbb_extension_finder_test extends phpbb_test_case
'suffix' => false, 'suffix' => false,
'prefix' => false, 'prefix' => false,
'directory' => false, 'directory' => false,
'is_dir' => false,
); );
$finder = new phpbb_extension_finder($this->extension_manager, dirname(__FILE__) . '/includes/', new phpbb_mock_cache(array( $finder = new phpbb_extension_finder($this->extension_manager, dirname(__FILE__) . '/includes/', new phpbb_mock_cache(array(