diff options
Diffstat (limited to 'phpBB/phpbb/di')
| -rw-r--r-- | phpBB/phpbb/di/container_builder.php | 157 | ||||
| -rw-r--r-- | phpBB/phpbb/di/extension/container_configuration.php | 44 | ||||
| -rw-r--r-- | phpBB/phpbb/di/extension/core.php | 81 | ||||
| -rw-r--r-- | phpBB/phpbb/di/extension/ext.php | 67 | 
4 files changed, 231 insertions, 118 deletions
diff --git a/phpBB/phpbb/di/container_builder.php b/phpBB/phpbb/di/container_builder.php index 638c13e86d..125ae28e9b 100644 --- a/phpBB/phpbb/di/container_builder.php +++ b/phpBB/phpbb/di/container_builder.php @@ -13,16 +13,25 @@  namespace phpbb\di; +use Symfony\Component\Config\ConfigCache; +use Symfony\Component\Config\FileLocator;  use Symfony\Component\DependencyInjection\ContainerBuilder;  use Symfony\Component\DependencyInjection\Dumper\PhpDumper; -use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; +use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass; +use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;  class container_builder  { -	/** @var string phpBB Root Path */ +	/** +	 * @var string phpBB Root Path +	 */  	protected $phpbb_root_path; -	/** @var string php file extension  */ +	/** +	 * @var string php file extension +	 */  	protected $php_ext;  	/** @@ -112,6 +121,11 @@ class container_builder  	protected $config_php_file;  	/** +	 * @var string +	 */ +	protected $cache_dir; + +	/**  	* Constructor  	*  	* @param \phpbb\config_php_file $config_php_file @@ -133,23 +147,30 @@ class container_builder  	public function get_container()  	{  		$container_filename = $this->get_container_filename(); -		if (!defined('DEBUG_CONTAINER') && $this->dump_container && file_exists($container_filename)) +		$config_cache = new ConfigCache($container_filename, defined('DEBUG')); +		if ($this->dump_container && $config_cache->isFresh())  		{  			require($container_filename);  			$this->container = new \phpbb_cache_container();  		}  		else  		{ -			if ($this->config_path === null) -			{ -				$this->config_path = $this->phpbb_root_path . 'config'; -			} -			$container_extensions = array(new \phpbb\di\extension\core($this->config_path)); +			$container_extensions = array(new \phpbb\di\extension\core($this->get_config_path()));  			if ($this->use_extensions)  			{  				$installed_exts = $this->get_installed_extensions(); -				$container_extensions[] = new \phpbb\di\extension\ext($installed_exts); +				foreach ($installed_exts as $ext_name => $path) +				{ +					$extension_class = '\\' . str_replace('/', '\\', $ext_name) . '\\di\\extension'; + +					if (!class_exists($extension_class)) +					{ +						$extension_class = '\phpbb\extension\di\extension_base'; +					} + +					$container_extensions[] = new $extension_class($ext_name, $path); +				}  			}  			if ($this->inject_config) @@ -171,6 +192,9 @@ class container_builder  				}  			} +			$loader = new YamlFileLoader($this->container, new FileLocator(phpbb_realpath($this->get_config_path()))); +			$loader->load($this->container->getParameter('core.environment') . '/config.yml'); +  			$this->inject_custom_parameters();  			if ($this->compile_container) @@ -178,9 +202,9 @@ class container_builder  				$this->container->compile();  			} -			if ($this->dump_container && !defined('DEBUG_CONTAINER')) +			if ($this->dump_container)  			{ -				$this->dump_container($container_filename); +				$this->dump_container($config_cache);  			}  		} @@ -267,6 +291,16 @@ class container_builder  	}  	/** +	 * Returns the path to the container configuration (default: root_path/config) +	 * +	 * @return string +	 */ +	protected function get_config_path() +	{ +		return $this->config_path ?: $this->phpbb_root_path . 'config'; +	} + +	/**  	* Set custom parameters to inject into the container.  	*  	* @param array $custom_parameters @@ -277,11 +311,31 @@ class container_builder  	}  	/** +	 * Set the path to the cache directory. +	 * +	 * @param string $cache_dir Path to the cache directory +	 */ +	public function set_cache_dir($cache_dir) +	{ +		$this->cache_dir = $cache_dir; +	} + +	/** +	 * Returns the path to the cache directory (default: root_path/cache/environment). +	 * +	 * @return string Path to the cache directory. +	 */ +	protected function get_cache_dir() +	{ +		return $this->cache_dir ?: $this->phpbb_root_path . 'cache/' . $this->get_environment() . '/'; +	} + +	/**  	* Dump the container to the disk.  	* -	* @param string $container_filename The name of the file. +	* @param ConfigCache $cache The config cache  	*/ -	protected function dump_container($container_filename) +	protected function dump_container($cache)  	{  		$dumper = new PhpDumper($this->container);  		$cached_container_dump = $dumper->dump(array( @@ -289,7 +343,7 @@ class container_builder  			'base_class'    => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',  		)); -		file_put_contents($container_filename, $cached_container_dump); +		$cache->write($cached_container_dump, $this->container->getResources());  	}  	/** @@ -362,34 +416,73 @@ class container_builder  	*/  	protected function create_container(array $extensions)  	{ -		$container = new ContainerBuilder(); +		$container = new ContainerBuilder(new ParameterBag($this->get_core_parameters())); + +		$extensions_alias = array();  		foreach ($extensions as $extension)  		{  			$container->registerExtension($extension); -			$container->loadFromExtension($extension->getAlias()); +			$extensions_alias[] = $extension->getAlias(); +			//$container->loadFromExtension($extension->getAlias());  		} +		$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions_alias)); +  		return $container;  	}  	/** -	* Inject the customs parameters into the container -	*/ +	 * Inject the customs parameters into the container +	 */  	protected function inject_custom_parameters()  	{ -		if ($this->custom_parameters === null) +		if ($this->custom_parameters !== null)  		{ -			$this->custom_parameters = array( -				'core.root_path' => $this->phpbb_root_path, -				'core.php_ext' => $this->php_ext, -			); +			foreach ($this->custom_parameters as $key => $value) +			{ +				$this->container->setParameter($key, $value); +			}  		} +	} -		foreach ($this->custom_parameters as $key => $value) +	/** +	 * Returns the core parameters. +	 * +	 * @return array An array of core parameters +	 */ +	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'         => DEBUG, +			), +			$this->get_env_parameters() +		); +	} + +	/** +	 * Gets the environment parameters. +	 * +	 * Only the parameters starting with "PHPBB__" are considered. +	 * +	 * @return array An array of parameters +	 */ +	protected function get_env_parameters() +	{ +		$parameters = array(); +		foreach ($_SERVER as $key => $value)  		{ -			$this->container->setParameter($key, $value); +			if (0 === strpos($key, 'PHPBB__')) +			{ +				$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value; +			}  		} + +		return $parameters;  	}  	/** @@ -400,6 +493,16 @@ class container_builder  	protected function get_container_filename()  	{  		$filename = str_replace(array('/', '.'), array('slash', 'dot'), $this->phpbb_root_path); -		return $this->phpbb_root_path . 'cache/container_' . $filename . '.' . $this->php_ext; +		return $this->get_cache_dir() . 'container_' . $filename . '.' . $this->php_ext; +	} + +	/** +	 * Return the name of the current environment. +	 * +	 * @return string +	 */ +	protected function get_environment() +	{ +		return PHPBB_ENVIRONMENT;  	}  } diff --git a/phpBB/phpbb/di/extension/container_configuration.php b/phpBB/phpbb/di/extension/container_configuration.php new file mode 100644 index 0000000000..ee58ec2b74 --- /dev/null +++ b/phpBB/phpbb/di/extension/container_configuration.php @@ -0,0 +1,44 @@ +<?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\di\extension; + +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +class container_configuration implements ConfigurationInterface +{ + +	/** +	 * Generates the configuration tree builder. +	 * +	 * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder +	 */ +	public function getConfigTreeBuilder() +	{ +		$treeBuilder = new TreeBuilder(); +		$rootNode = $treeBuilder->root('core'); +		$rootNode +			->children() +				->booleanNode('require_dev_dependencies')->defaultValue(false)->end() +				->arrayNode('twig') +					->addDefaultsIfNotSet() +					->children() +						->booleanNode('enable_debug_extension')->defaultValue(false)->end() +					->end() +				->end() +			->end() +		; +		return $treeBuilder; +	} +} diff --git a/phpBB/phpbb/di/extension/core.php b/phpBB/phpbb/di/extension/core.php index ca4fa5c082..451efc8e35 100644 --- a/phpBB/phpbb/di/extension/core.php +++ b/phpBB/phpbb/di/extension/core.php @@ -13,10 +13,11 @@  namespace phpbb\di\extension; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\Config\Resource\FileResource;  use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\HttpKernel\DependencyInjection\Extension;  use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -use Symfony\Component\Config\FileLocator; +use Symfony\Component\HttpKernel\DependencyInjection\Extension;  /**  * Container core extension @@ -24,42 +25,74 @@ use Symfony\Component\Config\FileLocator;  class core extends Extension  {  	/** -	* Config path -	* @var string -	*/ +	 * Config path +	 * @var string +	 */  	protected $config_path;  	/** -	* Constructor -	* -	* @param string $config_path Config path -	*/ +	 * Constructor +	 * +	 * @param string $config_path Config path +	 */  	public function __construct($config_path)  	{  		$this->config_path = $config_path;  	}  	/** -	* Loads a specific configuration. -	* -	* @param array            $config    An array of configuration values -	* @param ContainerBuilder $container A ContainerBuilder instance -	* -	* @throws \InvalidArgumentException When provided tag is not defined in this extension -	*/ -	public function load(array $config, ContainerBuilder $container) +	 * Loads a specific configuration. +	 * +	 * @param array            $configs   An array of configuration values +	 * @param ContainerBuilder $container A ContainerBuilder instance +	 * +	 * @throws \InvalidArgumentException When provided tag is not defined in this extension +	 */ +	public function load(array $configs, ContainerBuilder $container)  	{  		$loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($this->config_path))); -		$loader->load('services.yml'); +		$loader->load($container->getParameter('core.environment') . '/container/environment.yml'); + +		$config = $this->getConfiguration($configs, $container); +		$config = $this->processConfiguration($config, $configs); + +		if ($config['require_dev_dependencies']) +		{ +			if (!class_exists('Goutte\Client', true)) +			{ +				trigger_error( +					'Composer development dependencies have not been set up for the ' . $container->getParameter('core.environment') . ' environment yet, run ' . +					"'php ../composer.phar install --dev' from the phpBB directory to do so.", +					E_USER_ERROR +				); +			} +		} + +		if ($config['twig']['enable_debug_extension']) +		{ +			$definition = $container->getDefinition('template.twig.extensions.debug'); +			$definition->addTag('twig.extension'); +		} +	} + +	/** +	 * {@inheritdoc} +	 */ +	public function getConfiguration(array $config, ContainerBuilder $container) +	{ +		$r = new \ReflectionClass('\phpbb\di\extension\container_configuration'); +		$container->addResource(new FileResource($r->getFileName())); + +		return new container_configuration();  	}  	/** -	* Returns the recommended alias to use in XML. -	* -	* This alias is also the mandatory prefix to use when using YAML. -	* -	* @return string The alias -	*/ +	 * Returns the recommended alias to use in XML. +	 * +	 * This alias is also the mandatory prefix to use when using YAML. +	 * +	 * @return string The alias +	 */  	public function getAlias()  	{  		return 'core'; diff --git a/phpBB/phpbb/di/extension/ext.php b/phpBB/phpbb/di/extension/ext.php deleted file mode 100644 index 718c992d2e..0000000000 --- a/phpBB/phpbb/di/extension/ext.php +++ /dev/null @@ -1,67 +0,0 @@ -<?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\di\extension; - -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -use Symfony\Component\Config\FileLocator; - -/** -* Container ext extension -*/ -class ext extends Extension -{ -	protected $paths = array(); - -	public function __construct($enabled_extensions) -	{ -		foreach ($enabled_extensions as $ext => $path) -		{ -			$this->paths[] = $path; -		} -	} - -	/** -	* Loads a specific configuration. -	* -	* @param array            $config    An array of configuration values -	* @param ContainerBuilder $container A ContainerBuilder instance -	* -	* @throws \InvalidArgumentException When provided tag is not defined in this extension -	*/ -	public function load(array $config, ContainerBuilder $container) -	{ -		foreach ($this->paths as $path) -		{ -			if (file_exists($path . '/config/services.yml')) -			{ -				$loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($path . '/config'))); -				$loader->load('services.yml'); -			} -		} -	} - -	/** -	* Returns the recommended alias to use in XML. -	* -	* This alias is also the mandatory prefix to use when using YAML. -	* -	* @return string The alias -	*/ -	public function getAlias() -	{ -		return 'ext'; -	} -}  | 
