-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Pimcore.php
342 lines (299 loc) · 7.6 KB
/
Pimcore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
use Pimcore\Cache;
use Pimcore\File;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class Pimcore
{
/**
* @var bool|null
*/
public static $adminMode;
/**
* @var bool|null
*/
protected static $debugMode;
/**
* @var bool|null
*/
protected static $devMode;
/**
* @var bool
*/
private static $inShutdown = false;
/**
* @var bool
*/
private static $shutdownEnabled = true;
/**
* @var KernelInterface
*/
private static $kernel;
/**
* @var \Composer\Autoload\ClassLoader
*/
private static $autoloader;
/**
* @return bool
*/
public static function inDebugMode(): bool
{
return (bool) self::$debugMode;
}
/**
* @internal
*
* @return bool|null
*/
public static function getDebugMode(): ?bool
{
return self::$debugMode;
}
/**
* @internal
*
* @param bool $debugMode
*/
public static function setDebugMode(bool $debugMode): void
{
self::$debugMode = $debugMode;
}
/**
* @return bool
*/
public static function inDevMode(): bool
{
return (bool) self::$devMode;
}
/**
* @internal
*
* @return bool|null
*/
public static function getDevMode(): ?bool
{
return self::$devMode;
}
/**
* @internal
*
* @param bool $devMode
*/
public static function setDevMode(bool $devMode): void
{
self::$devMode = $devMode;
}
/**
* switches pimcore into the admin mode - there you can access also unpublished elements, ....
*
* @static
*/
public static function setAdminMode()
{
self::$adminMode = true;
}
/**
* switches back to the non admin mode, where unpublished elements are invisible
*
* @static
*/
public static function unsetAdminMode()
{
self::$adminMode = false;
}
/**
* check if the process is currently in admin mode or not
*
* @static
*
* @return bool
*/
public static function inAdmin()
{
if (self::$adminMode !== null) {
return self::$adminMode;
}
return false;
}
/**
* @return bool
*/
public static function isInstalled()
{
try {
\Pimcore\Db::get()->fetchOne('SELECT VERSION()');
return true;
} catch (\Exception $e) {
return false;
}
}
/**
* @return EventDispatcherInterface
*/
public static function getEventDispatcher()
{
return self::getContainer()->get('event_dispatcher');
}
/**
* @return KernelInterface
*/
public static function getKernel()
{
return static::$kernel;
}
/**
* @return bool
*/
public static function hasKernel()
{
if (static::$kernel) {
return true;
}
return false;
}
/**
* @param KernelInterface $kernel
*/
public static function setKernel(KernelInterface $kernel)
{
static::$kernel = $kernel;
}
/**
* Accessing the container this way is discouraged as dependencies should be wired through the container instead of
* needing to access the container directly. This exists mainly for compatibility with legacy code.
*
* @internal
*
* @return ContainerInterface
*/
public static function getContainer()
{
return static::getKernel()->getContainer();
}
/**
* @return bool
*/
public static function hasContainer()
{
if (static::hasKernel()) {
$container = static::getContainer();
if ($container) {
return true;
}
}
return false;
}
/**
* @return \Composer\Autoload\ClassLoader
*/
public static function getAutoloader(): \Composer\Autoload\ClassLoader
{
return self::$autoloader;
}
/**
* @param \Composer\Autoload\ClassLoader $autoloader
*/
public static function setAutoloader(\Composer\Autoload\ClassLoader $autoloader)
{
self::$autoloader = $autoloader;
}
/**
* Forces a garbage collection.
*
* @static
*
* @param array $keepItems
*/
public static function collectGarbage($keepItems = [])
{
$longRunningHelper = self::getContainer()->get(\Pimcore\Helper\LongRunningHelper::class);
$longRunningHelper->cleanUp([
'pimcoreRuntimeCache' => [
'keepItems' => $keepItems,
],
]);
}
/**
* this method is called with register_shutdown_function() and writes all data queued into the cache
*
* @static
*/
public static function shutdown()
{
// set inShutdown to true so that the output-buffer knows that he is allowed to send the headers
self::$inShutdown = true;
if (self::getContainer() === null) {
return;
}
if (self::$shutdownEnabled && self::isInstalled()) {
// write and clean up cache
Cache::shutdown();
}
}
/**
* @internal
*/
public static function disableShutdown()
{
self::$shutdownEnabled = false;
}
/**
* @internal
*/
public static function enableShutdown()
{
self::$shutdownEnabled = true;
}
public static function disableMinifyJs(): bool
{
if (self::inDevMode()) {
return true;
}
// magic parameter for debugging ExtJS stuff
if (array_key_exists('unminified_js', $_REQUEST) && self::inDebugMode()) {
return true;
}
return false;
}
public static function initLogger()
{
// special request log -> if parameter pimcore_log is set
if (array_key_exists('pimcore_log', $_REQUEST) && self::inDebugMode()) {
$requestLogName = date('Y-m-d_H-i-s');
if (!empty($_REQUEST['pimcore_log'])) {
// slashed are not allowed, replace them with hyphens
$requestLogName = str_replace('/', '-', $_REQUEST['pimcore_log']);
}
$requestLogFile = resolvePath(PIMCORE_LOG_DIRECTORY . '/request-' . $requestLogName . '.log');
if (strpos($requestLogFile, PIMCORE_LOG_DIRECTORY) !== 0) {
throw new \Exception('Not allowed');
}
if (!file_exists($requestLogFile)) {
File::put($requestLogFile, '');
}
$requestDebugHandler = new \Monolog\Handler\StreamHandler($requestLogFile);
foreach (self::getContainer()->getServiceIds() as $id) {
if (strpos($id, 'monolog.logger.') === 0) {
$logger = self::getContainer()->get($id);
if ($logger->getName() != 'event') {
// replace all handlers
$logger->setHandlers([$requestDebugHandler]);
}
}
}
}
}
}