1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-21 16:22:22 +02:00

Merge remote-tracking branch 'github-nickvergessen/ticket/12508' into develop-ascraeus

* github-nickvergessen/ticket/12508:
  [ticket/12508] Ignore extensions in migration_tips dev tool
  [ticket/12508] Fix doc block
  [ticket/12508] Add new line to separate the if and foreach better
  [ticket/12508] Add a unit test for set_extensions()
  [ticket/12508] Only take a list of names for set_extensions()
  [ticket/12508] Fix class doc block
  [ticket/12508] Move \phpbb\extension\finder to \phpbb\finder
  [ticket/12508] Fix usages of the finder
  [ticket/12508] Remove extension manager from finder
This commit is contained in:
Nils Adermann 2014-06-10 18:40:07 +02:00
commit 9842137028
11 changed files with 125 additions and 78 deletions

View File

@ -44,17 +44,9 @@ require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx);
$phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx);
$phpbb_class_loader->register();
class phpbb_extension_empty_manager extends \phpbb\extension\manager
{
public function __construct()
{
$this->extensions = array();
}
}
$finder = new \phpbb\extension\finder(new \phpbb_extension_empty_manager(), new \phpbb\filesystem(), $phpbb_root_path);
$finder = new \phpbb\finder(new \phpbb\filesystem(), $phpbb_root_path);
$classes = $finder->core_path('phpbb/')
->directory('db/migration/data')
->directory('/db/migration/data')
->get_classes();
$db = new \phpbb\db\driver\sqlite();

View File

@ -561,14 +561,14 @@ class acp_modules
$directory = $phpbb_root_path . 'includes/' . $module_class . '/info/';
$fileinfo = array();
$finder = $phpbb_extension_manager->get_finder();
$finder = $phpbb_extension_manager->get_finder($use_all_available);
$modules = $finder
->extension_suffix('_module')
->extension_directory("/$module_class")
->core_path("includes/$module_class/info/")
->core_prefix($module_class . '_')
->get_classes(true, $use_all_available);
->get_classes(true);
foreach ($modules as $cur_module)
{

View File

@ -37,6 +37,7 @@ class migration_tips extends \phpbb\console\command\command
protected function execute(InputInterface $input, OutputInterface $output)
{
$migrations = $this->extension_manager->get_finder()
->set_extensions(array())
->core_path('phpbb/db/migration/data/')
->get_classes();
$tips = $migrations;

View File

@ -46,10 +46,10 @@ class provider
}
/**
* @param \phpbb\extension\finder $finder
* @param \phpbb\finder $finder
* @return null
*/
public function find_routing_files(\phpbb\extension\finder $finder)
public function find_routing_files(\phpbb\finder $finder)
{
// We hardcode the path to the core config directory
// because the finder cannot find it

View File

@ -714,7 +714,7 @@ class migrator
/**
* Load migration data files from a directory
*
* @param \phpbb\extension\finder $finder
* @param \phpbb\finder $finder
* @param string $path Path to migration data files
* @param bool $check_fulfillable If TRUE (default), we will check
* if all of the migrations are fulfillable after loading them.
@ -723,7 +723,7 @@ class migrator
* with the last call to prevent throwing errors unnecessarily).
* @return array Array of migration names
*/
public function load_migrations(\phpbb\extension\finder $finder, $path, $check_fulfillable = true)
public function load_migrations(\phpbb\finder $finder, $path, $check_fulfillable = true)
{
if (!is_dir($path))
{

View File

@ -23,7 +23,7 @@ class base implements \phpbb\extension\extension_interface
/** @var ContainerInterface */
protected $container;
/** @var \phpbb\extension\finder */
/** @var \phpbb\finder */
protected $finder;
/** @var \phpbb\db\migrator */
@ -39,11 +39,11 @@ class base implements \phpbb\extension\extension_interface
* Constructor
*
* @param ContainerInterface $container Container object
* @param \phpbb\extension\finder $extension_finder
* @param \phpbb\finder $extension_finder
* @param string $extension_name Name of this extension (from ext.manager)
* @param string $extension_path Relative path to this extension
*/
public function __construct(ContainerInterface $container, \phpbb\extension\finder $extension_finder, \phpbb\db\migrator $migrator, $extension_name, $extension_path)
public function __construct(ContainerInterface $container, \phpbb\finder $extension_finder, \phpbb\db\migrator $migrator, $extension_name, $extension_path)
{
$this->container = $container;
$this->extension_finder = $extension_finder;

View File

@ -532,12 +532,22 @@ class manager
}
/**
* Instantiates a \phpbb\extension\finder.
* Instantiates a \phpbb\finder.
*
* @return \phpbb\extension\finder An extension finder instance
* @param bool $use_all_available Should we load all extensions, or just enabled ones
* @return \phpbb\finder An extension finder instance
*/
public function get_finder()
public function get_finder($use_all_available = false)
{
return new \phpbb\extension\finder($this, $this->filesystem, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder');
$finder = new \phpbb\finder($this->filesystem, $this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder');
if ($use_all_available)
{
$finder->set_extensions(array_keys($this->all_available()));
}
else
{
$finder->set_extensions(array_keys($this->all_enabled()));
}
return $finder;
}
}

View File

@ -11,14 +11,14 @@
*
*/
namespace phpbb\extension;
namespace phpbb;
/**
* The extension finder provides a simple way to locate files in active extensions
* The finder provides a simple way to locate files in the core and a set of extensions
*/
class finder
{
protected $extension_manager;
protected $extensions;
protected $filesystem;
protected $phpbb_root_path;
protected $cache;
@ -48,9 +48,6 @@ class finder
/**
* Creates a new finder instance with its dependencies
*
* @param \phpbb\extension\manager $extension_manager An extension manager
* instance that provides the finder with a list of active
* extensions and their locations
* @param \phpbb\filesystem $filesystem Filesystem instance
* @param string $phpbb_root_path Path to the phpbb root directory
* @param \phpbb\cache\driver\driver_interface $cache A cache instance or null
@ -58,9 +55,8 @@ class finder
* @param string $cache_name The name of the cache variable, defaults to
* _ext_finder
*/
public function __construct(\phpbb\extension\manager $extension_manager, \phpbb\filesystem $filesystem, $phpbb_root_path = '', \phpbb\cache\driver\driver_interface $cache = null, $php_ext = 'php', $cache_name = '_ext_finder')
public function __construct(\phpbb\filesystem $filesystem, $phpbb_root_path = '', \phpbb\cache\driver\driver_interface $cache = null, $php_ext = 'php', $cache_name = '_ext_finder')
{
$this->extension_manager = $extension_manager;
$this->filesystem = $filesystem;
$this->phpbb_root_path = $phpbb_root_path;
$this->cache = $cache;
@ -76,15 +72,37 @@ class finder
'extension_prefix' => false,
'extension_directory' => false,
);
$this->extensions = array();
$this->cached_queries = ($this->cache) ? $this->cache->get($this->cache_name) : false;
}
/**
* Set the array of extensions
*
* @param array $extensions A list of extensions that should be searched aswell
* @param bool $replace_list Should the list be emptied before adding the extensions
* @return \phpbb\finder This object for chaining calls
*/
public function set_extensions(array $extensions, $replace_list = true)
{
if ($replace_list)
{
$this->extensions = array();
}
foreach ($extensions as $ext_name)
{
$this->extensions[$ext_name] = $this->phpbb_root_path . 'ext/' . $ext_name . '/';
}
return $this;
}
/**
* Sets a core path to be searched in addition to extensions
*
* @param string $core_path The path relative to phpbb_root_path
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function core_path($core_path)
{
@ -100,7 +118,7 @@ class finder
* file extension is automatically added to suffixes.
*
* @param string $suffix A filename suffix
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function suffix($suffix)
{
@ -117,7 +135,7 @@ class finder
* file extension is automatically added to suffixes.
*
* @param string $extension_suffix A filename suffix
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function extension_suffix($extension_suffix)
{
@ -133,7 +151,7 @@ class finder
* file extension is automatically added to suffixes.
*
* @param string $core_suffix A filename suffix
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function core_suffix($core_suffix)
{
@ -145,7 +163,7 @@ class finder
* Sets the prefix all files found in extensions and core must match
*
* @param string $prefix A filename prefix
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function prefix($prefix)
{
@ -158,7 +176,7 @@ class finder
* Sets a prefix all files found in extensions must match
*
* @param string $extension_prefix A filename prefix
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function extension_prefix($extension_prefix)
{
@ -170,7 +188,7 @@ class finder
* Sets a prefix all files found in the core path must match
*
* @param string $core_prefix A filename prefix
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function core_prefix($core_prefix)
{
@ -185,7 +203,7 @@ class finder
* the current directory.
*
* @param string $directory
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function directory($directory)
{
@ -198,7 +216,7 @@ class finder
* Sets a directory all files found in extensions must be contained in
*
* @param string $extension_directory
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function extension_directory($extension_directory)
{
@ -210,7 +228,7 @@ class finder
* Sets a directory all files found in the core path must be contained in
*
* @param string $core_directory
* @return \phpbb\extension\finder This object for chaining calls
* @return \phpbb\finder This object for chaining calls
*/
public function core_directory($core_directory)
{
@ -246,16 +264,14 @@ class finder
* phpBB naming rules an incorrect class name will be returned.
*
* @param bool $cache Whether the result should be cached
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
* @return array An array of found class names
*/
public function get_classes($cache = true, $use_all_available = false)
public function get_classes($cache = true)
{
$this->query['extension_suffix'] .= '.' . $this->php_ext;
$this->query['core_suffix'] .= '.' . $this->php_ext;
$files = $this->find($cache, false, $use_all_available);
$files = $this->find($cache, false);
return $this->get_classes_from_files($files);
}
@ -290,27 +306,23 @@ class finder
* Finds all directories matching the configured options
*
* @param bool $cache Whether the result should be cached
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
* @param bool $extension_keys Whether the result should have extension name as array key
* @return array An array of paths to found directories
*/
public function get_directories($cache = true, $use_all_available = false, $extension_keys = false)
public function get_directories($cache = true, $extension_keys = false)
{
return $this->find_with_root_path($cache, true, $use_all_available, $extension_keys);
return $this->find_with_root_path($cache, true, $extension_keys);
}
/**
* Finds all files matching the configured options.
*
* @param bool $cache Whether the result should be cached
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
* @return array An array of paths to found files
*/
public function get_files($cache = true, $use_all_available = false)
public function get_files($cache = true)
{
return $this->find_with_root_path($cache, false, $use_all_available);
return $this->find_with_root_path($cache, false);
}
/**
@ -318,16 +330,14 @@ class finder
*
* @param bool $cache Whether the result should be cached
* @param bool $is_dir Directories will be returned when true, only files
* otherwise
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
* otherwise
* @param bool $extension_keys If true, result will be associative array
* with extension name as key
* @return array An array of paths to found items
*/
protected function find_with_root_path($cache = true, $is_dir = false, $use_all_available = false, $extension_keys = false)
protected function find_with_root_path($cache = true, $is_dir = false, $extension_keys = false)
{
$items = $this->find($cache, $is_dir, $use_all_available);
$items = $this->find($cache, $is_dir);
$result = array();
foreach ($items as $item => $ext_name)
@ -351,21 +361,11 @@ class finder
* @param bool $cache Whether the result should be cached
* @param bool $is_dir Directories will be returned when true, only files
* otherwise
* @param bool $use_all_available Use all available instead of just all
* enabled extensions
* @return array An array of paths to found items
*/
public function find($cache = true, $is_dir = false, $use_all_available = false)
public function find($cache = true, $is_dir = false)
{
if ($use_all_available)
{
$extensions = $this->extension_manager->all_available();
}
else
{
$extensions = $this->extension_manager->all_enabled();
}
$extensions = $this->extensions;
if ($this->query['core_path'])
{
$extensions['/'] = $this->phpbb_root_path . $this->query['core_path'];

View File

@ -42,12 +42,12 @@ class phpbb_controller_helper_route_test extends phpbb_test_case
)
);
$finder = new \phpbb\extension\finder(
$this->extension_manager,
$finder = new \phpbb\finder(
new \phpbb\filesystem(),
dirname(__FILE__) . '/',
new phpbb_mock_cache()
);
$finder->set_extensions(array_keys($this->extension_manager->all_enabled()));
$this->provider = new \phpbb\controller\provider();
$this->provider->find_routing_files($finder);
$this->provider->find(dirname(__FILE__) . '/');

View File

@ -14,7 +14,9 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_extension_finder_test extends phpbb_test_case
{
/** @var \phpbb\extension\manager */
protected $extension_manager;
/** @var \phpbb\finder */
protected $finder;
public function setUp()
@ -56,6 +58,47 @@ class phpbb_extension_finder_test extends phpbb_test_case
);
}
public function set_extensions_data()
{
return array(
array(
array(),
array('\phpbb\default\implementation'),
),
array(
array('vendor3/bar'),
array(
'\phpbb\default\implementation',
'\vendor3\bar\my\hidden_class',
),
),
array(
array('vendor2/foo', 'vendor3/bar'),
array(
'\phpbb\default\implementation',
'\vendor2\foo\a_class',
'\vendor2\foo\b_class',
'\vendor3\bar\my\hidden_class',
),
),
);
}
/**
* @dataProvider set_extensions_data
*/
public function test_set_extensions($extensions, $expected)
{
$classes = $this->finder
->set_extensions($extensions)
->core_path('phpbb/default/')
->extension_suffix('_class')
->get_classes();
sort($classes);
$this->assertEquals($expected, $classes);
}
public function test_get_directories()
{
$dirs = $this->finder
@ -201,7 +244,8 @@ class phpbb_extension_finder_test extends phpbb_test_case
public function test_get_classes_create_cache()
{
$cache = new phpbb_mock_cache;
$finder = new \phpbb\extension\finder($this->extension_manager, new \phpbb\filesystem(), dirname(__FILE__) . '/', $cache, 'php', '_custom_cache_name');
$finder = new \phpbb\finder(new \phpbb\filesystem(), dirname(__FILE__) . '/', $cache, 'php', '_custom_cache_name');
$finder->set_extensions(array_keys($this->extension_manager->all_enabled()));
$files = $finder->suffix('_class.php')->get_files();
$expected_files = array(
@ -239,8 +283,7 @@ class phpbb_extension_finder_test extends phpbb_test_case
'is_dir' => false,
);
$finder = new \phpbb\extension\finder(
$this->extension_manager,
$finder = new \phpbb\finder(
new \phpbb\filesystem(),
dirname(__FILE__) . '/',
new phpbb_mock_cache(array(
@ -249,6 +292,7 @@ class phpbb_extension_finder_test extends phpbb_test_case
),
))
);
$finder->set_extensions(array_keys($this->extension_manager->all_enabled()));
$classes = $finder
->core_path($query['core_path'])

View File

@ -35,16 +35,16 @@ class phpbb_pagination_pagination_test extends phpbb_template_template_test_case
->will($this->returnCallback(array($this, 'return_callback_implode')));
$manager = new phpbb_mock_extension_manager(dirname(__FILE__) . '/', array());
$this->finder = new \phpbb\extension\finder(
$manager,
$finder = new \phpbb\finder(
new \phpbb\filesystem(),
dirname(__FILE__) . '/',
new phpbb_mock_cache()
);
$finder->set_extensions(array_keys($manager->all_enabled()));
$this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1'));
$provider = new \phpbb\controller\provider();
$provider->find_routing_files($this->finder);
$provider->find_routing_files($finder);
$provider->find(dirname(__FILE__) . '/');
$this->helper = new phpbb_mock_controller_helper($this->template, $this->user, $this->config, $provider, $manager, '', 'php', dirname(__FILE__) . '/');
$this->pagination = new \phpbb\pagination($this->template, $this->user, $this->helper);