PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.15.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.15.0
5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Container / ContainerFactory.php
matomo / app / core / Container Last commit date
ContainerDoesNotExistException.php 5 years ago ContainerFactory.php 4 years ago IniConfigDefinitionSource.php 5 years ago StaticContainer.php 5 years ago
ContainerFactory.php
179 lines
1 <?php
2 /**
3 * Matomo - 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 Piwik\Application\Kernel\GlobalSettingsProvider;
14 use Piwik\Application\Kernel\PluginList;
15 use Piwik\Plugin\Manager;
16
17 /**
18 * Creates a configured DI container.
19 */
20 class ContainerFactory
21 {
22 /**
23 * @var PluginList
24 */
25 private $pluginList;
26
27 /**
28 * @var GlobalSettingsProvider
29 */
30 private $settings;
31
32 /**
33 * Optional environment configs to load.
34 *
35 * @var string[]
36 */
37 private $environments;
38
39 /**
40 * @var array[]
41 */
42 private $definitions;
43
44 /**
45 * @param PluginList $pluginList
46 * @param GlobalSettingsProvider $settings
47 * @param string[] $environment Optional environment configs to load.
48 * @param array[] $definitions
49 */
50 public function __construct(PluginList $pluginList, GlobalSettingsProvider $settings, array $environments = array(), array $definitions = array())
51 {
52 $this->pluginList = $pluginList;
53 $this->settings = $settings;
54 $this->environments = $environments;
55 $this->definitions = $definitions;
56 }
57
58 /**
59 * @link http://php-di.org/doc/container-configuration.html
60 * @throws \Exception
61 * @return Container
62 */
63 public function create()
64 {
65 $builder = new ContainerBuilder();
66
67 $builder->useAnnotations(false);
68
69 // INI config
70 $builder->addDefinitions(new IniConfigDefinitionSource($this->settings));
71
72 // Global config
73 $builder->addDefinitions(PIWIK_DOCUMENT_ROOT . '/config/global.php');
74
75 // Plugin configs
76 $this->addPluginConfigs($builder);
77
78 // Development config
79 if ($this->isDevelopmentModeEnabled()) {
80 $this->addEnvironmentConfig($builder, 'dev');
81 }
82
83 // Environment config
84 foreach ($this->environments as $environment) {
85 $this->addEnvironmentConfig($builder, $environment);
86 }
87
88 // User config
89 if (file_exists(PIWIK_USER_PATH . '/config/config.php')
90 && !in_array('test', $this->environments, true)) {
91 $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php');
92 }
93
94 if (!empty($this->definitions)) {
95 foreach ($this->definitions as $definitionArray) {
96 $builder->addDefinitions($definitionArray);
97 }
98 }
99
100 $container = $builder->build();
101 $container->set('Piwik\Application\Kernel\PluginList', $this->pluginList);
102 $container->set('Piwik\Application\Kernel\GlobalSettingsProvider', $this->settings);
103
104 return $container;
105 }
106
107 private function addEnvironmentConfig(ContainerBuilder $builder, $environment)
108 {
109 if (!$environment) {
110 return;
111 }
112
113 $file = sprintf('%s/config/environment/%s.php', PIWIK_USER_PATH, $environment);
114
115 if (file_exists($file)) {
116 $builder->addDefinitions($file);
117 }
118
119 // add plugin environment configs
120 $plugins = $this->pluginList->getActivatedPlugins();
121
122 if ($this->shouldSortPlugins()) {
123 $plugins = $this->sortPlugins($plugins);
124 }
125
126 foreach ($plugins as $plugin) {
127 $baseDir = Manager::getPluginDirectory($plugin);
128
129 $environmentFile = $baseDir . '/config/' . $environment . '.php';
130 if (file_exists($environmentFile)) {
131 $builder->addDefinitions($environmentFile);
132 }
133 }
134 }
135
136 private function addPluginConfigs(ContainerBuilder $builder)
137 {
138 $plugins = $this->pluginList->getActivatedPlugins();
139
140 if ($this->shouldSortPlugins()) {
141 $plugins = $this->sortPlugins($plugins);
142 }
143
144 foreach ($plugins as $plugin) {
145 $baseDir = Manager::getPluginDirectory($plugin);
146
147 $file = $baseDir . '/config/config.php';
148 if (file_exists($file)) {
149 $builder->addDefinitions($file);
150 }
151 }
152 }
153
154 /**
155 * This method is required for Matomo Cloud to allow for custom sorting of plugin order
156 *
157 * @return bool
158 */
159 private function shouldSortPlugins()
160 {
161 return isset($GLOBALS['MATOMO_SORT_PLUGINS']) && is_callable($GLOBALS['MATOMO_SORT_PLUGINS']);
162 }
163
164 /**
165 * @param array $plugins
166 * @return array
167 */
168 private function sortPlugins(array $plugins)
169 {
170 return call_user_func($GLOBALS['MATOMO_SORT_PLUGINS'], $plugins);
171 }
172
173 private function isDevelopmentModeEnabled()
174 {
175 $section = $this->settings->getSection('Development');
176 return (bool) @$section['enabled']; // TODO: code redundancy w/ Development. hopefully ok for now.
177 }
178 }
179