PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 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