PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.7
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.7
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
Container.php 2 years ago ContainerDoesNotExistException.php 2 years ago ContainerFactory.php 2 years ago IniConfigDefinitionSource.php 2 years ago StaticContainer.php 2 years ago
ContainerFactory.php
149 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://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 // add plugin environment configs
99 $plugins = $this->pluginList->getActivatedPlugins();
100 if ($this->shouldSortPlugins()) {
101 $plugins = $this->sortPlugins($plugins);
102 }
103 foreach ($plugins as $plugin) {
104 $baseDir = Manager::getPluginDirectory($plugin);
105 $environmentFile = $baseDir . '/config/' . $environment . '.php';
106 if (file_exists($environmentFile)) {
107 $builder->addDefinitions($environmentFile);
108 }
109 }
110 }
111 private function addPluginConfigs(ContainerBuilder $builder)
112 {
113 $plugins = $this->pluginList->getActivatedPlugins();
114 if ($this->shouldSortPlugins()) {
115 $plugins = $this->sortPlugins($plugins);
116 }
117 foreach ($plugins as $plugin) {
118 $baseDir = Manager::getPluginDirectory($plugin);
119 $file = $baseDir . '/config/config.php';
120 if (file_exists($file)) {
121 $builder->addDefinitions($file);
122 }
123 }
124 }
125 /**
126 * This method is required for Matomo Cloud to allow for custom sorting of plugin order
127 *
128 * @return bool
129 */
130 private function shouldSortPlugins() : bool
131 {
132 return isset($GLOBALS['MATOMO_SORT_PLUGINS']) && is_callable($GLOBALS['MATOMO_SORT_PLUGINS']);
133 }
134 /**
135 * @param array $plugins
136 * @return array
137 */
138 private function sortPlugins(array $plugins)
139 {
140 return call_user_func($GLOBALS['MATOMO_SORT_PLUGINS'], $plugins);
141 }
142 private function isDevelopmentModeEnabled() : bool
143 {
144 $section = $this->settings->getSection('Development');
145 return (bool) @$section['enabled'];
146 // TODO: code redundancy w/ Development. hopefully ok for now.
147 }
148 }
149