ContainerDoesNotExistException.php
6 years ago
ContainerFactory.php
6 years ago
IniConfigDefinitionSource.php
6 years ago
StaticContainer.php
6 years ago
ContainerFactory.php
153 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik\Container; |
| 10 | |
| 11 | use DI\Container; |
| 12 | use DI\ContainerBuilder; |
| 13 | use Doctrine\Common\Cache\ArrayCache; |
| 14 | use Piwik\Application\Kernel\GlobalSettingsProvider; |
| 15 | use Piwik\Application\Kernel\PluginList; |
| 16 | use Piwik\Plugin\Manager; |
| 17 | |
| 18 | /** |
| 19 | * Creates a configured DI container. |
| 20 | */ |
| 21 | class ContainerFactory |
| 22 | { |
| 23 | /** |
| 24 | * @var PluginList |
| 25 | */ |
| 26 | private $pluginList; |
| 27 | |
| 28 | /** |
| 29 | * @var GlobalSettingsProvider |
| 30 | */ |
| 31 | private $settings; |
| 32 | |
| 33 | /** |
| 34 | * Optional environment configs to load. |
| 35 | * |
| 36 | * @var string[] |
| 37 | */ |
| 38 | private $environments; |
| 39 | |
| 40 | /** |
| 41 | * @var array[] |
| 42 | */ |
| 43 | private $definitions; |
| 44 | |
| 45 | /** |
| 46 | * @param PluginList $pluginList |
| 47 | * @param GlobalSettingsProvider $settings |
| 48 | * @param string[] $environment Optional environment configs to load. |
| 49 | * @param array[] $definitions |
| 50 | */ |
| 51 | public function __construct(PluginList $pluginList, GlobalSettingsProvider $settings, array $environments = array(), array $definitions = array()) |
| 52 | { |
| 53 | $this->pluginList = $pluginList; |
| 54 | $this->settings = $settings; |
| 55 | $this->environments = $environments; |
| 56 | $this->definitions = $definitions; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @link http://php-di.org/doc/container-configuration.html |
| 61 | * @throws \Exception |
| 62 | * @return Container |
| 63 | */ |
| 64 | public function create() |
| 65 | { |
| 66 | $builder = new ContainerBuilder(); |
| 67 | |
| 68 | $builder->useAnnotations(false); |
| 69 | $builder->setDefinitionCache(new ArrayCache()); |
| 70 | |
| 71 | // INI config |
| 72 | $builder->addDefinitions(new IniConfigDefinitionSource($this->settings)); |
| 73 | |
| 74 | // Global config |
| 75 | $builder->addDefinitions(PIWIK_DOCUMENT_ROOT . '/config/global.php'); |
| 76 | |
| 77 | // Plugin configs |
| 78 | $this->addPluginConfigs($builder); |
| 79 | |
| 80 | // Development config |
| 81 | if ($this->isDevelopmentModeEnabled()) { |
| 82 | $this->addEnvironmentConfig($builder, 'dev'); |
| 83 | } |
| 84 | |
| 85 | // Environment config |
| 86 | foreach ($this->environments as $environment) { |
| 87 | $this->addEnvironmentConfig($builder, $environment); |
| 88 | } |
| 89 | |
| 90 | // User config |
| 91 | if (file_exists(PIWIK_USER_PATH . '/config/config.php') |
| 92 | && !in_array('test', $this->environments, true)) { |
| 93 | $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php'); |
| 94 | } |
| 95 | |
| 96 | if (!empty($this->definitions)) { |
| 97 | foreach ($this->definitions as $definitionArray) { |
| 98 | $builder->addDefinitions($definitionArray); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | $container = $builder->build(); |
| 103 | $container->set('Piwik\Application\Kernel\PluginList', $this->pluginList); |
| 104 | $container->set('Piwik\Application\Kernel\GlobalSettingsProvider', $this->settings); |
| 105 | |
| 106 | return $container; |
| 107 | } |
| 108 | |
| 109 | private function addEnvironmentConfig(ContainerBuilder $builder, $environment) |
| 110 | { |
| 111 | if (!$environment) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $file = sprintf('%s/config/environment/%s.php', PIWIK_USER_PATH, $environment); |
| 116 | |
| 117 | if (file_exists($file)) { |
| 118 | $builder->addDefinitions($file); |
| 119 | } |
| 120 | |
| 121 | // add plugin environment configs |
| 122 | $plugins = $this->pluginList->getActivatedPlugins(); |
| 123 | foreach ($plugins as $plugin) { |
| 124 | $baseDir = Manager::getPluginDirectory($plugin); |
| 125 | |
| 126 | $environmentFile = $baseDir . '/config/' . $environment . '.php'; |
| 127 | if (file_exists($environmentFile)) { |
| 128 | $builder->addDefinitions($environmentFile); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private function addPluginConfigs(ContainerBuilder $builder) |
| 134 | { |
| 135 | $plugins = $this->pluginList->getActivatedPlugins(); |
| 136 | |
| 137 | foreach ($plugins as $plugin) { |
| 138 | $baseDir = Manager::getPluginDirectory($plugin); |
| 139 | |
| 140 | $file = $baseDir . '/config/config.php'; |
| 141 | if (file_exists($file)) { |
| 142 | $builder->addDefinitions($file); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private function isDevelopmentModeEnabled() |
| 148 | { |
| 149 | $section = $this->settings->getSection('Development'); |
| 150 | return (bool) @$section['enabled']; // TODO: code redundancy w/ Development. hopefully ok for now. |
| 151 | } |
| 152 | } |
| 153 |