Skip to content
Extraits de code Groupes Projets
Valider f6fd6438 rédigé par Matthieu Napoli's avatar Matthieu Napoli
Parcourir les fichiers

#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...)
parent 76a5fe09
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
<?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);
}
}
......@@ -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)
......
......@@ -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 --&gt;<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;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter