|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file Application.php |
|
34
|
|
|
* |
|
35
|
|
|
* The Platine Application class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\App |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\App; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Config\Config; |
|
51
|
|
|
use Platine\Config\FileLoader; |
|
52
|
|
|
use Platine\Container\Container; |
|
53
|
|
|
use Platine\Framework\Service\Provider\BaseServiceProvider; |
|
54
|
|
|
use Platine\Framework\Service\Provider\EventServiceProvider; |
|
55
|
|
|
use Platine\Framework\Service\Provider\LoggerServiceProvider; |
|
56
|
|
|
use Platine\Framework\Service\Provider\RoutingServiceProvider; |
|
57
|
|
|
use Platine\Framework\Service\ServiceProvider; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* class Application |
|
61
|
|
|
* @package Platine\Framework\App |
|
62
|
|
|
*/ |
|
63
|
|
|
class Application extends Container |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The application version |
|
68
|
|
|
*/ |
|
69
|
|
|
public const VERSION = '1.0.0-dev'; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The base path for this application |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected string $basePath = ''; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* The application configuration path |
|
79
|
|
|
* @var string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected string $configPath = 'config'; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The list of service providers |
|
85
|
|
|
* @var array<string, ServiceProvider> |
|
86
|
|
|
*/ |
|
87
|
|
|
protected array $providers = []; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Whether the system already booted |
|
91
|
|
|
* @var bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected bool $booted = false; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The application environment |
|
97
|
|
|
* @var string |
|
98
|
|
|
*/ |
|
99
|
|
|
protected string $env = 'dev'; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Create new instance |
|
103
|
|
|
* @param string $basePath |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct(string $basePath = '') |
|
106
|
|
|
{ |
|
107
|
|
|
parent::__construct(); |
|
108
|
|
|
$this->basePath = $basePath; |
|
109
|
|
|
$this->loadCoreServiceProviders(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return the application version |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function version(): string |
|
117
|
|
|
{ |
|
118
|
|
|
return self::VERSION; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Return the application base path |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getBasePath(): string |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->basePath; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Return the application configuration path |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getConfigPath(): string |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->configPath; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set the application base path |
|
141
|
|
|
* @param string $basePath |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setBasePath(string $basePath): self |
|
145
|
|
|
{ |
|
146
|
|
|
$this->basePath = $basePath; |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Set the application configuration path |
|
153
|
|
|
* @param string $configPath |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setConfigPath(string $configPath): self |
|
157
|
|
|
{ |
|
158
|
|
|
$this->configPath = $configPath; |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Boot the application |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
public function boot(): void |
|
168
|
|
|
{ |
|
169
|
|
|
if ($this->booted) { |
|
170
|
|
|
return; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
foreach ($this->providers as $provider) { |
|
174
|
|
|
$this->bootServiceProvider($provider); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$this->booted = true; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Register the service provider |
|
182
|
|
|
* @param string|ServiceProvider $provider |
|
183
|
|
|
* @param bool $force whether to force registration of provider |
|
184
|
|
|
* if already loaded |
|
185
|
|
|
* @return ServiceProvider |
|
186
|
|
|
*/ |
|
187
|
|
|
public function registerServiceProvider( |
|
188
|
|
|
$provider, |
|
189
|
|
|
bool $force = false |
|
190
|
|
|
): ServiceProvider { |
|
191
|
|
|
$registered = $this->getServiceProvider($provider); |
|
192
|
|
|
if ($registered && !$force) { |
|
193
|
|
|
return $registered; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (is_string($provider)) { |
|
197
|
|
|
$provider = $this->createServiceProvider($provider); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$provider->register(); |
|
201
|
|
|
|
|
202
|
|
|
$this->markProviderAsRegistered($provider); |
|
203
|
|
|
|
|
204
|
|
|
if ($this->booted) { |
|
205
|
|
|
$this->bootServiceProvider($provider); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $provider; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Return the registered service provider if exist |
|
213
|
|
|
* @param string|ServiceProvider $provider |
|
214
|
|
|
* @return ServiceProvider|null |
|
215
|
|
|
*/ |
|
216
|
|
|
public function getServiceProvider($provider): ?ServiceProvider |
|
217
|
|
|
{ |
|
218
|
|
|
$name = is_string($provider) |
|
219
|
|
|
? $provider |
|
220
|
|
|
: get_class($provider); |
|
221
|
|
|
|
|
222
|
|
|
return $this->providers[$name] ?? null; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Load configured service providers |
|
227
|
|
|
* @return void |
|
228
|
|
|
*/ |
|
229
|
|
|
public function registerConfiguredServiceProviders(): void |
|
230
|
|
|
{ |
|
231
|
|
|
/** @template T @var Config<T> $config */ |
|
232
|
|
|
$config = $this->get(Config::class); |
|
233
|
|
|
|
|
234
|
|
|
/** @var string[] $providers */ |
|
235
|
|
|
$providers = $config->get('providers', []); |
|
236
|
|
|
foreach ($providers as $provider) { |
|
237
|
|
|
$this->registerServiceProvider($provider); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Load the application configuration |
|
243
|
|
|
* @return void |
|
244
|
|
|
*/ |
|
245
|
|
|
public function registerConfiguration(): void |
|
246
|
|
|
{ |
|
247
|
|
|
$loader = new FileLoader($this->getConfigPath()); |
|
248
|
|
|
$config = new Config($loader, $this->env); |
|
249
|
|
|
$this->instance($loader); |
|
250
|
|
|
$this->instance($config); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Create service provider |
|
255
|
|
|
* @param string $provider |
|
256
|
|
|
* @return ServiceProvider |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function createServiceProvider(string $provider): ServiceProvider |
|
259
|
|
|
{ |
|
260
|
|
|
return new $provider($this); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Boot the given service provider |
|
265
|
|
|
* @param ServiceProvider $provider |
|
266
|
|
|
* @return void |
|
267
|
|
|
*/ |
|
268
|
|
|
protected function bootServiceProvider(ServiceProvider $provider): void |
|
269
|
|
|
{ |
|
270
|
|
|
$provider->boot(); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Set the given service provider as registered |
|
275
|
|
|
* @param ServiceProvider $provider |
|
276
|
|
|
* @return void |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function markProviderAsRegistered(ServiceProvider $provider): void |
|
279
|
|
|
{ |
|
280
|
|
|
$this->providers[get_class($provider)] = $provider; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Load framework core service providers |
|
285
|
|
|
* @return void |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function loadCoreServiceProviders(): void |
|
288
|
|
|
{ |
|
289
|
|
|
$this->registerServiceProvider(new BaseServiceProvider($this)); |
|
290
|
|
|
$this->registerServiceProvider(new LoggerServiceProvider($this)); |
|
291
|
|
|
$this->registerServiceProvider(new EventServiceProvider($this)); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|