|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Thunder micro CLI framework. |
|
7
|
|
|
* (c) Jérémy Marodon <[email protected]> |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace RxThunder\Core\Kernel; |
|
13
|
|
|
|
|
14
|
|
|
use RxThunder\Core\Kernel\Exception\KernelNotBootedException; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
|
18
|
|
|
use Symfony\Component\Dotenv\Dotenv; |
|
19
|
|
|
|
|
20
|
|
|
class Kernel implements KernelInterface |
|
21
|
|
|
{ |
|
22
|
|
|
public const NAME = 'Thunder'; |
|
23
|
|
|
public const VERSION = '1.0.1'; |
|
24
|
|
|
public const VERSION_ID = 10001; |
|
25
|
|
|
public const MAJOR_VERSION = 1; |
|
26
|
|
|
public const MINOR_VERSION = 0; |
|
27
|
|
|
public const RELEASE_VERSION = 1; |
|
28
|
|
|
public const EXTRA_VERSION = ''; |
|
29
|
|
|
|
|
30
|
|
|
protected string $environment; |
|
31
|
|
|
protected ContainerBuilder $container; |
|
32
|
|
|
protected bool $booted = false; |
|
33
|
|
|
protected bool $debug; |
|
34
|
|
|
protected string $project_dir; |
|
35
|
|
|
|
|
36
|
5 |
|
public function __construct(string $environment, bool $debug = false) |
|
37
|
|
|
{ |
|
38
|
5 |
|
$this->environment = $environment; |
|
39
|
5 |
|
$this->debug = $debug; |
|
40
|
5 |
|
$this->project_dir = $this->initiateProjectDir(); |
|
41
|
5 |
|
$this->container = new ContainerBuilder(); |
|
42
|
5 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function boot(): void |
|
45
|
|
|
{ |
|
46
|
2 |
|
if ($this->booted) { |
|
47
|
1 |
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
$this->initializeContainer(); |
|
51
|
|
|
|
|
52
|
2 |
|
$this->booted = true; |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
protected function initializeContainer(): void |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->loadEnvironment(); |
|
58
|
2 |
|
$this->loadDefinitions(); |
|
59
|
|
|
|
|
60
|
2 |
|
$this->container->compile(); |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
protected function loadDefinitions(): void |
|
64
|
|
|
{ |
|
65
|
2 |
|
$this->container->setParameter('thunder.environment', $this->getEnvironment()); |
|
66
|
2 |
|
$this->container->setParameter('thunder.debug', $this->debugActivated()); |
|
67
|
2 |
|
$this->container->setParameter('thunder.project_dir', $this->getProjectDir()); |
|
68
|
2 |
|
$this->container->setParameter('thunder.config_dir', $this->getConfigDir()); |
|
69
|
|
|
|
|
70
|
|
|
$internal_loader = |
|
71
|
2 |
|
new PhpFileLoader( |
|
72
|
2 |
|
$this->container, |
|
73
|
2 |
|
new FileLocator(__DIR__ . '/../../config') |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$personal_loader = |
|
77
|
2 |
|
new PhpFileLoader( |
|
78
|
2 |
|
$this->container, |
|
79
|
2 |
|
new FileLocator($this->getConfigDir()) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
2 |
|
$internal_loader->load('console.php'); |
|
83
|
2 |
|
$internal_loader->load('container.php'); |
|
84
|
2 |
|
$internal_loader->load('router.php'); |
|
85
|
2 |
|
$internal_loader->load('logger.php'); |
|
86
|
|
|
|
|
87
|
2 |
|
$personal_loader->load('parameters.php'); |
|
88
|
2 |
|
$personal_loader->load('services.php'); |
|
89
|
2 |
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
protected function loadEnvironment(): void |
|
92
|
|
|
{ |
|
93
|
2 |
|
(new Dotenv())->populate(['APP_ENV' => $this->getEnvironment()]); |
|
94
|
2 |
|
(new Dotenv())->loadEnv($this->getProjectDir() . '/.env'); |
|
95
|
2 |
|
} |
|
96
|
|
|
|
|
97
|
5 |
|
protected function initiateProjectDir(): string |
|
98
|
|
|
{ |
|
99
|
5 |
|
if (isset($this->project_dir)) { |
|
100
|
1 |
|
throw new \LogicException('Project directory is already defined'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
$reflected = new \ReflectionObject($this); |
|
104
|
5 |
|
if (!$dir = $reflected->getFileName()) { |
|
105
|
|
|
throw new \LogicException(sprintf('Cannot get filename of class "%s".', $reflected->name)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
if (!file_exists($dir)) { |
|
109
|
|
|
throw new \LogicException(sprintf('File of class donʼt exists "%s".', $reflected->name)); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
$dir = $root_dir = \dirname($dir); |
|
113
|
5 |
|
while (!file_exists($dir . '/composer.lock')) { |
|
114
|
5 |
|
if ($dir === \dirname($dir)) { |
|
115
|
|
|
return $root_dir; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
5 |
|
$dir = \dirname($dir); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
5 |
|
return $dir; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
public function getProjectDir(): string |
|
125
|
|
|
{ |
|
126
|
1 |
|
return $this->project_dir; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
public function getConfigDir(): string |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->project_dir . '/config'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
public function getEnvironment(): string |
|
135
|
|
|
{ |
|
136
|
2 |
|
return $this->environment; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @throws KernelNotBootedException |
|
141
|
|
|
*/ |
|
142
|
2 |
|
public function getContainer(): ContainerBuilder |
|
143
|
|
|
{ |
|
144
|
2 |
|
if (!$this->booted) { |
|
145
|
1 |
|
throw new KernelNotBootedException(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
return $this->container; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
2 |
|
public function debugActivated(): bool |
|
152
|
|
|
{ |
|
153
|
2 |
|
return $this->debug; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
public function booted(): bool |
|
157
|
|
|
{ |
|
158
|
1 |
|
return $this->booted; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|