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