diff options
-rw-r--r-- | core/Application/Environment.php | 57 | ||||
-rw-r--r-- | core/CliMulti/RequestCommand.php | 1 | ||||
-rw-r--r-- | core/Console.php | 9 | ||||
-rw-r--r-- | core/Container/StaticContainer.php | 33 | ||||
-rw-r--r-- | core/dispatch.php | 3 | ||||
-rw-r--r-- | misc/cron/archive.php | 4 | ||||
-rw-r--r-- | misc/others/cli-script-bootstrap.php | 4 | ||||
-rw-r--r-- | piwik.php | 3 | ||||
-rw-r--r-- | tests/PHPUnit/Framework/Fixture.php | 9 | ||||
-rw-r--r-- | tests/PHPUnit/bootstrap.php | 4 |
10 files changed, 95 insertions, 32 deletions
diff --git a/core/Application/Environment.php b/core/Application/Environment.php new file mode 100644 index 0000000000..75b650f95f --- /dev/null +++ b/core/Application/Environment.php @@ -0,0 +1,57 @@ +<?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\Application; + +use DI\Container; +use Piwik\Container\ContainerFactory; +use Piwik\Container\StaticContainer; + +/** + * TODO + */ +class Environment +{ + private $environment; + + /** + * @var Container + */ + private $container; + + public function __construct($environment) + { + $this->environment = $environment; + } + + public function init() + { + $this->container = $this->createContainer(); + + StaticContainer::set($this->container); + } + + public function getContainer() + { + return $this->container; + } + + public function get($id) + { + return $this->container->get($id); + } + + /** + * @link http://php-di.org/doc/container-configuration.html + */ + private function createContainer() + { + $containerFactory = new ContainerFactory($this->environment, StaticContainer::getDefinitons()); + return $containerFactory->create(); + } +}
\ No newline at end of file diff --git a/core/CliMulti/RequestCommand.php b/core/CliMulti/RequestCommand.php index dd91931a9c..272d9016db 100644 --- a/core/CliMulti/RequestCommand.php +++ b/core/CliMulti/RequestCommand.php @@ -97,7 +97,6 @@ class RequestCommand extends ConsoleCommand */ private function recreateContainerWithWebEnvironment() { - StaticContainer::setEnvironment(null); StaticContainer::clearContainer(); Log::unsetInstance(); } diff --git a/core/Console.php b/core/Console.php index cfed85a994..268a421708 100644 --- a/core/Console.php +++ b/core/Console.php @@ -8,6 +8,7 @@ */ namespace Piwik; +use Piwik\Application\Environment; use Piwik\Config\ConfigNotFoundException; use Piwik\Container\StaticContainer; use Piwik\Plugin\Manager as PluginManager; @@ -20,6 +21,11 @@ use Symfony\Component\Console\Output\OutputInterface; class Console extends Application { + /** + * @var Environment + */ + private $environment; + public function __construct() { $this->checkCompatibility(); @@ -34,7 +40,8 @@ class Console extends Application $this->getDefinition()->addOption($option); - StaticContainer::setEnvironment('cli'); + $this->environment = new Environment('cli'); + $this->environment->init(); } public function doRun(InputInterface $input, OutputInterface $output) diff --git a/core/Container/StaticContainer.php b/core/Container/StaticContainer.php index 8468dce2ae..b290d7d60b 100644 --- a/core/Container/StaticContainer.php +++ b/core/Container/StaticContainer.php @@ -25,13 +25,6 @@ class StaticContainer private static $container; /** - * Optional environment config to load. - * - * @var bool - */ - private static $environment; - - /** * Definitions to register in the container. * * @var array @@ -44,7 +37,7 @@ class StaticContainer public static function getContainer() { if (self::$container === null) { - self::$container = self::createContainer(); + throw new \Exception("The root container has not been created yet."); } return self::$container; @@ -65,25 +58,6 @@ class StaticContainer self::$container = $container; } - /** - * @link http://php-di.org/doc/container-configuration.html - */ - private static function createContainer() - { - $containerFactory = new ContainerFactory(self::$environment, self::$definitions); - return $containerFactory->create(); - } - - /** - * Set the application environment (cli, test, …) or null for the default one. - * - * @param string|null $environment - */ - public static function setEnvironment($environment) - { - self::$environment = $environment; - } - public static function addDefinitions(array $definitions) { self::$definitions = $definitions; @@ -100,4 +74,9 @@ class StaticContainer { return self::getContainer()->get($name); } + + public static function getDefinitons() + { + return self::$definitions; + } } diff --git a/core/dispatch.php b/core/dispatch.php index c63f2f76b4..fd05aac2b5 100644 --- a/core/dispatch.php +++ b/core/dispatch.php @@ -24,6 +24,9 @@ if (!defined('PIWIK_ENABLE_DISPATCH')) { } if (PIWIK_ENABLE_DISPATCH) { + $environment = new \Piwik\Application\Environment(null); + $environment->init(); + $controller = FrontController::getInstance(); try { diff --git a/misc/cron/archive.php b/misc/cron/archive.php index eecd78946b..2f0d97a523 100644 --- a/misc/cron/archive.php +++ b/misc/cron/archive.php @@ -64,7 +64,9 @@ if (isset($_SERVER['argv']) && Piwik\Console::isSupported()) { if (Piwik\Common::isPhpCliMode()) { // We can run the archive in CLI with `php-cgi` so we have to configure the container/logger // just like for CLI - StaticContainer::setEnvironment('cli'); + $container = new \Piwik\Application\Environment('cli'); + $container->init(); + /** @var ConsoleHandler $consoleLogHandler */ $consoleLogHandler = StaticContainer::get('Symfony\Bridge\Monolog\Handler\ConsoleHandler'); $consoleLogHandler->setOutput(new ConsoleOutput(OutputInterface::VERBOSITY_VERBOSE)); diff --git a/misc/others/cli-script-bootstrap.php b/misc/others/cli-script-bootstrap.php index afd3494834..b7d24f4958 100644 --- a/misc/others/cli-script-bootstrap.php +++ b/misc/others/cli-script-bootstrap.php @@ -28,7 +28,9 @@ $GLOBALS['PIWIK_TRACKER_DEBUG'] = false; define('PIWIK_ENABLE_DISPATCH', false); if (Piwik\Common::isPhpCliMode()) { - StaticContainer::setEnvironment('cli'); + $environment = new \Piwik\Application\Environment('cli'); // TODO: what is this script for? need to test it if it's still needed. + $environment->init(); + /** @var ConsoleHandler $consoleLogHandler */ $consoleLogHandler = StaticContainer::get('Symfony\Bridge\Monolog\Handler\ConsoleHandler'); $consoleLogHandler->setOutput(new ConsoleOutput()); @@ -48,6 +48,9 @@ require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Cache.php'; require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Request.php'; require_once PIWIK_INCLUDE_PATH . '/core/Cookie.php'; +$environment = new \Piwik\Application\Environment(null); +$environment->init(); + Tracker::loadTrackerEnvironment(); $tracker = new Tracker(); diff --git a/tests/PHPUnit/Framework/Fixture.php b/tests/PHPUnit/Framework/Fixture.php index 04e23dadb3..82276e856b 100644 --- a/tests/PHPUnit/Framework/Fixture.php +++ b/tests/PHPUnit/Framework/Fixture.php @@ -8,6 +8,7 @@ namespace Piwik\Tests\Framework; use Piwik\Access; +use Piwik\Application\Environment; use Piwik\Cache\Backend\File; use Piwik\Cache as PiwikCache; use Piwik\Common; @@ -98,6 +99,11 @@ class Fixture extends \PHPUnit_Framework_Assert public $testEnvironment = null; /** + * @var Environment + */ + public $piwikEnvironment; + + /** * @return string */ protected static function getPythonBinary() @@ -142,6 +148,9 @@ class Fixture extends \PHPUnit_Framework_Assert public function performSetUp($setupEnvironmentOnly = false) { + $this->piwikEnvironment = new Environment('test'); + $this->piwikEnvironment->init(); + try { if ($this->createConfig) { Config::setSingletonInstance(new TestConfig()); diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php index edaea8a9f4..3cb15b5c3b 100644 --- a/tests/PHPUnit/bootstrap.php +++ b/tests/PHPUnit/bootstrap.php @@ -43,7 +43,9 @@ if (getenv('PIWIK_USE_XHPROF') == 1) { } // setup container for tests -StaticContainer::setEnvironment('test'); +$rootTestEnvironment = new \Piwik\Application\Environment('test'); +$rootTestEnvironment->init(); +unset($rootTestEnvironment); // so it doesn't appear in $_GLOBALS and so PHPUnit won't try to serialize it. (TODO: this is a quick hack) // require test fixtures $fixturesToLoad = array( |