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