diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-10 05:16:45 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-10 05:16:45 +0300 |
commit | f6fd643844fbcdfd5986a909f1ac2f26e1a7db4f (patch) | |
tree | f4aad249ec2379c3611f8f6fd72ec02b33039fa1 /core/Container | |
parent | 76a5fe098e5a939a2709867133d74b9441c9a4d9 (diff) |
#6622 Logger refactoring: moved the container creation into a new ContainerFactory
This class can be used in tests to create a specific environment (e.g. the prod environment, CLI environment, etc...)
Diffstat (limited to 'core/Container')
-rw-r--r-- | core/Container/ContainerFactory.php | 79 | ||||
-rw-r--r-- | core/Container/StaticContainer.php | 47 |
2 files changed, 91 insertions, 35 deletions
diff --git a/core/Container/ContainerFactory.php b/core/Container/ContainerFactory.php new file mode 100644 index 0000000000..f0414452b7 --- /dev/null +++ b/core/Container/ContainerFactory.php @@ -0,0 +1,79 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Container; + +use DI\Container; +use DI\ContainerBuilder; +use Doctrine\Common\Cache\ArrayCache; +use Piwik\Config; + +/** + * Creates a configured DI container. + */ +class ContainerFactory +{ + /** + * Optional environment config to load. + * + * @var bool + */ + private $environment; + + /** + * @param string|null $environment Optional environment config to load. + */ + public function __construct($environment = null) + { + $this->environment = $environment; + } + + /** + * @link http://php-di.org/doc/container-configuration.html + * @throws \Exception + * @return Container + */ + public function create() + { + if (!class_exists('DI\ContainerBuilder')) { + throw new \Exception('DI\ContainerBuilder could not be found, maybe you are using Piwik from git and need to update Composer: php composer.phar update'); + } + + $builder = new ContainerBuilder(); + + $builder->useAnnotations(false); + $builder->setDefinitionCache(new ArrayCache()); + + // INI config + $builder->addDefinitions(new IniConfigDefinitionSource(Config::getInstance())); + + // Global config + $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php'); + + // User config + if (file_exists(PIWIK_USER_PATH . '/config/config.php')) { + $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php'); + } + + // Environment config + $this->addEnvironmentConfig($builder); + + return $builder->build(); + } + + private function addEnvironmentConfig(ContainerBuilder $builder) + { + if (!$this->environment) { + return; + } + + $file = sprintf('%s/config/environment/%s.php', PIWIK_USER_PATH, $this->environment); + + $builder->addDefinitions($file); + } +} diff --git a/core/Container/StaticContainer.php b/core/Container/StaticContainer.php index d45ed4f6fb..6d0974cf1b 100644 --- a/core/Container/StaticContainer.php +++ b/core/Container/StaticContainer.php @@ -9,9 +9,6 @@ namespace Piwik\Container; use DI\Container; -use DI\ContainerBuilder; -use Doctrine\Common\Cache\ArrayCache; -use Piwik\Config; /** * This class provides a static access to the container. @@ -52,42 +49,22 @@ class StaticContainer } /** + * Only use this in tests. + * + * @param Container $container + */ + public static function set(Container $container) + { + self::$container = $container; + } + + /** * @link http://php-di.org/doc/container-configuration.html */ private static function createContainer() { - if (!class_exists('DI\ContainerBuilder')) { - throw new \Exception('DI\ContainerBuilder could not be found, maybe you are using Piwik from git and need to update Composer: php composer.phar update'); - } - - $builder = new ContainerBuilder(); - - $builder->useAnnotations(false); - - // TODO set a better cache - $builder->setDefinitionCache(new ArrayCache()); - - // Old global INI config - $builder->addDefinitions(new IniConfigDefinitionSource(Config::getInstance())); - - // Global config - $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php'); - - // User config - if (file_exists(PIWIK_USER_PATH . '/config/config.php')) { - $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php'); - } - - // Environment config - if (self::$environment) { - $builder->addDefinitions(sprintf( - '%s/config/environment/%s.php', - PIWIK_USER_PATH, - self::$environment - )); - } - - return $builder->build(); + $containerFactory = new ContainerFactory(self::$environment); + return $containerFactory->create(); } public static function setEnvironment($environment) |