diff --git a/core/Container/ContainerFactory.php b/core/Container/ContainerFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..f0414452b7e9e01b056fa7629b1d6ec043c202d1 --- /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 d45ed4f6fbb0db0bdefc0211830f139abcb7c715..6d0974cf1b3af67c59fa7da39fdef7f373a1df71 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. @@ -51,43 +48,23 @@ class StaticContainer self::$container = null; } + /** + * 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) diff --git a/tests/PHPUnit/Integration/LogTest.php b/tests/PHPUnit/Integration/LogTest.php index 05f54ad1a1a9effdb0d333e20bc213ccb3443ef6..c5ff9c6f9e55988b9749e807e4f77e9a94c8185a 100644 --- a/tests/PHPUnit/Integration/LogTest.php +++ b/tests/PHPUnit/Integration/LogTest.php @@ -11,6 +11,7 @@ namespace Piwik\Tests\Integration; use Exception; use Piwik\Common; use Piwik\Config; +use Piwik\Container\ContainerFactory; use Piwik\Container\StaticContainer; use Piwik\Db; use Piwik\Error; @@ -35,12 +36,12 @@ class LogTest extends IntegrationTestCase 'screen' => '<div style=\'word-wrap: break-word; border: 3px solid red; padding:4px; width:70%; background-color:#FFFF96;\'> <strong>There is an error. Please report the message and full backtrace in the <a href=\'?module=Proxy&action=redirect&url=http://forum.piwik.org\' target=\'_blank\'>Piwik forums</a> (please do a Search first as it might have been reported already!).</strong><br /><br/> - <em>dummy error message</em> in <strong>LogTest.php</strong> on line <strong>170</strong> + <em>dummy error message</em> in <strong>LogTest.php</strong> on line <strong>174</strong> <br /><br />Backtrace --><div style="font-family:Courier;font-size:10pt"><br /> dummy backtrace</div></div>', - 'file' => '[Piwik\Tests\Integration\LogTest] LogTest.php(170): dummy error message + 'file' => '[Piwik\Tests\Integration\LogTest] LogTest.php(174): dummy error message dummy backtrace', - 'database' => '[Piwik\Tests\Integration\LogTest] LogTest.php(170): dummy error message + 'database' => '[Piwik\Tests\Integration\LogTest] LogTest.php(174): dummy error message dummy backtrace' ); @@ -79,7 +80,10 @@ dummy backtrace' { parent::setUp(); - StaticContainer::reset(); + // Create the container in the normal environment (because in tests logging is disabled) + $containerFactory = new ContainerFactory(); + $container = $containerFactory->create(); + StaticContainer::set($container); Log::unsetInstance(); Config::getInstance()->log['string_message_format'] = self::STRING_MESSAGE_FORMAT;