This repository was archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKernel.php
112 lines (87 loc) · 2.66 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
<?php
namespace Harmony\Core\Module\Kernel;
use DI\Container;
use DI\ContainerBuilder;
use Exception;
use Harmony\Core\Module\Config\Env\DotEnvPathsContainerInterface;
use Harmony\Core\Module\Config\ModulesToLoadInterface;
use Harmony\Core\Module\Config\ProviderInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class Kernel {
/** @var ProviderInterface[] */
protected array $modules = [];
protected Container $diContainer;
/** @var class-string<Command>[] */
protected array $commands = [];
protected RouteCollection $routes;
protected UrlGenerator $urlGenerator;
protected RequestContext $context;
public function __construct(
protected ?DotEnvPathsContainerInterface $dotEnvs = null,
protected ?ModulesToLoadInterface $modulesToLoad = null,
) {
$this->loadEnv();
$this->loadModules();
$this->loadDI();
$this->loadCommands();
$this->loadRouting();
}
protected function loadEnv(): void {
if ($this->dotEnvs === null) {
return;
}
$dotenv = new Dotenv();
$dotEnvPaths = $this->dotEnvs->getEnvPaths();
foreach ($dotEnvPaths as $path) {
$dotenv->load($path->value);
unset($path);
}
}
protected function loadModules(): void {
if ($this->modulesToLoad === null) {
return;
}
foreach ($this->modulesToLoad->getProviders() as $module) {
$this->modules[] = new $module();
unset($module);
}
}
/**
* @throws Exception
*/
protected function loadDI(): void {
$diBuilder = new ContainerBuilder();
foreach ($this->modules as $module) {
$definitions = $module->getResolverDefinitions();
$diBuilder->addDefinitions($definitions);
unset($definitions);
}
$this->diContainer = $diBuilder->build();
}
protected function loadCommands(): void {
foreach ($this->modules as $module) {
$commands = $module->getCommands();
$this->commands += $commands;
unset($commands);
}
}
protected function loadRouting(): void {
$this->routes = new RouteCollection();
foreach ($this->modules as $module) {
$routes = $module->getRoutes();
foreach ($routes as $route) {
$symfonyRoute = new Route($route->uri, [
"_controller" => $route->controllerAction,
]);
$this->routes->add($route->name, $symfonyRoute);
}
}
$this->context = new RequestContext();
$this->urlGenerator = new UrlGenerator($this->routes, $this->context);
}
}