diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-12 06:20:08 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-12 06:33:27 +0300 |
commit | c831518f45f2e75dce59639e7a2a10a90a830c91 (patch) | |
tree | f64a27c4fadf42feb4e1741f332bbbcfa56bbbb5 /core/Container | |
parent | c6a5b9c7e818f6e3d4275a65daae71a80755ad39 (diff) |
Import the old INI configuration in the DI config
Diffstat (limited to 'core/Container')
-rw-r--r-- | core/Container/IniConfigDefinitionSource.php | 114 | ||||
-rw-r--r-- | core/Container/StaticContainer.php | 7 |
2 files changed, 119 insertions, 2 deletions
diff --git a/core/Container/IniConfigDefinitionSource.php b/core/Container/IniConfigDefinitionSource.php new file mode 100644 index 0000000000..b84d33d94f --- /dev/null +++ b/core/Container/IniConfigDefinitionSource.php @@ -0,0 +1,114 @@ +<?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\Definition\Exception\DefinitionException; +use DI\Definition\MergeableDefinition; +use DI\Definition\Source\ChainableDefinitionSource; +use DI\Definition\Source\DefinitionSource; +use DI\Definition\ValueDefinition; +use Piwik\Config; + +/** + * Import the old INI config into PHP-DI. + */ +class IniConfigDefinitionSource implements DefinitionSource, ChainableDefinitionSource +{ + /** + * @var Config + */ + private $config; + + /** + * @var string + */ + private $prefix; + + /** + * @var DefinitionSource + */ + private $chainedSource; + + /** + * @param Config $config + * @param string $prefix Prefix for the container entries. + */ + public function __construct(Config $config, $prefix = 'old_config.') + { + $this->config = $config; + $this->prefix = $prefix; + } + + public function getDefinition($name, MergeableDefinition $parentDefinition = null) + { + // INI only contains values, so no definition merging here + if ($parentDefinition) { + return $this->notFound($name, $parentDefinition); + } + + if (strpos($name, $this->prefix) !== 0) { + return $this->notFound($name, $parentDefinition); + } + + list($sectionName, $configKey) = $this->parseEntryName($name); + + $section = $this->getSection($sectionName); + + if ($configKey === null) { + return new ValueDefinition($name, $section); + } + + if (! array_key_exists($configKey, $section)) { + return $this->notFound($name, $parentDefinition); + } + + return new ValueDefinition($name, $section[$configKey]); + } + + public function chain(DefinitionSource $source) + { + $this->chainedSource = $source; + } + + private function parseEntryName($name) + { + $parts = explode('.', $name, 3); + + array_shift($parts); + + if (! isset($parts[1])) { + $parts[1] = null; + } + + return $parts; + } + + private function getSection($sectionName) + { + $section = $this->config->$sectionName; + + if (!is_array($section)) { + throw new DefinitionException(sprintf( + 'Piwik\Config did not return an array for the config section %s', + $section + )); + } + + return $section; + } + + private function notFound($name, $parentDefinition) + { + if ($this->chainedSource) { + return $this->chainedSource->getDefinition($name, $parentDefinition); + } + + return null; + } +} diff --git a/core/Container/StaticContainer.php b/core/Container/StaticContainer.php index 2f950198ec..5948ec1c78 100644 --- a/core/Container/StaticContainer.php +++ b/core/Container/StaticContainer.php @@ -11,6 +11,7 @@ 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. @@ -48,9 +49,11 @@ class StaticContainer } $builder = new ContainerBuilder(); - // TODO add cache + // TODO set a better cache $builder->setDefinitionCache(new ArrayCache()); - // $builder->writeProxiesToFile(true, PIWIK_USER_PATH . '/tmp/proxies'); + + // Old global INI config + $builder->addDefinitions(new IniConfigDefinitionSource(Config::getInstance())); // Global config $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php'); |