-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathKernel.php
More file actions
186 lines (150 loc) · 5.4 KB
/
Kernel.php
File metadata and controls
186 lines (150 loc) · 5.4 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
<?php
declare(strict_types=1);
namespace SimpleSAML;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Utils\System;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use function getenv;
use function is_dir;
use function sys_get_temp_dir;
/**
* A class to create the container and handle a given request.
*/
class Kernel extends BaseKernel
{
use MicroKernelTrait;
public const string CONFIG_EXTS = '.{php,xml,yaml,yml}';
public function __construct()
{
$env = getenv('APP_ENV') ?: (getenv('SYMFONY_ENV') ?: 'prod');
parent::__construct($env, false);
}
/**
* @return string
*/
public function getCacheDir(): string
{
$configuration = Configuration::getInstance();
$temp = $configuration->getOptionalString('tempdir', null);
$cache = $configuration->getOptionalString('cachedir', null);
$cacheDir = $cache ?? $temp;
Assert::notNull($cacheDir, "Missing cachedir parameter in config.php");
$cachePath = $cacheDir . DIRECTORY_SEPARATOR . 'global';
$sysUtils = new System();
if ($sysUtils->isAbsolutePath($cachePath)) {
return $cachePath;
}
return $configuration->getBaseDir() . $cachePath;
}
/**
* @return string
*/
public function getLogDir(): string
{
$configuration = Configuration::getInstance();
$handler = $configuration->getString('logging.handler');
$loggingPath = $configuration->getOptionalString('loggingdir', sys_get_temp_dir());
$sysUtils = new System();
if ($sysUtils->isAbsolutePath($loggingPath)) {
return $loggingPath;
}
return $configuration->getBaseDir() . DIRECTORY_SEPARATOR . $loggingPath;
}
/**
* {@inheritdoc}
*/
public function registerBundles(): array
{
return [
new FrameworkBundle(),
];
}
/**
* Configures the container.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* @param \Symfony\Component\Config\Loader\LoaderInterface $loader
*/
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$configuration = Configuration::getInstance();
$baseDir = $configuration->getBaseDir();
$loader->load($baseDir . '/routing/services/*' . self::CONFIG_EXTS, 'glob');
foreach ($this->getEnabledModules() as $module) {
$confDir = Module::getModuleDir($module) . '/routing/services';
if (is_dir($confDir)) {
$loader->load($confDir . '/**/*' . self::CONFIG_EXTS, 'glob');
}
}
$container->loadFromExtension('framework', [
'secret' => Configuration::getInstance()->getString('secretsalt'),
]);
$this->registerModuleControllers($container);
}
/**
* Import routes.
*
* @param \Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator $routes
*/
protected function configureRoutes(RoutingConfigurator $routes): void
{
$configuration = Configuration::getInstance();
$baseDir = $configuration->getBaseDir();
$routes->import($baseDir . '/routing/routes/*' . self::CONFIG_EXTS);
foreach ($this->getEnabledModules() as $module) {
$confDir = Module::getModuleDir($module) . '/routing/routes';
if (is_dir($confDir)) {
$routes->import($confDir . '/**/*' . self::CONFIG_EXTS)->prefix('/module/' . $module, false);
}
}
}
/**
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
private function registerModuleControllers(ContainerBuilder $container): void
{
foreach ($this->getEnabledModules() as $module) {
try {
$definition = new Definition();
$definition->setAutowired(true);
$definition->setPublic(true);
$controllerDir = Module::getModuleDir($module) . '/src/Controller';
if (!is_dir($controllerDir)) {
continue;
}
$loader = new DirectoryLoader(
$container,
new FileLocator($controllerDir . '/'),
);
$loader->registerClasses(
$definition,
'SimpleSAML\\Module\\' . $module . '\\Controller\\',
$controllerDir . '/*',
);
} catch (FileLocatorFileNotFoundException $e) {
}
}
}
/**
* @return string[]
*/
private function getEnabledModules(): array
{
$modules = [];
foreach (Module::getModules() as $module) {
if (Module::isModuleEnabled($module)) {
$modules[] = $module;
}
}
return $modules;
}
}