[ticket/12631] Add finder.not_use_cache

PHPBB3-12631
This commit is contained in:
Rubén Calvo 2018-09-01 01:32:22 +02:00 committed by Marc Alexander
parent 38024d14bf
commit 9308764fae
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
22 changed files with 136 additions and 42 deletions

View file

@ -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 }

View file

@ -5,9 +5,9 @@ services:
- '@service_container'
- '@dbal.conn'
- '@config'
- '@finder.factory'
- '%tables.ext%'
- '%core.root_path%'
- '%core.php_ext%'
- '@cache'
ext.composer.installer:

View file

@ -0,0 +1,8 @@
services:
finder.factory:
class: phpbb\finder\factory
arguments:
- '@cache'
- '%finder.not_use_cache%'
- '%core.root_path%'
- '%core.php_ext%'

View file

@ -25,3 +25,6 @@ core:
session:
log_errors: true
finder:
not_use_cache: true

View file

@ -8,6 +8,7 @@ services:
- '%core.root_path%'
- '%core.php_ext%'
- '%tables%'
- '%finder.not_use_cache%'
tags:
- { name: install_database_install, order: 1 }

View file

@ -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();

View file

@ -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()

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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;
}
}

View file

@ -0,0 +1,53 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @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);
}
}

View file

@ -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];
}

View file

@ -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);

View file

@ -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
);
}

View file

@ -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()));

View file

@ -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
);
}

View file

@ -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
);

View file

@ -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);
}
/**

View file

@ -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);
}
}

View file

@ -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))
{

View file

@ -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();

View file

@ -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)
);