diff --git a/phpBB/config/default/container/services.yml b/phpBB/config/default/container/services.yml index 1d48ead588..ba78673211 100644 --- a/phpBB/config/default/container/services.yml +++ b/phpBB/config/default/container/services.yml @@ -12,6 +12,7 @@ imports: - { resource: services_feed.yml } - { resource: services_files.yml } - { resource: services_filesystem.yml } + - { resource: services_finder.yml } - { resource: services_help.yml } - { resource: services_http.yml } - { resource: services_language.yml } diff --git a/phpBB/config/default/container/services_extensions.yml b/phpBB/config/default/container/services_extensions.yml index 53e36f0fda..2c04716683 100644 --- a/phpBB/config/default/container/services_extensions.yml +++ b/phpBB/config/default/container/services_extensions.yml @@ -5,9 +5,9 @@ services: - '@service_container' - '@dbal.conn' - '@config' + - '@finder.factory' - '%tables.ext%' - '%core.root_path%' - - '%core.php_ext%' - '@cache' ext.composer.installer: diff --git a/phpBB/config/default/container/services_finder.yml b/phpBB/config/default/container/services_finder.yml new file mode 100644 index 0000000000..9937f35396 --- /dev/null +++ b/phpBB/config/default/container/services_finder.yml @@ -0,0 +1,8 @@ +services: + finder.factory: + class: phpbb\finder\factory + arguments: + - '@cache' + - '%finder.not_use_cache%' + - '%core.root_path%' + - '%core.php_ext%' diff --git a/phpBB/config/development/config.yml b/phpBB/config/development/config.yml index 0c28916e27..fb96e5251c 100644 --- a/phpBB/config/development/config.yml +++ b/phpBB/config/development/config.yml @@ -25,3 +25,6 @@ core: session: log_errors: true + + finder: + not_use_cache: true diff --git a/phpBB/config/installer/container/services_install_database.yml b/phpBB/config/installer/container/services_install_database.yml index 14baed7e79..5f10ff0373 100644 --- a/phpBB/config/installer/container/services_install_database.yml +++ b/phpBB/config/installer/container/services_install_database.yml @@ -8,6 +8,7 @@ services: - '%core.root_path%' - '%core.php_ext%' - '%tables%' + - '%finder.not_use_cache%' tags: - { name: install_database_install, order: 1 } diff --git a/phpBB/develop/create_schema_files.php b/phpBB/develop/create_schema_files.php index e057975ba9..912b681575 100644 --- a/phpBB/develop/create_schema_files.php +++ b/phpBB/develop/create_schema_files.php @@ -43,7 +43,7 @@ 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(); -$finder = new \phpbb\finder($phpbb_root_path); +$finder = new \phpbb\finder\finder(null, true, $phpbb_root_path, $phpEx); $classes = $finder->core_path('phpbb/') ->directory('/db/migration/data') ->get_classes(); diff --git a/phpBB/phpbb/di/extension/container_configuration.php b/phpBB/phpbb/di/extension/container_configuration.php index b3a726ad2a..4d23de22de 100644 --- a/phpBB/phpbb/di/extension/container_configuration.php +++ b/phpBB/phpbb/di/extension/container_configuration.php @@ -64,6 +64,10 @@ class container_configuration implements ConfigurationInterface ->children() ->booleanNode('force_sid')->defaultValue(false)->end() ->booleanNode('log_errors')->defaultValue(false)->end() + ->arrayNode('finder') + ->addDefaultsIfNotSet() + ->children() + ->booleanNode('not_use_cache')->defaultValue(false)->end() ->end() ->end() ->end() diff --git a/phpBB/phpbb/di/extension/core.php b/phpBB/phpbb/di/extension/core.php index 65848cdadf..20f3501be7 100644 --- a/phpBB/phpbb/di/extension/core.php +++ b/phpBB/phpbb/di/extension/core.php @@ -117,6 +117,11 @@ class core extends Extension foreach ($config['session'] as $name => $value) { $container->setParameter('session.' . $name, $value); + + // Set the finder options + foreach ($config['finder'] as $name => $value) + { + $container->setParameter('finder.' . $name, $value); } } diff --git a/phpBB/phpbb/extension/base.php b/phpBB/phpbb/extension/base.php index c056ab8bb3..57a281fdae 100644 --- a/phpBB/phpbb/extension/base.php +++ b/phpBB/phpbb/extension/base.php @@ -23,7 +23,7 @@ class base implements \phpbb\extension\extension_interface /** @var ContainerInterface */ protected $container; - /** @var \phpbb\finder */ + /** @var \phpbb\finder\finder */ protected $extension_finder; /** @var \phpbb\db\migrator */ @@ -42,12 +42,12 @@ class base implements \phpbb\extension\extension_interface * Constructor * * @param ContainerInterface $container Container object - * @param \phpbb\finder $extension_finder + * @param \phpbb\finder\finder $extension_finder * @param \phpbb\db\migrator $migrator * @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\finder $extension_finder, \phpbb\db\migrator $migrator, $extension_name, $extension_path) + public function __construct(ContainerInterface $container, \phpbb\finder\finder $extension_finder, \phpbb\db\migrator $migrator, $extension_name, $extension_path) { $this->container = $container; $this->extension_finder = $extension_finder; diff --git a/phpBB/phpbb/extension/manager.php b/phpBB/phpbb/extension/manager.php index 48d0fb6c92..275043d7e8 100644 --- a/phpBB/phpbb/extension/manager.php +++ b/phpBB/phpbb/extension/manager.php @@ -15,6 +15,7 @@ namespace phpbb\extension; use phpbb\exception\runtime_exception; use phpbb\file_downloader; +use phpbb\finder\factory as finder_factory; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -27,8 +28,8 @@ class manager protected $db; protected $config; + protected $finder_factory; protected $cache; - protected $php_ext; protected $extensions; protected $extension_table; protected $phpbb_root_path; @@ -42,20 +43,19 @@ class manager * @param \phpbb\config\config $config Config object * @param string $extension_table The name of the table holding extensions * @param string $phpbb_root_path Path to the phpbb includes directory. - * @param string $php_ext php file extension, defaults to php * @param \phpbb\cache\service|null $cache A cache instance or null * @param string $cache_name The name of the cache variable, defaults to _ext */ - public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $extension_table, $phpbb_root_path, $php_ext = 'php', \phpbb\cache\service $cache = null, $cache_name = '_ext') + public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, finder_factory $finder_factory, $extension_table, $phpbb_root_path, \phpbb\cache\service $cache = null, $cache_name = '_ext') { $this->cache = $cache; $this->cache_name = $cache_name; $this->config = $config; + $this->finder_factory = $finder_factory; $this->container = $container; $this->db = $db; $this->extension_table = $extension_table; $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $php_ext; $this->extensions = ($this->cache) ? $this->cache->get($this->cache_name) : false; @@ -568,14 +568,15 @@ class manager } /** - * Instantiates a \phpbb\finder. + * Instantiates a \phpbb\finder\finder. * * @param bool $use_all_available Should we load all extensions, or just enabled ones - * @return \phpbb\finder An extension finder instance + * @return \phpbb\finder\finder An extension finder instance */ public function get_finder($use_all_available = false) { - $finder = new \phpbb\finder($this->phpbb_root_path, $this->cache, $this->php_ext, $this->cache_name . '_finder'); + $finder = $this->finder_factory->get($this->cache_name . '_finder'); + if ($use_all_available) { $finder->set_extensions(array_keys($this->all_available())); @@ -584,6 +585,7 @@ class manager { $finder->set_extensions(array_keys($this->all_enabled())); } + return $finder; } } diff --git a/phpBB/phpbb/finder/factory.php b/phpBB/phpbb/finder/factory.php new file mode 100644 index 0000000000..21b568b735 --- /dev/null +++ b/phpBB/phpbb/finder/factory.php @@ -0,0 +1,53 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\finder; + +/** +* The finder provides a simple way to locate files in the core and a set of extensions +*/ +class factory +{ + protected $cache; + protected $not_use_cache; + protected $phpbb_root_path; + protected $php_ext; + + /** + * Creates a new finder instance with its dependencies + * + * @param string $phpbb_root_path Path to the phpbb root directory + * @param \phpbb\cache\service $cache A cache instance or null + * @param string $php_ext php file extension + * @param string $cache_name The name of the cache variable, defaults to + * _ext_finder + */ + public function __construct(/*\phpbb\cache\service */ $cache, $not_use_cache, $phpbb_root_path, $php_ext) + { + $this->cache = $cache; + $this->not_use_cache = $not_use_cache; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + } + + /** + * The cache variable name used to store $this->cached_queries in $this->cache. + * + * Allows the use of multiple differently configured finders with the same cache. + * @var string + */ + public function get($cache_name = '_ext_finder') + { + return new finder($this->cache, $this->not_use_cache, $this->phpbb_root_path, $this->php_ext, $cache_name); + } +} diff --git a/phpBB/phpbb/finder.php b/phpBB/phpbb/finder/finder.php similarity index 93% rename from phpBB/phpbb/finder.php rename to phpBB/phpbb/finder/finder.php index 1815377a7d..80b98b5eff 100644 --- a/phpBB/phpbb/finder.php +++ b/phpBB/phpbb/finder/finder.php @@ -11,7 +11,7 @@ * */ -namespace phpbb; +namespace phpbb\finder; use phpbb\filesystem\helper as filesystem_helper; @@ -23,6 +23,7 @@ class finder protected $extensions; protected $phpbb_root_path; protected $cache; + protected $not_use_cache; protected $php_ext; /** @@ -55,10 +56,11 @@ class finder * @param string $cache_name The name of the cache variable, defaults to * _ext_finder */ - public function __construct($phpbb_root_path = '', \phpbb\cache\service $cache = null, $php_ext = 'php', $cache_name = '_ext_finder') + public function __construct(/*\phpbb\cache\service */ $cache, $not_use_cache, $phpbb_root_path, $php_ext, $cache_name = '_ext_finder') { $this->phpbb_root_path = $phpbb_root_path; $this->cache = $cache; + $this->not_use_cache = $not_use_cache; $this->php_ext = $php_ext; $this->cache_name = $cache_name; @@ -81,7 +83,7 @@ class finder * * @param array $extensions A list of extensions that should be searched as well * @param bool $replace_list Should the list be emptied before adding the extensions - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function set_extensions(array $extensions, $replace_list = true) { @@ -101,7 +103,7 @@ class finder * Sets a core path to be searched in addition to extensions * * @param string $core_path The path relative to phpbb_root_path - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function core_path($core_path) { @@ -117,7 +119,7 @@ class finder * file extension is automatically added to suffixes. * * @param string $suffix A filename suffix - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function suffix($suffix) { @@ -134,7 +136,7 @@ class finder * file extension is automatically added to suffixes. * * @param string $extension_suffix A filename suffix - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function extension_suffix($extension_suffix) { @@ -150,7 +152,7 @@ class finder * file extension is automatically added to suffixes. * * @param string $core_suffix A filename suffix - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function core_suffix($core_suffix) { @@ -162,7 +164,7 @@ class finder * Sets the prefix all files found in extensions and core must match * * @param string $prefix A filename prefix - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function prefix($prefix) { @@ -175,7 +177,7 @@ class finder * Sets a prefix all files found in extensions must match * * @param string $extension_prefix A filename prefix - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function extension_prefix($extension_prefix) { @@ -187,7 +189,7 @@ class finder * Sets a prefix all files found in the core path must match * * @param string $core_prefix A filename prefix - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function core_prefix($core_prefix) { @@ -202,7 +204,7 @@ class finder * the current directory. * * @param string $directory - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function directory($directory) { @@ -215,7 +217,7 @@ class finder * Sets a directory all files found in extensions must be contained in * * @param string $extension_directory - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function extension_directory($extension_directory) { @@ -227,7 +229,7 @@ class finder * Sets a directory all files found in the core path must be contained in * * @param string $core_directory - * @return \phpbb\finder This object for chaining calls + * @return \phpbb\finder\finder This object for chaining calls */ public function core_directory($core_directory) { @@ -424,7 +426,7 @@ class finder $this->query['is_dir'] = $is_dir; $query = md5(serialize($this->query) . serialize($extensions)); - if (!defined('DEBUG') && $cache && isset($this->cached_queries[$query])) + if (!$this->not_use_cache && $cache && isset($this->cached_queries[$query])) { return $this->cached_queries[$query]; } diff --git a/phpBB/phpbb/install/module/install_database/task/create_schema_file.php b/phpBB/phpbb/install/module/install_database/task/create_schema_file.php index 99febcbd18..1788271365 100644 --- a/phpBB/phpbb/install/module/install_database/task/create_schema_file.php +++ b/phpBB/phpbb/install/module/install_database/task/create_schema_file.php @@ -45,6 +45,11 @@ class create_schema_file extends \phpbb\install\task_base */ protected $php_ext; + /** + * @var string + */ + protected $not_use_cache; + /** * Constructor * @@ -58,7 +63,8 @@ class create_schema_file extends \phpbb\install\task_base \phpbb\install\helper\database $db_helper, \phpbb\filesystem\filesystem_interface $filesystem, $phpbb_root_path, - $php_ext) + $php_ext, + $not_use_cache) { $dbms = $db_helper->get_available_dbms($config->get('dbms')); $dbms = $dbms[$config->get('dbms')]['DRIVER']; @@ -78,6 +84,7 @@ class create_schema_file extends \phpbb\install\task_base $this->filesystem = $filesystem; $this->phpbb_root_path = $phpbb_root_path; $this->php_ext = $php_ext; + $this->not_use_cache = $not_use_cache; parent::__construct(true); } @@ -117,7 +124,7 @@ class create_schema_file extends \phpbb\install\task_base include ($this->phpbb_root_path . 'includes/constants.' . $this->php_ext); } - $finder = new \phpbb\finder($this->phpbb_root_path, null, $this->php_ext); + $finder = new \phpbb\finder\finder(null, true, $this->phpbb_root_path, $this->php_ext); $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes(); $factory = new \phpbb\db\tools\factory(); $db_tools = $factory->get($this->db, true); diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 71a3ba7ef4..e75b45522e 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -54,6 +54,8 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->config = new \phpbb\config\db($this->db, new phpbb_mock_cache, 'phpbb_config'); + $finder_factory = $this->createMock('\phpbb\finder\factory'); + $tools = array( new \phpbb\db\migration\tool\config($this->config), ); @@ -80,9 +82,9 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $container, $this->db, $this->config, + $finder_factory, 'phpbb_ext', __DIR__ . '/../../phpBB/', - 'php', null ); } diff --git a/tests/extension/finder_test.php b/tests/extension/finder_test.php index e890fd10af..75d400269c 100644 --- a/tests/extension/finder_test.php +++ b/tests/extension/finder_test.php @@ -15,7 +15,7 @@ class phpbb_extension_finder_test extends phpbb_test_case { /** @var \phpbb\extension\manager */ protected $extension_manager; - /** @var \phpbb\finder */ + /** @var \phpbb\finder\finder */ protected $finder; protected function setUp(): void @@ -243,7 +243,7 @@ class phpbb_extension_finder_test extends phpbb_test_case public function test_get_classes_create_cache() { $cache = new phpbb_mock_cache; - $finder = new \phpbb\finder(__DIR__ . '/', $cache, 'php', '_custom_cache_name'); + $finder = new \phpbb\finder\finder($cache, true, __DIR__ . '/', 'php', '_custom_cache_name'); $finder->set_extensions(array_keys($this->extension_manager->all_enabled())); $files = $finder->suffix('_class.php')->get_files(); @@ -282,13 +282,15 @@ class phpbb_extension_finder_test extends phpbb_test_case 'is_dir' => false, ); - $finder = new \phpbb\finder( - __DIR__ . '/', + $finder = new \phpbb\finder\finder( new phpbb_mock_cache(array( '_ext_finder' => array( md5(serialize($query)) => array('file_name' => 'extension'), ), - )) + )), + true, + __DIR__ . '/', + '_ext_finder' ); $finder->set_extensions(array_keys($this->extension_manager->all_enabled())); diff --git a/tests/extension/manager_test.php b/tests/extension/manager_test.php index e984bf8957..14b6a9c57f 100644 --- a/tests/extension/manager_test.php +++ b/tests/extension/manager_test.php @@ -147,13 +147,14 @@ class phpbb_extension_manager_test extends phpbb_database_test_case protected function create_extension_manager($with_cache = true) { + $phpbb_root_path = __DIR__ . './../../phpBB/'; + $php_ext = 'php'; $config = new \phpbb\config\config(array('version' => PHPBB_VERSION)); $db = $this->new_dbal(); $factory = new \phpbb\db\tools\factory(); + $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $php_ext); $db_tools = $factory->get($db); - $phpbb_root_path = __DIR__ . './../../phpBB/'; - $php_ext = 'php'; $table_prefix = 'phpbb_'; $container = new phpbb_mock_container_builder(); @@ -177,9 +178,9 @@ class phpbb_extension_manager_test extends phpbb_database_test_case $container, $db, $config, + $finder_factory, 'phpbb_ext', __DIR__ . '/', - $php_ext, ($with_cache) ? new \phpbb\cache\service(new phpbb_mock_cache(), $config, $db, $phpbb_root_path, $php_ext) : null ); } diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index e385ad323c..e4ae09064e 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -42,6 +42,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $this->db = $this->new_dbal(); $factory = new \phpbb\db\tools\factory(); $this->db_tools = $factory->get($this->db); + $finder_factory = $this->createMock('\phpbb\finder\factory'); $this->phpbb_root_path = __DIR__ . '/'; $this->phpEx = 'php'; @@ -97,9 +98,9 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $container, $this->db, $this->config, + $finder_factory, 'phpbb_ext', $this->phpbb_root_path, - $this->phpEx, $this->cache ); diff --git a/tests/mock/container_builder.php b/tests/mock/container_builder.php index 5b73256fdd..db389929ed 100644 --- a/tests/mock/container_builder.php +++ b/tests/mock/container_builder.php @@ -23,6 +23,7 @@ class phpbb_mock_container_builder implements ContainerInterface $this->setParameter('debug.load_time', false); $this->setParameter('session.log_errors', false); $this->setParameter('session.force_sid', true); + $this->setParameter('finder.not_use_cache', false); } /** diff --git a/tests/mock/extension_manager.php b/tests/mock/extension_manager.php index 0d6d110647..e38b1a1b1e 100644 --- a/tests/mock/extension_manager.php +++ b/tests/mock/extension_manager.php @@ -19,11 +19,11 @@ class phpbb_mock_extension_manager extends \phpbb\extension\manager $lang = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = 'php'; $this->extensions = $extensions; $this->filesystem = new \phpbb\filesystem\filesystem(); $this->container = $container; $this->config = new \phpbb\config\config(array()); $this->user = new \phpbb\user($lang,'\phpbb\datetime'); + $this->finder_factory = new \phpbb\finder\factory(null, true, $this->phpbb_root_path, $phpEx); } } diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index e9ffe62ad0..deaee99cd1 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -74,7 +74,7 @@ abstract class phpbb_database_test_case extends TestCase $setup_extensions = static::setup_extensions(); - $finder = new \phpbb\finder($phpbb_root_path, null, $phpEx); + $finder = new \phpbb\finder\finder(null, true, $phpbb_root_path, $phpEx); $finder->core_path('phpbb/db/migration/data/'); if (!empty($setup_extensions)) { diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index ced91fa37e..fdc6736a80 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -365,7 +365,7 @@ class phpbb_database_test_connection_manager { global $phpbb_root_path, $phpEx, $table_prefix; - $finder = new \phpbb\finder($phpbb_root_path, null, $phpEx); + $finder = new \phpbb\finder\finder(null, true, $phpbb_root_path, $phpEx); $classes = $finder->core_path('phpbb/db/migration/data/') ->get_classes(); diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 3300fc09c5..e94e233b22 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -239,6 +239,7 @@ class phpbb_functional_test_case extends phpbb_test_case $config = new \phpbb\config\config(array('version' => PHPBB_VERSION)); $db = $this->get_db(); $factory = new \phpbb\db\tools\factory(); + $finder_factory = new \phpbb\finder\factory(null, false, $phpbb_root_path, $phpEx); $db_tools = $factory->get($db); $container = new phpbb_mock_container_builder(); @@ -262,9 +263,9 @@ class phpbb_functional_test_case extends phpbb_test_case $container, $db, $config, + $finder_factory, self::$config['table_prefix'] . 'ext', __DIR__ . '/', - $phpEx, new \phpbb\cache\service($this->get_cache_driver(), $config, $this->db, $phpbb_root_path, $phpEx) );