| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
|
|
|
15 |
use Symfony\Component\Config\ConfigCache;
|
|
|
16 |
use Symfony\Component\Config\Loader\DelegatingLoader;
|
|
|
17 |
use Symfony\Component\Config\Loader\LoaderResolver;
|
|
|
18 |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
|
|
19 |
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
|
|
|
20 |
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
21 |
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
22 |
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
|
|
|
23 |
use Symfony\Component\DependencyInjection\Dumper\Preloader;
|
|
|
24 |
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
|
|
25 |
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
|
|
|
26 |
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
|
|
|
27 |
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
|
|
|
28 |
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
|
|
|
29 |
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
|
|
30 |
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
|
|
31 |
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
|
|
32 |
use Symfony\Component\ErrorHandler\DebugClassLoader;
|
|
|
33 |
use Symfony\Component\Filesystem\Filesystem;
|
|
|
34 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
35 |
use Symfony\Component\HttpFoundation\Response;
|
|
|
36 |
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
|
|
37 |
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
|
|
|
38 |
use Symfony\Component\HttpKernel\Config\FileLocator;
|
|
|
39 |
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
|
|
|
40 |
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
|
|
|
41 |
|
|
|
42 |
// Help opcache.preload discover always-needed symbols
|
|
|
43 |
class_exists(ConfigCache::class);
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* The Kernel is the heart of the Symfony system.
|
|
|
47 |
*
|
|
|
48 |
* It manages an environment made of bundles.
|
|
|
49 |
*
|
|
|
50 |
* Environment names must always start with a letter and
|
|
|
51 |
* they must only contain letters and numbers.
|
|
|
52 |
*
|
|
|
53 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
54 |
*/
|
|
|
55 |
abstract class Kernel implements KernelInterface, RebootableInterface, TerminableInterface
|
|
|
56 |
{
|
|
|
57 |
/**
|
|
|
58 |
* @var array<string, BundleInterface>
|
|
|
59 |
*/
|
|
|
60 |
protected $bundles = [];
|
|
|
61 |
|
|
|
62 |
protected $container;
|
|
|
63 |
protected $environment;
|
|
|
64 |
protected $debug;
|
|
|
65 |
protected $booted = false;
|
|
|
66 |
protected $startTime;
|
|
|
67 |
|
|
|
68 |
private string $projectDir;
|
|
|
69 |
private ?string $warmupDir = null;
|
|
|
70 |
private int $requestStackSize = 0;
|
|
|
71 |
private bool $resetServices = false;
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* @var array<string, bool>
|
|
|
75 |
*/
|
|
|
76 |
private static array $freshCache = [];
|
|
|
77 |
|
| 1663 |
lars |
78 |
public const VERSION = '6.2.10';
|
|
|
79 |
public const VERSION_ID = 60210;
|
| 148 |
lars |
80 |
public const MAJOR_VERSION = 6;
|
|
|
81 |
public const MINOR_VERSION = 2;
|
| 1663 |
lars |
82 |
public const RELEASE_VERSION = 10;
|
| 148 |
lars |
83 |
public const EXTRA_VERSION = '';
|
|
|
84 |
|
|
|
85 |
public const END_OF_MAINTENANCE = '07/2023';
|
|
|
86 |
public const END_OF_LIFE = '07/2023';
|
|
|
87 |
|
|
|
88 |
public function __construct(string $environment, bool $debug)
|
|
|
89 |
{
|
|
|
90 |
if (!$this->environment = $environment) {
|
|
|
91 |
throw new \InvalidArgumentException(sprintf('Invalid environment provided to "%s": the environment cannot be empty.', get_debug_type($this)));
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$this->debug = $debug;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public function __clone()
|
|
|
98 |
{
|
|
|
99 |
$this->booted = false;
|
|
|
100 |
$this->container = null;
|
|
|
101 |
$this->requestStackSize = 0;
|
|
|
102 |
$this->resetServices = false;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function boot()
|
|
|
106 |
{
|
|
|
107 |
if (true === $this->booted) {
|
|
|
108 |
if (!$this->requestStackSize && $this->resetServices) {
|
|
|
109 |
if ($this->container->has('services_resetter')) {
|
|
|
110 |
$this->container->get('services_resetter')->reset();
|
|
|
111 |
}
|
|
|
112 |
$this->resetServices = false;
|
|
|
113 |
if ($this->debug) {
|
|
|
114 |
$this->startTime = microtime(true);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
if (null === $this->container) {
|
|
|
122 |
$this->preBoot();
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
foreach ($this->getBundles() as $bundle) {
|
|
|
126 |
$bundle->setContainer($this->container);
|
|
|
127 |
$bundle->boot();
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$this->booted = true;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public function reboot(?string $warmupDir)
|
|
|
134 |
{
|
|
|
135 |
$this->shutdown();
|
|
|
136 |
$this->warmupDir = $warmupDir;
|
|
|
137 |
$this->boot();
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
public function terminate(Request $request, Response $response)
|
|
|
141 |
{
|
|
|
142 |
if (false === $this->booted) {
|
|
|
143 |
return;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
if ($this->getHttpKernel() instanceof TerminableInterface) {
|
|
|
147 |
$this->getHttpKernel()->terminate($request, $response);
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
public function shutdown()
|
|
|
152 |
{
|
|
|
153 |
if (false === $this->booted) {
|
|
|
154 |
return;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
$this->booted = false;
|
|
|
158 |
|
|
|
159 |
foreach ($this->getBundles() as $bundle) {
|
|
|
160 |
$bundle->shutdown();
|
|
|
161 |
$bundle->setContainer(null);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
$this->container = null;
|
|
|
165 |
$this->requestStackSize = 0;
|
|
|
166 |
$this->resetServices = false;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response
|
|
|
170 |
{
|
|
|
171 |
if (!$this->booted) {
|
|
|
172 |
$container = $this->container ?? $this->preBoot();
|
|
|
173 |
|
|
|
174 |
if ($container->has('http_cache')) {
|
|
|
175 |
return $container->get('http_cache')->handle($request, $type, $catch);
|
|
|
176 |
}
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
$this->boot();
|
|
|
180 |
++$this->requestStackSize;
|
|
|
181 |
$this->resetServices = true;
|
|
|
182 |
|
|
|
183 |
try {
|
|
|
184 |
return $this->getHttpKernel()->handle($request, $type, $catch);
|
|
|
185 |
} finally {
|
|
|
186 |
--$this->requestStackSize;
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Gets an HTTP kernel from the container.
|
|
|
192 |
*/
|
|
|
193 |
protected function getHttpKernel(): HttpKernelInterface
|
|
|
194 |
{
|
|
|
195 |
return $this->container->get('http_kernel');
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
public function getBundles(): array
|
|
|
199 |
{
|
|
|
200 |
return $this->bundles;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
public function getBundle(string $name): BundleInterface
|
|
|
204 |
{
|
|
|
205 |
if (!isset($this->bundles[$name])) {
|
|
|
206 |
throw new \InvalidArgumentException(sprintf('Bundle "%s" does not exist or it is not enabled. Maybe you forgot to add it in the "registerBundles()" method of your "%s.php" file?', $name, get_debug_type($this)));
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
return $this->bundles[$name];
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
public function locateResource(string $name): string
|
|
|
213 |
{
|
|
|
214 |
if ('@' !== $name[0]) {
|
|
|
215 |
throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
if (str_contains($name, '..')) {
|
|
|
219 |
throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$bundleName = substr($name, 1);
|
|
|
223 |
$path = '';
|
|
|
224 |
if (str_contains($bundleName, '/')) {
|
|
|
225 |
[$bundleName, $path] = explode('/', $bundleName, 2);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
$bundle = $this->getBundle($bundleName);
|
|
|
229 |
if (file_exists($file = $bundle->getPath().'/'.$path)) {
|
|
|
230 |
return $file;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name));
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
public function getEnvironment(): string
|
|
|
237 |
{
|
|
|
238 |
return $this->environment;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public function isDebug(): bool
|
|
|
242 |
{
|
|
|
243 |
return $this->debug;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Gets the application root dir (path of the project's composer file).
|
|
|
248 |
*/
|
|
|
249 |
public function getProjectDir(): string
|
|
|
250 |
{
|
|
|
251 |
if (!isset($this->projectDir)) {
|
|
|
252 |
$r = new \ReflectionObject($this);
|
|
|
253 |
|
|
|
254 |
if (!is_file($dir = $r->getFileName())) {
|
|
|
255 |
throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name));
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
$dir = $rootDir = \dirname($dir);
|
|
|
259 |
while (!is_file($dir.'/composer.json')) {
|
|
|
260 |
if ($dir === \dirname($dir)) {
|
|
|
261 |
return $this->projectDir = $rootDir;
|
|
|
262 |
}
|
|
|
263 |
$dir = \dirname($dir);
|
|
|
264 |
}
|
|
|
265 |
$this->projectDir = $dir;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
return $this->projectDir;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
public function getContainer(): ContainerInterface
|
|
|
272 |
{
|
|
|
273 |
if (!$this->container) {
|
|
|
274 |
throw new \LogicException('Cannot retrieve the container from a non-booted kernel.');
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
return $this->container;
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* @internal
|
|
|
282 |
*/
|
|
|
283 |
public function setAnnotatedClassCache(array $annotatedClasses)
|
|
|
284 |
{
|
|
|
285 |
file_put_contents(($this->warmupDir ?: $this->getBuildDir()).'/annotations.map', sprintf('<?php return %s;', var_export($annotatedClasses, true)));
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
public function getStartTime(): float
|
|
|
289 |
{
|
|
|
290 |
return $this->debug && null !== $this->startTime ? $this->startTime : -\INF;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
public function getCacheDir(): string
|
|
|
294 |
{
|
|
|
295 |
return $this->getProjectDir().'/var/cache/'.$this->environment;
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
public function getBuildDir(): string
|
|
|
299 |
{
|
|
|
300 |
// Returns $this->getCacheDir() for backward compatibility
|
|
|
301 |
return $this->getCacheDir();
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
public function getLogDir(): string
|
|
|
305 |
{
|
|
|
306 |
return $this->getProjectDir().'/var/log';
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
public function getCharset(): string
|
|
|
310 |
{
|
|
|
311 |
return 'UTF-8';
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
/**
|
|
|
315 |
* Gets the patterns defining the classes to parse and cache for annotations.
|
|
|
316 |
*/
|
|
|
317 |
public function getAnnotatedClassesToCompile(): array
|
|
|
318 |
{
|
|
|
319 |
return [];
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
/**
|
|
|
323 |
* Initializes bundles.
|
|
|
324 |
*
|
|
|
325 |
* @throws \LogicException if two bundles share a common name
|
|
|
326 |
*/
|
|
|
327 |
protected function initializeBundles()
|
|
|
328 |
{
|
|
|
329 |
// init bundles
|
|
|
330 |
$this->bundles = [];
|
|
|
331 |
foreach ($this->registerBundles() as $bundle) {
|
|
|
332 |
$name = $bundle->getName();
|
|
|
333 |
if (isset($this->bundles[$name])) {
|
|
|
334 |
throw new \LogicException(sprintf('Trying to register two bundles with the same name "%s".', $name));
|
|
|
335 |
}
|
|
|
336 |
$this->bundles[$name] = $bundle;
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
/**
|
|
|
341 |
* The extension point similar to the Bundle::build() method.
|
|
|
342 |
*
|
|
|
343 |
* Use this method to register compiler passes and manipulate the container during the building process.
|
|
|
344 |
*/
|
|
|
345 |
protected function build(ContainerBuilder $container)
|
|
|
346 |
{
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
/**
|
|
|
350 |
* Gets the container class.
|
|
|
351 |
*
|
|
|
352 |
* @throws \InvalidArgumentException If the generated classname is invalid
|
|
|
353 |
*/
|
|
|
354 |
protected function getContainerClass(): string
|
|
|
355 |
{
|
|
|
356 |
$class = static::class;
|
|
|
357 |
$class = str_contains($class, "@anonymous\0") ? get_parent_class($class).str_replace('.', '_', ContainerBuilder::hash($class)) : $class;
|
|
|
358 |
$class = str_replace('\\', '_', $class).ucfirst($this->environment).($this->debug ? 'Debug' : '').'Container';
|
|
|
359 |
|
|
|
360 |
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $class)) {
|
|
|
361 |
throw new \InvalidArgumentException(sprintf('The environment "%s" contains invalid characters, it can only contain characters allowed in PHP class names.', $this->environment));
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
return $class;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* Gets the container's base class.
|
|
|
369 |
*
|
|
|
370 |
* All names except Container must be fully qualified.
|
|
|
371 |
*/
|
|
|
372 |
protected function getContainerBaseClass(): string
|
|
|
373 |
{
|
|
|
374 |
return 'Container';
|
|
|
375 |
}
|
|
|
376 |
|
|
|
377 |
/**
|
|
|
378 |
* Initializes the service container.
|
|
|
379 |
*
|
|
|
380 |
* The built version of the service container is used when fresh, otherwise the
|
|
|
381 |
* container is built.
|
|
|
382 |
*/
|
|
|
383 |
protected function initializeContainer()
|
|
|
384 |
{
|
|
|
385 |
$class = $this->getContainerClass();
|
|
|
386 |
$buildDir = $this->warmupDir ?: $this->getBuildDir();
|
|
|
387 |
$cache = new ConfigCache($buildDir.'/'.$class.'.php', $this->debug);
|
|
|
388 |
$cachePath = $cache->getPath();
|
|
|
389 |
|
|
|
390 |
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
|
|
|
391 |
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
|
|
|
392 |
|
|
|
393 |
try {
|
|
|
394 |
if (is_file($cachePath) && \is_object($this->container = include $cachePath)
|
|
|
395 |
&& (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh()))
|
|
|
396 |
) {
|
|
|
397 |
self::$freshCache[$cachePath] = true;
|
|
|
398 |
$this->container->set('kernel', $this);
|
|
|
399 |
error_reporting($errorLevel);
|
|
|
400 |
|
|
|
401 |
return;
|
|
|
402 |
}
|
|
|
403 |
} catch (\Throwable $e) {
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
$oldContainer = \is_object($this->container) ? new \ReflectionClass($this->container) : $this->container = null;
|
|
|
407 |
|
|
|
408 |
try {
|
|
|
409 |
is_dir($buildDir) ?: mkdir($buildDir, 0777, true);
|
|
|
410 |
|
|
|
411 |
if ($lock = fopen($cachePath.'.lock', 'w')) {
|
|
|
412 |
if (!flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock) && !flock($lock, $wouldBlock ? \LOCK_SH : \LOCK_EX)) {
|
|
|
413 |
fclose($lock);
|
|
|
414 |
$lock = null;
|
|
|
415 |
} elseif (!is_file($cachePath) || !\is_object($this->container = include $cachePath)) {
|
|
|
416 |
$this->container = null;
|
|
|
417 |
} elseif (!$oldContainer || \get_class($this->container) !== $oldContainer->name) {
|
|
|
418 |
flock($lock, \LOCK_UN);
|
|
|
419 |
fclose($lock);
|
|
|
420 |
$this->container->set('kernel', $this);
|
|
|
421 |
|
|
|
422 |
return;
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
} catch (\Throwable $e) {
|
|
|
426 |
} finally {
|
|
|
427 |
error_reporting($errorLevel);
|
|
|
428 |
}
|
|
|
429 |
|
|
|
430 |
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
|
|
|
431 |
$collectedLogs = [];
|
|
|
432 |
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
|
|
|
433 |
if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type) {
|
|
|
434 |
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
if (isset($collectedLogs[$message])) {
|
|
|
438 |
++$collectedLogs[$message]['count'];
|
|
|
439 |
|
|
|
440 |
return null;
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 5);
|
|
|
444 |
// Clean the trace by removing first frames added by the error handler itself.
|
|
|
445 |
for ($i = 0; isset($backtrace[$i]); ++$i) {
|
|
|
446 |
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
|
|
|
447 |
$backtrace = \array_slice($backtrace, 1 + $i);
|
|
|
448 |
break;
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
for ($i = 0; isset($backtrace[$i]); ++$i) {
|
|
|
452 |
if (!isset($backtrace[$i]['file'], $backtrace[$i]['line'], $backtrace[$i]['function'])) {
|
|
|
453 |
continue;
|
|
|
454 |
}
|
|
|
455 |
if (!isset($backtrace[$i]['class']) && 'trigger_deprecation' === $backtrace[$i]['function']) {
|
|
|
456 |
$file = $backtrace[$i]['file'];
|
|
|
457 |
$line = $backtrace[$i]['line'];
|
|
|
458 |
$backtrace = \array_slice($backtrace, 1 + $i);
|
|
|
459 |
break;
|
|
|
460 |
}
|
|
|
461 |
}
|
|
|
462 |
|
|
|
463 |
// Remove frames added by DebugClassLoader.
|
|
|
464 |
for ($i = \count($backtrace) - 2; 0 < $i; --$i) {
|
|
|
465 |
if (DebugClassLoader::class === ($backtrace[$i]['class'] ?? null)) {
|
|
|
466 |
$backtrace = [$backtrace[$i + 1]];
|
|
|
467 |
break;
|
|
|
468 |
}
|
|
|
469 |
}
|
|
|
470 |
|
|
|
471 |
$collectedLogs[$message] = [
|
|
|
472 |
'type' => $type,
|
|
|
473 |
'message' => $message,
|
|
|
474 |
'file' => $file,
|
|
|
475 |
'line' => $line,
|
|
|
476 |
'trace' => [$backtrace[0]],
|
|
|
477 |
'count' => 1,
|
|
|
478 |
];
|
|
|
479 |
|
|
|
480 |
return null;
|
|
|
481 |
});
|
|
|
482 |
}
|
|
|
483 |
|
|
|
484 |
try {
|
|
|
485 |
$container = null;
|
|
|
486 |
$container = $this->buildContainer();
|
|
|
487 |
$container->compile();
|
|
|
488 |
} finally {
|
|
|
489 |
if ($collectDeprecations) {
|
|
|
490 |
restore_error_handler();
|
|
|
491 |
|
|
|
492 |
@file_put_contents($buildDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs)));
|
|
|
493 |
@file_put_contents($buildDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
|
|
|
494 |
}
|
|
|
495 |
}
|
|
|
496 |
|
|
|
497 |
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());
|
|
|
498 |
|
|
|
499 |
if ($lock) {
|
|
|
500 |
flock($lock, \LOCK_UN);
|
|
|
501 |
fclose($lock);
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
$this->container = require $cachePath;
|
|
|
505 |
$this->container->set('kernel', $this);
|
|
|
506 |
|
|
|
507 |
if ($oldContainer && \get_class($this->container) !== $oldContainer->name) {
|
|
|
508 |
// Because concurrent requests might still be using them,
|
|
|
509 |
// old container files are not removed immediately,
|
|
|
510 |
// but on a next dump of the container.
|
|
|
511 |
static $legacyContainers = [];
|
|
|
512 |
$oldContainerDir = \dirname($oldContainer->getFileName());
|
|
|
513 |
$legacyContainers[$oldContainerDir.'.legacy'] = true;
|
|
|
514 |
foreach (glob(\dirname($oldContainerDir).\DIRECTORY_SEPARATOR.'*.legacy', \GLOB_NOSORT) as $legacyContainer) {
|
|
|
515 |
if (!isset($legacyContainers[$legacyContainer]) && @unlink($legacyContainer)) {
|
|
|
516 |
(new Filesystem())->remove(substr($legacyContainer, 0, -7));
|
|
|
517 |
}
|
|
|
518 |
}
|
|
|
519 |
|
|
|
520 |
touch($oldContainerDir.'.legacy');
|
|
|
521 |
}
|
|
|
522 |
|
|
|
523 |
$preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir')) : [];
|
|
|
524 |
|
|
|
525 |
if ($this->container->has('cache_warmer')) {
|
|
|
526 |
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')));
|
|
|
527 |
}
|
|
|
528 |
|
|
|
529 |
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $buildDir.'/'.$class.'.preload.php')) {
|
|
|
530 |
Preloader::append($preloadFile, $preload);
|
|
|
531 |
}
|
|
|
532 |
}
|
|
|
533 |
|
|
|
534 |
/**
|
|
|
535 |
* Returns the kernel parameters.
|
|
|
536 |
*/
|
|
|
537 |
protected function getKernelParameters(): array
|
|
|
538 |
{
|
|
|
539 |
$bundles = [];
|
|
|
540 |
$bundlesMetadata = [];
|
|
|
541 |
|
|
|
542 |
foreach ($this->bundles as $name => $bundle) {
|
|
|
543 |
$bundles[$name] = $bundle::class;
|
|
|
544 |
$bundlesMetadata[$name] = [
|
|
|
545 |
'path' => $bundle->getPath(),
|
|
|
546 |
'namespace' => $bundle->getNamespace(),
|
|
|
547 |
];
|
|
|
548 |
}
|
|
|
549 |
|
|
|
550 |
return [
|
|
|
551 |
'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
|
|
|
552 |
'kernel.environment' => $this->environment,
|
|
|
553 |
'kernel.runtime_environment' => '%env(default:kernel.environment:APP_RUNTIME_ENV)%',
|
|
|
554 |
'kernel.debug' => $this->debug,
|
|
|
555 |
'kernel.build_dir' => realpath($buildDir = $this->warmupDir ?: $this->getBuildDir()) ?: $buildDir,
|
|
|
556 |
'kernel.cache_dir' => realpath($cacheDir = ($this->getCacheDir() === $this->getBuildDir() ? ($this->warmupDir ?: $this->getCacheDir()) : $this->getCacheDir())) ?: $cacheDir,
|
|
|
557 |
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
|
|
|
558 |
'kernel.bundles' => $bundles,
|
|
|
559 |
'kernel.bundles_metadata' => $bundlesMetadata,
|
|
|
560 |
'kernel.charset' => $this->getCharset(),
|
|
|
561 |
'kernel.container_class' => $this->getContainerClass(),
|
|
|
562 |
];
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
/**
|
|
|
566 |
* Builds the service container.
|
|
|
567 |
*
|
|
|
568 |
* @throws \RuntimeException
|
|
|
569 |
*/
|
|
|
570 |
protected function buildContainer(): ContainerBuilder
|
|
|
571 |
{
|
|
|
572 |
foreach (['cache' => $this->getCacheDir(), 'build' => $this->warmupDir ?: $this->getBuildDir(), 'logs' => $this->getLogDir()] as $name => $dir) {
|
|
|
573 |
if (!is_dir($dir)) {
|
|
|
574 |
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
|
|
|
575 |
throw new \RuntimeException(sprintf('Unable to create the "%s" directory (%s).', $name, $dir));
|
|
|
576 |
}
|
|
|
577 |
} elseif (!is_writable($dir)) {
|
|
|
578 |
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory (%s).', $name, $dir));
|
|
|
579 |
}
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
$container = $this->getContainerBuilder();
|
|
|
583 |
$container->addObjectResource($this);
|
|
|
584 |
$this->prepareContainer($container);
|
|
|
585 |
$this->registerContainerConfiguration($this->getContainerLoader($container));
|
|
|
586 |
|
|
|
587 |
$container->addCompilerPass(new AddAnnotatedClassesToCachePass($this));
|
|
|
588 |
|
|
|
589 |
return $container;
|
|
|
590 |
}
|
|
|
591 |
|
|
|
592 |
/**
|
|
|
593 |
* Prepares the ContainerBuilder before it is compiled.
|
|
|
594 |
*/
|
|
|
595 |
protected function prepareContainer(ContainerBuilder $container)
|
|
|
596 |
{
|
|
|
597 |
$extensions = [];
|
|
|
598 |
foreach ($this->bundles as $bundle) {
|
|
|
599 |
if ($extension = $bundle->getContainerExtension()) {
|
|
|
600 |
$container->registerExtension($extension);
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
if ($this->debug) {
|
|
|
604 |
$container->addObjectResource($bundle);
|
|
|
605 |
}
|
|
|
606 |
}
|
|
|
607 |
|
|
|
608 |
foreach ($this->bundles as $bundle) {
|
|
|
609 |
$bundle->build($container);
|
|
|
610 |
}
|
|
|
611 |
|
|
|
612 |
$this->build($container);
|
|
|
613 |
|
|
|
614 |
foreach ($container->getExtensions() as $extension) {
|
|
|
615 |
$extensions[] = $extension->getAlias();
|
|
|
616 |
}
|
|
|
617 |
|
|
|
618 |
// ensure these extensions are implicitly loaded
|
|
|
619 |
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
|
|
|
620 |
}
|
|
|
621 |
|
|
|
622 |
/**
|
|
|
623 |
* Gets a new ContainerBuilder instance used to build the service container.
|
|
|
624 |
*/
|
|
|
625 |
protected function getContainerBuilder(): ContainerBuilder
|
|
|
626 |
{
|
|
|
627 |
$container = new ContainerBuilder();
|
|
|
628 |
$container->getParameterBag()->add($this->getKernelParameters());
|
|
|
629 |
|
|
|
630 |
if ($this instanceof ExtensionInterface) {
|
|
|
631 |
$container->registerExtension($this);
|
|
|
632 |
}
|
|
|
633 |
if ($this instanceof CompilerPassInterface) {
|
|
|
634 |
$container->addCompilerPass($this, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000);
|
|
|
635 |
}
|
|
|
636 |
|
|
|
637 |
return $container;
|
|
|
638 |
}
|
|
|
639 |
|
|
|
640 |
/**
|
|
|
641 |
* Dumps the service container to PHP code in the cache.
|
|
|
642 |
*
|
|
|
643 |
* @param string $class The name of the class to generate
|
|
|
644 |
* @param string $baseClass The name of the container's base class
|
|
|
645 |
*/
|
|
|
646 |
protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, string $class, string $baseClass)
|
|
|
647 |
{
|
|
|
648 |
// cache the container
|
|
|
649 |
$dumper = new PhpDumper($container);
|
|
|
650 |
|
|
|
651 |
$content = $dumper->dump([
|
|
|
652 |
'class' => $class,
|
|
|
653 |
'base_class' => $baseClass,
|
|
|
654 |
'file' => $cache->getPath(),
|
|
|
655 |
'as_files' => true,
|
|
|
656 |
'debug' => $this->debug,
|
|
|
657 |
'build_time' => $container->hasParameter('kernel.container_build_time') ? $container->getParameter('kernel.container_build_time') : time(),
|
|
|
658 |
'preload_classes' => array_map('get_class', $this->bundles),
|
|
|
659 |
]);
|
|
|
660 |
|
|
|
661 |
$rootCode = array_pop($content);
|
|
|
662 |
$dir = \dirname($cache->getPath()).'/';
|
|
|
663 |
$fs = new Filesystem();
|
|
|
664 |
|
|
|
665 |
foreach ($content as $file => $code) {
|
|
|
666 |
$fs->dumpFile($dir.$file, $code);
|
|
|
667 |
@chmod($dir.$file, 0666 & ~umask());
|
|
|
668 |
}
|
|
|
669 |
$legacyFile = \dirname($dir.key($content)).'.legacy';
|
|
|
670 |
if (is_file($legacyFile)) {
|
|
|
671 |
@unlink($legacyFile);
|
|
|
672 |
}
|
|
|
673 |
|
|
|
674 |
$cache->write($rootCode, $container->getResources());
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
/**
|
|
|
678 |
* Returns a loader for the container.
|
|
|
679 |
*/
|
|
|
680 |
protected function getContainerLoader(ContainerInterface $container): DelegatingLoader
|
|
|
681 |
{
|
|
|
682 |
$env = $this->getEnvironment();
|
|
|
683 |
$locator = new FileLocator($this);
|
|
|
684 |
$resolver = new LoaderResolver([
|
|
|
685 |
new XmlFileLoader($container, $locator, $env),
|
|
|
686 |
new YamlFileLoader($container, $locator, $env),
|
|
|
687 |
new IniFileLoader($container, $locator, $env),
|
|
|
688 |
new PhpFileLoader($container, $locator, $env, class_exists(ConfigBuilderGenerator::class) ? new ConfigBuilderGenerator($this->getBuildDir()) : null),
|
|
|
689 |
new GlobFileLoader($container, $locator, $env),
|
|
|
690 |
new DirectoryLoader($container, $locator, $env),
|
|
|
691 |
new ClosureLoader($container, $env),
|
|
|
692 |
]);
|
|
|
693 |
|
|
|
694 |
return new DelegatingLoader($resolver);
|
|
|
695 |
}
|
|
|
696 |
|
|
|
697 |
private function preBoot(): ContainerInterface
|
|
|
698 |
{
|
|
|
699 |
if ($this->debug) {
|
|
|
700 |
$this->startTime = microtime(true);
|
|
|
701 |
}
|
|
|
702 |
if ($this->debug && !isset($_ENV['SHELL_VERBOSITY']) && !isset($_SERVER['SHELL_VERBOSITY'])) {
|
|
|
703 |
putenv('SHELL_VERBOSITY=3');
|
|
|
704 |
$_ENV['SHELL_VERBOSITY'] = 3;
|
|
|
705 |
$_SERVER['SHELL_VERBOSITY'] = 3;
|
|
|
706 |
}
|
|
|
707 |
|
|
|
708 |
$this->initializeBundles();
|
|
|
709 |
$this->initializeContainer();
|
|
|
710 |
|
|
|
711 |
$container = $this->container;
|
|
|
712 |
|
|
|
713 |
if ($container->hasParameter('kernel.trusted_hosts') && $trustedHosts = $container->getParameter('kernel.trusted_hosts')) {
|
|
|
714 |
Request::setTrustedHosts($trustedHosts);
|
|
|
715 |
}
|
|
|
716 |
|
|
|
717 |
if ($container->hasParameter('kernel.trusted_proxies') && $container->hasParameter('kernel.trusted_headers') && $trustedProxies = $container->getParameter('kernel.trusted_proxies')) {
|
|
|
718 |
Request::setTrustedProxies(\is_array($trustedProxies) ? $trustedProxies : array_map('trim', explode(',', $trustedProxies)), $container->getParameter('kernel.trusted_headers'));
|
|
|
719 |
}
|
|
|
720 |
|
|
|
721 |
return $container;
|
|
|
722 |
}
|
|
|
723 |
|
|
|
724 |
/**
|
|
|
725 |
* Removes comments from a PHP source string.
|
|
|
726 |
*
|
|
|
727 |
* We don't use the PHP php_strip_whitespace() function
|
|
|
728 |
* as we want the content to be readable and well-formatted.
|
|
|
729 |
*/
|
|
|
730 |
public static function stripComments(string $source): string
|
|
|
731 |
{
|
|
|
732 |
if (!\function_exists('token_get_all')) {
|
|
|
733 |
return $source;
|
|
|
734 |
}
|
|
|
735 |
|
|
|
736 |
$rawChunk = '';
|
|
|
737 |
$output = '';
|
|
|
738 |
$tokens = token_get_all($source);
|
|
|
739 |
$ignoreSpace = false;
|
|
|
740 |
for ($i = 0; isset($tokens[$i]); ++$i) {
|
|
|
741 |
$token = $tokens[$i];
|
|
|
742 |
if (!isset($token[1]) || 'b"' === $token) {
|
|
|
743 |
$rawChunk .= $token;
|
|
|
744 |
} elseif (\T_START_HEREDOC === $token[0]) {
|
|
|
745 |
$output .= $rawChunk.$token[1];
|
|
|
746 |
do {
|
|
|
747 |
$token = $tokens[++$i];
|
|
|
748 |
$output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token;
|
|
|
749 |
} while (\T_END_HEREDOC !== $token[0]);
|
|
|
750 |
$rawChunk = '';
|
|
|
751 |
} elseif (\T_WHITESPACE === $token[0]) {
|
|
|
752 |
if ($ignoreSpace) {
|
|
|
753 |
$ignoreSpace = false;
|
|
|
754 |
|
|
|
755 |
continue;
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
// replace multiple new lines with a single newline
|
|
|
759 |
$rawChunk .= preg_replace(['/\n{2,}/S'], "\n", $token[1]);
|
|
|
760 |
} elseif (\in_array($token[0], [\T_COMMENT, \T_DOC_COMMENT])) {
|
|
|
761 |
if (!\in_array($rawChunk[\strlen($rawChunk) - 1], [' ', "\n", "\r", "\t"], true)) {
|
|
|
762 |
$rawChunk .= ' ';
|
|
|
763 |
}
|
|
|
764 |
$ignoreSpace = true;
|
|
|
765 |
} else {
|
|
|
766 |
$rawChunk .= $token[1];
|
|
|
767 |
|
|
|
768 |
// The PHP-open tag already has a new-line
|
|
|
769 |
if (\T_OPEN_TAG === $token[0]) {
|
|
|
770 |
$ignoreSpace = true;
|
|
|
771 |
} else {
|
|
|
772 |
$ignoreSpace = false;
|
|
|
773 |
}
|
|
|
774 |
}
|
|
|
775 |
}
|
|
|
776 |
|
|
|
777 |
$output .= $rawChunk;
|
|
|
778 |
|
|
|
779 |
unset($tokens, $rawChunk);
|
|
|
780 |
gc_mem_caches();
|
|
|
781 |
|
|
|
782 |
return $output;
|
|
|
783 |
}
|
|
|
784 |
|
|
|
785 |
public function __sleep(): array
|
|
|
786 |
{
|
|
|
787 |
return ['environment', 'debug'];
|
|
|
788 |
}
|
|
|
789 |
|
|
|
790 |
public function __wakeup()
|
|
|
791 |
{
|
|
|
792 |
if (\is_object($this->environment) || \is_object($this->debug)) {
|
|
|
793 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
794 |
}
|
|
|
795 |
|
|
|
796 |
$this->__construct($this->environment, $this->debug);
|
|
|
797 |
}
|
|
|
798 |
}
|