PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.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
Container.php 1 year ago ContainerDoesNotExistException.php 2 years ago ContainerFactory.php 1 year ago IniConfigDefinitionSource.php 2 years ago StaticContainer.php 2 years ago
ContainerFactory.php
160 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 PluginList $pluginList
40 * @param GlobalSettingsProvider $settings
41 * @param string[] $environments Optional environment configs to load.
42 * @param array[] $definitions
43 */
44 public function __construct(PluginList $pluginList, GlobalSettingsProvider $settings, array $environments = array(), array $definitions = array())
45 {
46 $this->pluginList = $pluginList;
47 $this->settings = $settings;
48 $this->environments = $environments;
49 $this->definitions = $definitions;
50 }
51 /**
52 * @link http://php-di.org/doc/container-configuration.html
53 * @throws \Exception
54 * @return Container
55 */
56 public function create() : \Piwik\Container\Container
57 {
58 $builder = new ContainerBuilder(\Piwik\Container\Container::class);
59 $builder->useAnnotations(\false);
60 // INI config
61 $builder->addDefinitions(new \Piwik\Container\IniConfigDefinitionSource($this->settings));
62 // Global config
63 $builder->addDefinitions(PIWIK_DOCUMENT_ROOT . '/config/global.php');
64 // Plugin configs
65 $this->addPluginConfigs($builder);
66 // Development config
67 if ($this->isDevelopmentModeEnabled()) {
68 $this->addEnvironmentConfig($builder, 'dev');
69 }
70 // Environment config
71 foreach ($this->environments as $environment) {
72 $this->addEnvironmentConfig($builder, $environment);
73 }
74 // User config
75 if (file_exists(PIWIK_USER_PATH . '/config/config.php') && !in_array('test', $this->environments, \true)) {
76 $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php');
77 }
78 if (!empty($this->definitions)) {
79 foreach ($this->definitions as $definitionArray) {
80 $builder->addDefinitions($definitionArray);
81 }
82 }
83 /** @var Container $container */
84 $container = $builder->build();
85 $container->set('Piwik\\Application\\Kernel\\PluginList', $this->pluginList);
86 $container->set('Piwik\\Application\\Kernel\\GlobalSettingsProvider', $this->settings);
87 return $container;
88 }
89 private function addEnvironmentConfig(ContainerBuilder $builder, $environment)
90 {
91 if (!$environment) {
92 return;
93 }
94 $file = sprintf('%s/config/environment/%s.php', PIWIK_USER_PATH, $environment);
95 if (file_exists($file)) {
96 $builder->addDefinitions($file);
97 }
98 $file = sprintf('%s/config/environment/%s-%s.php', PIWIK_USER_PATH, $environment, strtolower($this->getDatabaseSchema()));
99 if (file_exists($file)) {
100 $builder->addDefinitions($file);
101 }
102 // add plugin environment configs
103 $plugins = $this->pluginList->getActivatedPlugins();
104 $plugins = array_unique(array_merge($plugins, Manager::getAlwaysActivatedPlugins()));
105 if ($this->shouldSortPlugins()) {
106 $plugins = $this->sortPlugins($plugins);
107 }
108 foreach ($plugins as $plugin) {
109 $baseDir = Manager::getPluginDirectory($plugin);
110 $environmentFile = $baseDir . '/config/' . $environment . '.php';
111 if (file_exists($environmentFile)) {
112 $builder->addDefinitions($environmentFile);
113 }
114 }
115 }
116 private function addPluginConfigs(ContainerBuilder $builder)
117 {
118 $plugins = $this->pluginList->getActivatedPlugins();
119 $plugins = array_unique(array_merge($plugins, Manager::getAlwaysActivatedPlugins()));
120 if ($this->shouldSortPlugins()) {
121 $plugins = $this->sortPlugins($plugins);
122 }
123 foreach ($plugins as $plugin) {
124 $baseDir = Manager::getPluginDirectory($plugin);
125 $file = $baseDir . '/config/config.php';
126 if (file_exists($file)) {
127 $builder->addDefinitions($file);
128 }
129 }
130 }
131 /**
132 * This method is required for Matomo Cloud to allow for custom sorting of plugin order
133 *
134 * @return bool
135 */
136 private function shouldSortPlugins() : bool
137 {
138 return isset($GLOBALS['MATOMO_SORT_PLUGINS']) && is_callable($GLOBALS['MATOMO_SORT_PLUGINS']);
139 }
140 /**
141 * @param array $plugins
142 * @return array
143 */
144 private function sortPlugins(array $plugins)
145 {
146 return call_user_func($GLOBALS['MATOMO_SORT_PLUGINS'], $plugins);
147 }
148 private function isDevelopmentModeEnabled() : bool
149 {
150 $section = $this->settings->getSection('Development');
151 return (bool) @$section['enabled'];
152 // TODO: code redundancy w/ Development. hopefully ok for now.
153 }
154 private function getDatabaseSchema() : string
155 {
156 $section = $this->settings->getSection('database');
157 return $section['schema'] ?? 'Mysql';
158 }
159 }
160