/
Kernel.php
233 lines (192 loc) · 8.52 KB
/
Kernel.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
<?php
namespace ForkCMS\App;
use PDOException;
use Spoon;
use SpoonDatabaseException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Backend\DependencyInjection\BackendExtension;
/**
* The Kernel provides a proper way to load an environment and DI container.
* It also handles requests and responses.
*/
abstract class Kernel extends BaseKernel
{
/** @var Request We need this to check if a module is being installed */
private $request;
/**
* Constructor.
*
* @param string $environment The environment
* @param bool $enableDebug Whether to enable debugging or not
*
* @api
*/
public function __construct(string $environment, bool $enableDebug)
{
$this->request = Request::createFromGlobals();
parent::__construct($environment, $enableDebug);
$this->boot();
}
/**
* {@inheritdoc}
*
* @api
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
{
// boot if it hasn't booted yet
$this->boot();
return $this->getHttpKernel()->handle($request, $type, $catch);
}
/**
* Boot and define the Fork Constants.
*/
public function boot(): void
{
if ($this->booted) {
return;
}
parent::boot();
// define Fork constants
$this->defineForkConstants();
}
/**
* This will disappear in time in favour of container-driven parameters.
*
* @deprecated
*/
public function defineForkConstants(): void
{
$container = $this->getContainer();
Spoon::setDebug($container->getParameter('kernel.debug'));
Spoon::setDebugEmail($container->getParameter('fork.debug_email'));
Spoon::setDebugMessage($container->getParameter('fork.debug_message'));
Spoon::setCharset(strtolower($container->getParameter('kernel.charset')));
/**
* @deprecated SPOON_* constants are deprecated in favour of Spoon::set*().
* Will be removed in the next major release.
*/
defined('PATH_WWW') || define('PATH_WWW', realpath($container->getParameter('site.path_www')));
defined('SITE_DEFAULT_LANGUAGE') || define('SITE_DEFAULT_LANGUAGE', $container->getParameter('site.default_language'));
defined('SITE_DEFAULT_TITLE') || define('SITE_DEFAULT_TITLE', $container->getParameter('site.default_title'));
defined('SITE_MULTILANGUAGE') || define('SITE_MULTILANGUAGE', $container->getParameter('site.multilanguage'));
defined('SITE_DOMAIN') || define('SITE_DOMAIN', $container->getParameter('site.domain'));
defined('SITE_PROTOCOL') || define('SITE_PROTOCOL', $container->getParameter('site.protocol'));
defined('SITE_URL') || define('SITE_URL', SITE_PROTOCOL . '://' . SITE_DOMAIN);
defined('FORK_VERSION') || define('FORK_VERSION', $container->getParameter('fork.version'));
defined('ACTION_GROUP_TAG') || define('ACTION_GROUP_TAG', $container->getParameter('action.group_tag'));
defined('ACTION_RIGHTS_LEVEL') || define('ACTION_RIGHTS_LEVEL', $container->getParameter('action.rights_level'));
defined('BACKEND_PATH') || define('BACKEND_PATH', PATH_WWW . '/src/Backend');
defined('BACKEND_CACHE_PATH') || define('BACKEND_CACHE_PATH', BACKEND_PATH . '/Cache');
defined('BACKEND_CORE_PATH') || define('BACKEND_CORE_PATH', BACKEND_PATH . '/Core');
defined('BACKEND_MODULES_PATH') || define('BACKEND_MODULES_PATH', BACKEND_PATH . '/Modules');
defined('BACKEND_CORE_URL') || define('BACKEND_CORE_URL', '/src/Backend/Core');
defined('BACKEND_CACHE_URL') || define('BACKEND_CACHE_URL', '/src/Backend/Cache');
defined('FRONTEND_PATH') || define('FRONTEND_PATH', PATH_WWW . '/src/Frontend');
defined('FRONTEND_CACHE_PATH') || define('FRONTEND_CACHE_PATH', FRONTEND_PATH . '/Cache');
defined('FRONTEND_THEMES_PATH') || define('FRONTEND_THEMES_PATH', FRONTEND_PATH . '/Themes');
defined('FRONTEND_CORE_PATH') || define('FRONTEND_CORE_PATH', FRONTEND_PATH . '/Core');
defined('FRONTEND_MODULES_PATH') || define('FRONTEND_MODULES_PATH', FRONTEND_PATH . '/Modules');
defined('FRONTEND_FILES_PATH') || define('FRONTEND_FILES_PATH', FRONTEND_PATH . '/Files');
defined('FRONTEND_FILES_URL') || define('FRONTEND_FILES_URL', '/src/Frontend/Files');
defined('FRONTEND_CORE_URL') || define('FRONTEND_CORE_URL', '/src/Frontend/Core');
defined('FRONTEND_CACHE_URL') || define('FRONTEND_CACHE_URL', '/src/Frontend/Cache');
}
/**
* Builds the service container.
*
* @throws \RuntimeException
*
* @return ContainerBuilder The compiled service container
*/
protected function buildContainer()
{
$container = parent::buildContainer();
$installedModules = $this->getInstalledModules($container);
$container->setParameter('installed_modules', $installedModules);
foreach ($installedModules as $module) {
$class = 'Backend\\Modules\\' . $module . '\\DependencyInjection\\' . $module . 'Extension';
if (class_exists($class)) {
$container->registerExtension(new $class());
}
}
$container->registerExtension(new BackendExtension());
// ensure these extensions are implicitly loaded
$container->getCompilerPassConfig()->setMergePass(
new MergeExtensionConfigurationPass(array_keys($container->getExtensions()))
);
return $container;
}
private function getInstalledModules(ContainerBuilder $containerBuilder): array
{
// on installation all modules should be loaded
if ($this->environment === 'install' || $this->environment === 'test') {
return $this->getAllPossibleModuleNames();
}
$moduleNames = [];
if ($this->isInstallingModule()) {
$moduleNames[] = $this->request->query->get('module') ?? $_SERVER['INSTALLING_MODULE'];
}
try {
$moduleNames = array_merge(
$moduleNames,
(array) $containerBuilder->get('database')->getColumn(
'SELECT name FROM modules'
)
);
} catch (SpoonDatabaseException $e) {
$moduleNames = [];
} catch (PDOException $e) {
// fork is probably not installed yet
$moduleNames = [];
}
if (empty($moduleNames)) {
return $this->getAllPossibleModuleNames();
}
return $moduleNames;
}
public function isInstallingModule(): bool
{
$isInstallingModuleHttp = preg_match('/\/private(\/\w\w)?\/extensions\/install_module\?/', $this->request->getRequestUri())
&& $this->request->query->has('module')
&& in_array($this->request->query->get('module'), $this->getAllPossibleModuleNames(), true);
$isInstallingModuleCli = PHP_SAPI === "cli"
&& isset($_SERVER['INSTALLING_MODULE'])
&& in_array($_SERVER['INSTALLING_MODULE'], $this->getAllPossibleModuleNames(), true);
return $isInstallingModuleHttp || $isInstallingModuleCli;
}
private function getAllPossibleModuleNames(): array
{
$moduleNames = [];
$finder = new Finder();
$directories = $finder->directories()->in(__DIR__ . '/../src/Backend/Modules')->depth(0);
foreach ($directories->getIterator() as $directory) {
$moduleNames[] = $directory->getFilename();
}
return $moduleNames;
}
protected function initializeContainer(): void
{
// remove the cache dir when installing a module to trigger rebuilding the kernel
if ($this->isInstallingModule()) {
$fileSystem = new Filesystem();
$fileSystem->remove($this->getCacheDir().'/'.$this->getContainerClass().'.php');
}
parent::initializeContainer();
}
public function getLogDir(): string
{
return dirname(__DIR__).'/var/logs/' . $this->environment;
}
public function getCacheDir(): string
{
return dirname(__DIR__) . '/var/cache/' . $this->environment;
}
}