-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpKernel.php
More file actions
188 lines (148 loc) · 5.67 KB
/
HttpKernel.php
File metadata and controls
188 lines (148 loc) · 5.67 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
<?php declare(strict_types=1);
namespace Shopware\Core;
use Composer\Autoload\ClassLoader;
use Composer\InstalledVersions;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Shopware\Core\Framework\Adapter\Cache\CacheIdLoader;
use Shopware\Core\Framework\Adapter\Database\MySQLFactory;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\DbalKernelPluginLoader;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\KernelPluginLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
/**
* @psalm-import-type Params from DriverManager
*/
class HttpKernel
{
protected static ?Connection $connection = null;
/**
* @var class-string<Kernel>
*/
protected static string $kernelClass = Kernel::class;
/**
* @var class-string<HttpCache>
*/
protected static string $httpCacheClass = HttpCache::class;
protected ?ClassLoader $classLoader;
protected string $environment;
protected bool $debug;
protected ?string $projectDir = null;
protected ?KernelPluginLoader $pluginLoader = null;
protected ?KernelInterface $kernel = null;
public function __construct(string $environment, bool $debug, ?ClassLoader $classLoader = null)
{
$this->classLoader = $classLoader;
$this->environment = $environment;
$this->debug = $debug;
}
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): HttpKernelResult
{
try {
return $this->doHandle($request, (int) $type, (bool) $catch);
} catch (Exception $e) {
/** @var Params|array{url?: string} $connectionParams */
$connectionParams = self::getConnection()->getParams();
$message = str_replace([$connectionParams['url'] ?? null, $connectionParams['password'] ?? null, $connectionParams['user'] ?? null], '******', $e->getMessage());
throw new \RuntimeException(sprintf('Could not connect to database. Message from SQL Server: %s', $message));
}
}
public function getKernel(): KernelInterface
{
return $this->createKernel();
}
/**
* Allows to switch the plugin loading.
*/
public function setPluginLoader(KernelPluginLoader $pluginLoader): void
{
$this->pluginLoader = $pluginLoader;
}
public static function getConnection(): Connection
{
if (self::$connection) {
return self::$connection;
}
self::$connection = MySQLFactory::create();
return self::$connection;
}
public function terminate(Request $request, Response $response): void
{
if (!$this->kernel instanceof TerminableInterface) {
return;
}
$this->kernel->terminate($request, $response);
}
private function doHandle(Request $request, int $type, bool $catch): HttpKernelResult
{
// create core kernel which contains bootstrapping for plugins etc.
$kernel = $this->createKernel();
$kernel->boot();
$response = $kernel->handle($request, $type, $catch);
return new HttpKernelResult($request, $response);
}
private function createKernel(): KernelInterface
{
if ($this->kernel !== null) {
return $this->kernel;
}
if (InstalledVersions::isInstalled('shopware/platform')) {
$shopwareVersion = InstalledVersions::getVersion('shopware/platform')
. '@' . InstalledVersions::getReference('shopware/platform');
} else {
$shopwareVersion = InstalledVersions::getVersion('shopware/core')
. '@' . InstalledVersions::getReference('shopware/core');
}
$connection = self::getConnection();
$pluginLoader = $this->createPluginLoader($connection);
$cacheId = (new CacheIdLoader())->load();
return $this->kernel = new static::$kernelClass(
$this->environment,
$this->debug,
$pluginLoader,
$cacheId,
$shopwareVersion,
$connection,
$this->getProjectDir()
);
}
private function getProjectDir(): string
{
if ($this->projectDir === null) {
if ($dir = $_ENV['PROJECT_ROOT'] ?? $_SERVER['PROJECT_ROOT'] ?? false) {
return $this->projectDir = $dir;
}
$r = new \ReflectionObject($this);
/** @var string $dir */
$dir = $r->getFileName();
if (!file_exists($dir)) {
throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name));
}
$dir = $rootDir = \dirname($dir);
while (!file_exists($dir . '/vendor')) {
if ($dir === \dirname($dir)) {
return $this->projectDir = $rootDir;
}
$dir = \dirname($dir);
}
$this->projectDir = $dir;
}
return $this->projectDir;
}
private function createPluginLoader(Connection $connection): KernelPluginLoader
{
if ($this->pluginLoader) {
return $this->pluginLoader;
}
if (!$this->classLoader) {
throw new \RuntimeException('No plugin loader and no class loader provided');
}
$this->pluginLoader = new DbalKernelPluginLoader($this->classLoader, null, $connection);
return $this->pluginLoader;
}
}