-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathKernel.php
More file actions
283 lines (230 loc) · 8.88 KB
/
Kernel.php
File metadata and controls
283 lines (230 loc) · 8.88 KB
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel;
use Symfony\Component\DependencyInjection\Dumper\Preloader;
use Symfony\Component\DependencyInjection\Kernel\AbstractKernel;
use Symfony\Component\DependencyInjection\Kernel\KernelTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Bundle\BundleAdapter;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
/**
* The Kernel is the heart of the Symfony system.
*
* It manages an environment made of bundles.
*
* Environment names must always start with a letter and
* they must only contain letters and numbers.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Kernel extends AbstractKernel implements KernelInterface, RebootableInterface, TerminableInterface
{
use KernelTrait {
registerBundles as public;
registerContainerConfiguration as public;
initializeBundles as protected doInitializeBundles;
initializeContainer as protected doInitializeContainer;
getKernelParameters as private doGetKernelParameters;
}
private ?string $warmupDir = null;
private int $requestStackSize = 0;
private bool $resetServices = false;
private bool $handlingHttpCache = false;
public const VERSION = '8.1.0-DEV';
public const VERSION_ID = 80100;
public const MAJOR_VERSION = 8;
public const MINOR_VERSION = 1;
public const RELEASE_VERSION = 0;
public const EXTRA_VERSION = 'DEV';
public const END_OF_MAINTENANCE = '01/2027';
public const END_OF_LIFE = '01/2027';
public function __clone()
{
parent::__clone();
$this->requestStackSize = 0;
$this->resetServices = false;
$this->handlingHttpCache = false;
}
public function boot(): void
{
if ($this->booted) {
if (!$this->requestStackSize && $this->resetServices) {
if ($this->container->has('services_resetter')) {
$this->container->get('services_resetter')->reset();
}
$this->resetServices = false;
if ($this->debug) {
$this->startTime = microtime(true);
}
}
return;
}
if (!$this->container) {
$this->preBoot();
}
foreach ($this->getBundles() as $bundle) {
$bundle->setContainer($this->container);
$bundle->boot();
}
$this->booted = true;
}
public function reboot(?string $warmupDir): void
{
$this->shutdown();
$this->warmupDir = $warmupDir;
$this->boot();
}
public function terminate(Request $request, Response $response): void
{
if (!$this->booted) {
return;
}
if ($this->getHttpKernel() instanceof TerminableInterface) {
$this->getHttpKernel()->terminate($request, $response);
}
}
public function shutdown(): void
{
parent::shutdown();
$this->requestStackSize = 0;
$this->resetServices = false;
}
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response
{
if (!$this->container) {
$this->preBoot();
}
if (HttpKernelInterface::MAIN_REQUEST === $type && !$this->handlingHttpCache && $this->container->has('http_cache')) {
$this->handlingHttpCache = true;
try {
return $this->container->get('http_cache')->handle($request, $type, $catch);
} finally {
$this->handlingHttpCache = false;
$this->resetServices = true;
}
}
$this->boot();
++$this->requestStackSize;
if (!$this->handlingHttpCache) {
$this->resetServices = true;
}
try {
return $this->getHttpKernel()->handle($request, $type, $catch);
} finally {
--$this->requestStackSize;
}
}
protected function getHttpKernel(): HttpKernelInterface
{
return $this->container->get('http_kernel');
}
public function getBundle(string $name): BundleInterface
{
if (!isset($this->bundles[$name])) {
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)));
}
return $this->bundles[$name];
}
public function getCacheDir(): string
{
return $this->getProjectDir().'/var/cache/'.$this->environment;
}
public function getBuildDir(): string
{
return $this->getCacheDir();
}
public function getShareDir(): ?string
{
return $this->getCacheDir();
}
public function getLogDir(): string
{
return $this->getProjectDir().'/var/log';
}
public function getCharset(): string
{
return 'UTF-8';
}
protected function initializeBundles(): void
{
$this->doInitializeBundles();
foreach ($this->bundles as $name => $bundle) {
$this->bundles[$name] = !$bundle instanceof BundleInterface ? new BundleAdapter($bundle) : $bundle;
}
}
protected function initializeContainer(): void
{
$cachePath = $this->getEffectiveBuildDir().'/'.$this->getContainerClass().'.php';
$oldMtime = is_file($cachePath) ? filemtime($cachePath) : false;
$this->doInitializeContainer();
if (false !== $oldMtime && filemtime($cachePath) === $oldMtime) {
return;
}
$buildDir = $this->container->getParameter('kernel.build_dir');
$cacheDir = $this->container->getParameter('kernel.cache_dir');
$preload = $this instanceof WarmableInterface ? $this->warmUp($cacheDir, $buildDir) : [];
if ($this->container->has('cache_warmer')) {
$cacheWarmer = $this->container->get('cache_warmer');
if ($cacheDir !== $buildDir) {
$cacheWarmer->enableOptionalWarmers();
}
$preload = array_merge($preload, $cacheWarmer->warmUp($cacheDir, $buildDir));
}
if ($preload && file_exists($preloadFile = $buildDir.'/'.$this->getContainerClass().'.preload.php')) {
Preloader::append($preloadFile, $preload);
}
}
protected function getKernelParameters(): array
{
$parameters = $this->doGetKernelParameters() + [
'kernel.charset' => $this->getCharset(),
];
foreach ($this->bundles as $name => $bundle) {
$parameters['kernel.bundles_metadata'][$name]['namespace'] = $bundle->getNamespace();
}
return $parameters;
}
private function preBoot(): void
{
if ($this->debug) {
$this->startTime = microtime(true);
}
$this->initializeBundles();
$this->initializeContainer();
$container = $this->container;
if ($container->hasParameter('kernel.trusted_hosts') && $trustedHosts = $container->getParameter('kernel.trusted_hosts')) {
Request::setTrustedHosts(\is_array($trustedHosts) ? $trustedHosts : preg_split('/\s*+,\s*+(?![^{]*})/', $trustedHosts));
}
if ($container->hasParameter('kernel.trusted_proxies') && $container->hasParameter('kernel.trusted_headers') && $trustedProxies = $container->getParameter('kernel.trusted_proxies')) {
$trustedHeaders = $container->getParameter('kernel.trusted_headers');
if (\is_string($trustedHeaders)) {
$trustedHeaders = array_map('trim', explode(',', $trustedHeaders));
}
if (\is_array($trustedHeaders)) {
$trustedHeaderSet = 0;
foreach ($trustedHeaders as $header) {
if (!\defined($const = Request::class.'::HEADER_'.strtr(strtoupper($header), '-', '_'))) {
throw new \InvalidArgumentException(\sprintf('The trusted header "%s" is not supported.', $header));
}
$trustedHeaderSet |= \constant($const);
}
} else {
$trustedHeaderSet = $trustedHeaders ?? (Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO);
}
Request::setTrustedProxies(\is_array($trustedProxies) ? $trustedProxies : array_map('trim', explode(',', $trustedProxies)), $trustedHeaderSet);
}
}
private function getEffectiveBuildDir(): string
{
return $this->warmupDir ?: $this->getBuildDir();
}
}