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 / Application / Environment.php
matomo / app / core / Application Last commit date
Kernel 1 month ago Environment.php 3 months ago EnvironmentManipulator.php 3 months ago
Environment.php
220 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\Application;
10
11 use Piwik\Container\Container;
12 use Piwik\Application\Kernel\EnvironmentValidator;
13 use Piwik\Application\Kernel\GlobalSettingsProvider;
14 use Piwik\Application\Kernel\PluginList;
15 use Piwik\Container\ContainerFactory;
16 use Piwik\Container\StaticContainer;
17 use Piwik\Piwik;
18 /**
19 * Encapsulates Piwik environment setup and access.
20 *
21 * The Piwik environment consists of two main parts: the kernel and the DI container.
22 *
23 * The 'kernel' is the core part of Piwik that cannot be modified / extended through the DI container.
24 * It includes components that are required to create the DI container.
25 *
26 * Currently the only objects in the 'kernel' are a GlobalSettingsProvider object and a
27 * PluginList object. The GlobalSettingsProvider object is required for the current PluginList
28 * implementation and for checking whether Development mode is enabled. The PluginList is
29 * needed in order to determine what plugins are activated, since plugins can provide their
30 * own DI configuration.
31 *
32 * The DI container contains every other Piwik object, including the Plugin\Manager,
33 * plugin API instances, dependent services, etc. Plugins and users can override/extend
34 * the objects in this container.
35 *
36 * NOTE: DI support in Piwik is currently a work in process; not everything is currently
37 * stored in the DI container, but we are working towards this.
38 */
39 class Environment
40 {
41 /**
42 * @internal
43 * @var EnvironmentManipulator
44 */
45 private static $globalEnvironmentManipulator = null;
46 /**
47 * @var string
48 */
49 private $environment;
50 /**
51 * @var array
52 */
53 private $definitions;
54 /**
55 * @var Container
56 */
57 private $container;
58 /**
59 * @var GlobalSettingsProvider
60 */
61 private $globalSettingsProvider;
62 /**
63 * @var PluginList
64 */
65 private $pluginList;
66 /**
67 * @param string $environment
68 * @param array $definitions
69 */
70 public function __construct($environment, array $definitions = array())
71 {
72 $this->environment = $environment;
73 $this->definitions = $definitions;
74 }
75 public function getEnvironmentName()
76 {
77 return $this->environment;
78 }
79 /**
80 * Initializes the kernel globals and DI container.
81 */
82 public function init()
83 {
84 $this->invokeBeforeContainerCreatedHook();
85 $this->container = $this->createContainer();
86 $this->container->set(self::class, $this);
87 StaticContainer::push($this->container);
88 $this->validateEnvironment();
89 $this->invokeEnvironmentBootstrappedHook();
90 Piwik::postEvent('Environment.bootstrapped');
91 // this event should be removed eventually
92 }
93 /**
94 * Destroys an environment. MUST be called when embedding environments.
95 */
96 public function destroy()
97 {
98 StaticContainer::pop();
99 }
100 /**
101 * Returns the DI container. All Piwik objects for a specific Piwik instance should be stored
102 * in this container.
103 *
104 * @return Container
105 */
106 public function getContainer()
107 {
108 return $this->container;
109 }
110 /**
111 * @link https://php-di.org/doc/container-configuration.html
112 */
113 private function createContainer()
114 {
115 $pluginList = $this->getPluginListCached();
116 $settings = $this->getGlobalSettingsCached();
117 $extraDefinitions = $this->getExtraDefinitionsFromManipulators();
118 $definitions = array_merge(StaticContainer::getDefinitions(), $extraDefinitions, array($this->definitions));
119 $environments = array($this->environment);
120 $environments = array_merge($environments, $this->getExtraEnvironmentsFromManipulators());
121 $containerFactory = new ContainerFactory($pluginList, $settings, $environments, $definitions);
122 return $containerFactory->create();
123 }
124 protected function getGlobalSettingsCached()
125 {
126 if ($this->globalSettingsProvider === null) {
127 $original = $this->getGlobalSettings();
128 $globalSettingsProvider = $this->getGlobalSettingsProviderOverride($original);
129 $this->globalSettingsProvider = $globalSettingsProvider ?: $original;
130 }
131 return $this->globalSettingsProvider;
132 }
133 protected function getPluginListCached()
134 {
135 if ($this->pluginList === null) {
136 $pluginList = $this->getPluginListOverride();
137 $this->pluginList = $pluginList ?: $this->getPluginList();
138 }
139 return $this->pluginList;
140 }
141 /**
142 * Returns the kernel global GlobalSettingsProvider object. Derived classes can override this method
143 * to provide a different implementation.
144 *
145 * @return null|GlobalSettingsProvider
146 */
147 protected function getGlobalSettings()
148 {
149 return new GlobalSettingsProvider();
150 }
151 /**
152 * Returns the kernel global PluginList object. Derived classes can override this method to
153 * provide a different implementation.
154 *
155 * @return PluginList
156 */
157 protected function getPluginList()
158 {
159 // TODO: in tracker should only load tracker plugins. can't do properly until tracker entrypoint is encapsulated.
160 return new PluginList($this->getGlobalSettingsCached());
161 }
162 private function validateEnvironment()
163 {
164 /** @var EnvironmentValidator $validator */
165 $validator = $this->container->get('Piwik\\Application\\Kernel\\EnvironmentValidator');
166 $validator->validate();
167 }
168 /**
169 * @internal
170 */
171 public static function setGlobalEnvironmentManipulator(\Piwik\Application\EnvironmentManipulator $manipulator)
172 {
173 self::$globalEnvironmentManipulator = $manipulator;
174 }
175 private function getGlobalSettingsProviderOverride(GlobalSettingsProvider $original)
176 {
177 if (self::$globalEnvironmentManipulator) {
178 return self::$globalEnvironmentManipulator->makeGlobalSettingsProvider($original);
179 } else {
180 return null;
181 }
182 }
183 private function invokeBeforeContainerCreatedHook()
184 {
185 if (self::$globalEnvironmentManipulator) {
186 return self::$globalEnvironmentManipulator->beforeContainerCreated();
187 }
188 }
189 private function getExtraDefinitionsFromManipulators()
190 {
191 if (self::$globalEnvironmentManipulator) {
192 return self::$globalEnvironmentManipulator->getExtraDefinitions();
193 } else {
194 return array();
195 }
196 }
197 private function invokeEnvironmentBootstrappedHook()
198 {
199 if (self::$globalEnvironmentManipulator) {
200 self::$globalEnvironmentManipulator->onEnvironmentBootstrapped();
201 }
202 }
203 private function getExtraEnvironmentsFromManipulators()
204 {
205 if (self::$globalEnvironmentManipulator) {
206 return self::$globalEnvironmentManipulator->getExtraEnvironments();
207 } else {
208 return array();
209 }
210 }
211 private function getPluginListOverride()
212 {
213 if (self::$globalEnvironmentManipulator) {
214 return self::$globalEnvironmentManipulator->makePluginList($this->getGlobalSettingsCached());
215 } else {
216 return null;
217 }
218 }
219 }
220