From c6fe6d49bc923bc7f3e83e99b9fc9aa0630ee3a6 Mon Sep 17 00:00:00 2001 From: Joshua Angnoe Date: Sat, 5 Oct 2019 04:13:41 +0700 Subject: [PATCH] [ticket/16178] Make container_builder use same cache directory as Container Both should be using the same core.cache_dir directory. PHPBB3-16178 --- phpBB/phpbb/di/container_builder.php | 21 +++- tests/di/container_cache_directory_test.php | 125 ++++++++++++++++++++ 2 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 tests/di/container_cache_directory_test.php diff --git a/phpBB/phpbb/di/container_builder.php b/phpBB/phpbb/di/container_builder.php index 1884e41f15..79b00be19a 100644 --- a/phpBB/phpbb/di/container_builder.php +++ b/phpBB/phpbb/di/container_builder.php @@ -116,6 +116,11 @@ class container_builder /** @var \Exception */ private $build_exception; + /** + * @var array + */ + private $env_parameters = []; + /** * Constructor * @@ -124,8 +129,14 @@ class container_builder */ public function __construct($phpbb_root_path, $php_ext) { - $this->phpbb_root_path = $phpbb_root_path; - $this->php_ext = $php_ext; + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $php_ext; + $this->env_parameters = $this->get_env_parameters(); + + if (isset($this->env_parameters['core.cache_dir'])) + { + $this->with_cache_dir($this->env_parameters['core.cache_dir']); + } } /** @@ -581,14 +592,14 @@ class container_builder protected function get_core_parameters() { return array_merge( - array( + [ 'core.root_path' => $this->phpbb_root_path, 'core.php_ext' => $this->php_ext, 'core.environment' => $this->get_environment(), 'core.debug' => defined('DEBUG') ? DEBUG : false, 'core.cache_dir' => $this->get_cache_dir(), - ), - $this->get_env_parameters() + ], + $this->env_parameters ); } diff --git a/tests/di/container_cache_directory_test.php b/tests/di/container_cache_directory_test.php new file mode 100644 index 0000000000..02deefa802 --- /dev/null +++ b/tests/di/container_cache_directory_test.php @@ -0,0 +1,125 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace +{ + class container_cache_directory_test extends \phpbb_test_case //phpbb_di_container_test + { + protected $config_php; + + /** + * @var \phpbb\di\container_builder + */ + protected $builder; + protected $phpbb_root_path; + protected $filename; + + public function setUp(): void + { + $this->phpbb_root_path = dirname(__FILE__) . '/'; + $this->config_php = new \phpbb\config_php_file($this->phpbb_root_path . 'fixtures/', 'php'); + $this->builder = new phpbb_mock_phpbb_di_container_builder($this->phpbb_root_path . 'fixtures/', 'php'); + $this->builder->with_config($this->config_php); + + $this->filename = $this->phpbb_root_path . '../tmp/container.php'; + if (is_file($this->filename)) + { + unlink($this->filename); + } + + parent::setUp(); + } + + public function test_cache_directory_can_be_overridden() + { + $new_cache_directory = $this->phpbb_root_path . 'fixtures/overwrite-cache-directory/test/'; + + // This is how one overrides the cache directory. + // The file cache driver will now write to a new directory. + $_SERVER['PHPBB____core__cache_dir'] = $new_cache_directory; + + $container = $this->builder->get_container(); + + $this->assertEquals($container->getParameter('core.cache_dir'), $new_cache_directory); + } + + /** + * By default autoload_xxx.php and container_xxx.php files + * will also be written to the default cache directory. + * This test demonstrates the default behavior. + */ + public function test_container_and_autoload_cache() + { + $default_cache_directory = $this->phpbb_root_path . 'fixtures/cache/test'; + + // Make sure our test directory will be empty. + if (is_dir($default_cache_directory)) + { + array_map('unlink', glob($default_cache_directory . '/*')); + } + else + { + mkdir($default_cache_directory, 0777, true); + } + + // Use the normal container_builder + $builder = new \phpbb\di\container_builder($this->phpbb_root_path . 'fixtures/', 'php'); + $builder->with_config($this->config_php); + + $container = $builder->get_container(); + + $files_written_to_cache = array_map('basename', glob($default_cache_directory . '/*')); + + $this->assertNotEmpty(preg_grep('/autoload_.+.php/', $files_written_to_cache), 'There should be an autoload file in the cache directory.'); + $this->assertNotEmpty(preg_grep('/container_.+.php/', $files_written_to_cache), 'There should be an container file in the cache directory.'); + + // Cleanup the cache directory to prevent class redeclaration errors. + array_map('unlink', glob($default_cache_directory . '/*')); + } + + /** + * The desired behavior: When we have a custom cache directory + * the autoload and container cache files are also written to the custom cache directory. + */ + public function test_autoload_and_container_cache_are_written_to_overriden_cache_directory() + { + $new_cache_directory = $this->phpbb_root_path . 'fixtures/overwrite-cache-directory/test/'; + + $_SERVER['PHPBB____core__cache_dir'] = $new_cache_directory; + + // Make sure our test directory will be empty. + if (is_dir($new_cache_directory)) + { + array_map('unlink', glob($new_cache_directory . '/*')); + } + else + { + mkdir($new_cache_directory, 0777, true); + } + + // Use the normal container_builder + $builder = new \phpbb\di\container_builder($this->phpbb_root_path . 'fixtures/', 'php'); + $builder->with_config($this->config_php); + + $container = $builder->get_container(); + + $files_written_to_cache = array_map('basename', glob($new_cache_directory."/*")); + + $this->assertNotEmpty(preg_grep('/autoload_.+.php/', $files_written_to_cache), 'There should be an autoload file in the cache directory.'); + $this->assertNotEmpty(preg_grep('/container_.+.php/', $files_written_to_cache), 'There should be an container file in the cache directory.'); + + array_map('unlink', glob($new_cache_directory . '/*')); + + } + } +}