1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Core; |
24
|
|
|
|
25
|
|
|
use Closure; |
26
|
|
|
use Syscodes\Support\Str; |
27
|
|
|
use Syscodes\Collections\Arr; |
28
|
|
|
use Syscodes\Container\Container; |
29
|
|
|
use Syscodes\Support\ServiceProvider; |
30
|
|
|
use Syscodes\Log\LoggerServiceProvider; |
31
|
|
|
use Syscodes\Events\EventServiceProvider; |
32
|
|
|
use Syscodes\Routing\RoutingServiceProvider; |
33
|
|
|
use Syscodes\Core\Http\Exceptions\HttpException; |
34
|
|
|
use Syscodes\Core\Http\Exceptions\NotFoundHttpException; |
35
|
|
|
use Syscodes\Contracts\Core\Application as ApplicationContract; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Allows the loading of service providers and functions to activate |
39
|
|
|
* routes, environments and calls of main classes. |
40
|
|
|
* |
41
|
|
|
* @author Alexander Campo <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
class Application extends Container implements ApplicationContract |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* The current globally available application. |
47
|
|
|
* |
48
|
|
|
* @var string $instance |
49
|
|
|
*/ |
50
|
|
|
protected static $instance; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Php version. |
54
|
|
|
*/ |
55
|
|
|
protected static $phpVersion = '7.3.12'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The custom application path defined by the developer. |
59
|
|
|
* |
60
|
|
|
* @var string $appPath |
61
|
|
|
*/ |
62
|
|
|
protected $appPath; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The base path for the Lenevor installation. |
66
|
|
|
* |
67
|
|
|
* @var string $basePath |
68
|
|
|
*/ |
69
|
|
|
protected $basePath; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Indicates if the application has 'booted'. |
73
|
|
|
* |
74
|
|
|
* @var bool $booted |
75
|
|
|
*/ |
76
|
|
|
protected $booted = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The array of booted callbacks. |
80
|
|
|
* |
81
|
|
|
* @var callable[] $bootedCallbacks |
82
|
|
|
*/ |
83
|
|
|
protected $bootedCallbacks = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The array of booting callbacks. |
87
|
|
|
* |
88
|
|
|
* @var callable[] $bootingCallbacks |
89
|
|
|
*/ |
90
|
|
|
protected $bootingCallbacks = []; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the current application environment. |
94
|
|
|
* |
95
|
|
|
* @var string |
96
|
|
|
*/ |
97
|
|
|
protected $env; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The custom environment path defined by the developer. |
101
|
|
|
* |
102
|
|
|
* @var string $environmentPath |
103
|
|
|
*/ |
104
|
|
|
protected $environmentPath; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The environment file to load during bootstrapping. |
108
|
|
|
* |
109
|
|
|
* @var string $environmentFile |
110
|
|
|
*/ |
111
|
|
|
protected $environmentFile = '.env'; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Indicates if the application has been bootstrapped before. |
115
|
|
|
* |
116
|
|
|
* @var bool $hasBeenBootstrapped |
117
|
|
|
*/ |
118
|
|
|
protected $hasBeenBootstrapped = false; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* The names of the loaded service providers. |
122
|
|
|
* |
123
|
|
|
* @var array $loadServiceProviders |
124
|
|
|
*/ |
125
|
|
|
protected $loadServiceProviders = []; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* All of the registered services providers. |
129
|
|
|
* |
130
|
|
|
* @var \Syscodes\Support\ServiceProvider[] $serviceProviders |
131
|
|
|
*/ |
132
|
|
|
protected $serviceProviders = []; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Constructor. Create a new Application instance. |
136
|
|
|
* |
137
|
|
|
* @param string|null $path |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function __construct($path = null) |
142
|
|
|
{ |
143
|
|
|
if ($path) |
144
|
|
|
{ |
145
|
|
|
$this->setBasePath($path); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->registerBaseBindings(); |
149
|
|
|
$this->registerBaseServiceProviders(); |
150
|
|
|
$this->registerCoreContainerAliases(); |
151
|
|
|
$this->requerimentVersion(static::$phpVersion); |
152
|
|
|
$this->getExtensionLoaded(['mbstring']); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Throw an HttpException with the given data. |
157
|
|
|
* |
158
|
|
|
* @param int $code |
159
|
|
|
* @param string $message |
160
|
|
|
* @param array $headers |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
* |
164
|
|
|
* @throws \Syscodes\Core\Http\Exceptions\NotFoundHttpException |
165
|
|
|
* @throws \Syscodes\Core\Http\Exceptions\HttpException |
166
|
|
|
*/ |
167
|
|
|
public function abort($code, $message = '', array $headers = []) |
168
|
|
|
{ |
169
|
|
|
// Convert the first letter in capital |
170
|
|
|
$message = ucfirst($message); |
171
|
|
|
|
172
|
|
|
if ($code == 404) { |
173
|
|
|
throw new NotFoundHttpException($message); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
throw new HttpException($code, $message, null, $headers); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set the base path for the application. |
181
|
|
|
* |
182
|
|
|
* @param string $path |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function setBasePath(string $path) |
187
|
|
|
{ |
188
|
|
|
$this->basePath = rtrim($path, '\/'); |
189
|
|
|
|
190
|
|
|
$this->bindContainerPaths(); |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Register all of the base service providers. |
197
|
|
|
* |
198
|
|
|
* @return void |
199
|
|
|
*/ |
200
|
|
|
protected function registerBaseServiceProviders() |
201
|
|
|
{ |
202
|
|
|
$this->register(new EventServiceProvider($this)); |
203
|
|
|
$this->register(new LoggerServiceProvider($this)); |
204
|
|
|
$this->register(new RoutingServiceProvider($this)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Bind all of the application paths in the container. |
209
|
|
|
* |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
protected function bindContainerPaths() |
213
|
|
|
{ |
214
|
|
|
$this->instance('path', $this->path()); |
215
|
|
|
$this->instance('path.base', $this->basePath()); |
216
|
|
|
$this->instance('path.lang', $this->langPath()); |
217
|
|
|
$this->instance('path.config', $this->configPath()); |
218
|
|
|
$this->instance('path.public', $this->publicPath()); |
219
|
|
|
$this->instance('path.storage', $this->storagePath()); |
220
|
|
|
$this->instance('path.database', $this->databasePath()); |
221
|
|
|
$this->instance('path.resources', $this->resourcePath()); |
222
|
|
|
$this->instance('path.bootstrap', $this->bootstrapPath()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get the path to the application "app" directory. |
227
|
|
|
* |
228
|
|
|
* @param string $path |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function path($path = '') |
232
|
|
|
{ |
233
|
|
|
$appPath = $this->basePath.DIRECTORY_SEPARATOR.'app'; |
234
|
|
|
|
235
|
|
|
return $appPath.($path ? DIRECTORY_SEPARATOR.$path : $path); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the base path of the Lenevor installation. |
240
|
|
|
* |
241
|
|
|
* @param string $path Optionally, a path to append to the base path |
242
|
|
|
* |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function basePath($path = '') |
246
|
|
|
{ |
247
|
|
|
return $this->basePath.($path ? DIRECTORY_SEPARATOR.$path : $path); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get the path to the bootstrap directory. |
252
|
|
|
* |
253
|
|
|
* @param string $path Optionally, a path to append to the bootstrap path |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function bootstrapPath($path = '') |
258
|
|
|
{ |
259
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.'bootstrap'.($path ? DIRECTORY_SEPARATOR.$path : $path); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get the path to the application configuration files. |
264
|
|
|
* |
265
|
|
|
* @param string $path Optionally, a path to append to the config path |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
public function configPath($path = '') |
270
|
|
|
{ |
271
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.'config'.($path ? DIRECTORY_SEPARATOR.$path : $path); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get the path to the database directory. |
276
|
|
|
* |
277
|
|
|
* @param string $path Optionally, a path to append to the database path |
278
|
|
|
* |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
public function databasePath($path = '') |
282
|
|
|
{ |
283
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.'database'.($path ? DIRECTORY_SEPARATOR.$path : $path); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get the path to the lang directory. |
288
|
|
|
* |
289
|
|
|
* @return string |
290
|
|
|
*/ |
291
|
|
|
public function langPath() |
292
|
|
|
{ |
293
|
|
|
return $this->resourcePath().DIRECTORY_SEPARATOR.'lang'; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get the path to the public / web directory. |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
public function publicPath() |
302
|
|
|
{ |
303
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.'public'; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get the path to the resources directory. |
308
|
|
|
* |
309
|
|
|
* @param string $path $path Optionally, a path to append to the resources path |
310
|
|
|
* @return string |
311
|
|
|
*/ |
312
|
|
|
public function resourcePath($path = '') |
313
|
|
|
{ |
314
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.'resources'.($path ? DIRECTORY_SEPARATOR.$path : $path); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get the path to the storage directory. |
319
|
|
|
* |
320
|
|
|
* @return string |
321
|
|
|
*/ |
322
|
|
|
public function storagePath() |
323
|
|
|
{ |
324
|
|
|
return $this->basePath.DIRECTORY_SEPARATOR.'storage'; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Run the given array of bootstap classes. |
329
|
|
|
* |
330
|
|
|
* @param string[] $bootstrappers |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
|
|
public function bootstrapWith(array $bootstrappers) |
335
|
|
|
{ |
336
|
|
|
$this->hasBeenBootstrapped = true; |
337
|
|
|
|
338
|
|
|
foreach ($bootstrappers as $bootstrapper) { |
339
|
|
|
$this->make($bootstrapper)->bootstrap($this); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Set the directory for the environment file. |
345
|
|
|
* |
346
|
|
|
* @param string $path |
347
|
|
|
* |
348
|
|
|
* @return $this |
349
|
|
|
*/ |
350
|
|
|
public function setEnvironmentPath($path) |
351
|
|
|
{ |
352
|
|
|
$this->environmentPath = $path; |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Get the path to the environment file directory. |
359
|
|
|
* |
360
|
|
|
* @return string |
361
|
|
|
*/ |
362
|
|
|
public function environmentPath() |
363
|
|
|
{ |
364
|
|
|
return $this->environmentPath ?: $this->basePath; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Set the environment file to be loaded during bootstrapping. |
369
|
|
|
* |
370
|
|
|
* @param string $file |
371
|
|
|
* |
372
|
|
|
* @return $this |
373
|
|
|
*/ |
374
|
|
|
public function setEnvironmentFile($file) |
375
|
|
|
{ |
376
|
|
|
$this->environmentFile = $file; |
377
|
|
|
|
378
|
|
|
return $this; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Get the environment file the application is using. |
383
|
|
|
* |
384
|
|
|
* @return string |
385
|
|
|
*/ |
386
|
|
|
public function environmentFile() |
387
|
|
|
{ |
388
|
|
|
return $this->environmentFile ?: '.env'; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Get the fully qualified path to the environment file. |
393
|
|
|
* |
394
|
|
|
* @return string |
395
|
|
|
*/ |
396
|
|
|
public function environmentFilePath() |
397
|
|
|
{ |
398
|
|
|
return $this->environmentPath().DIRECTORY_SEPARATOR.$this->environmentFile(); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Get or check the current application environment. |
403
|
|
|
* |
404
|
|
|
* @param string|array ...$environments |
405
|
|
|
* |
406
|
|
|
* @return string|bool |
407
|
|
|
*/ |
408
|
|
|
public function environment(...$environments) |
409
|
|
|
{ |
410
|
|
|
if (count($environments) > 0) { |
411
|
|
|
$pattern = is_array($environments[0]) ? $environments[0] : $environments; |
412
|
|
|
|
413
|
|
|
return Str::is($pattern, $this->env); |
|
|
|
|
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return $this->env; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Detect the application's current environment. |
421
|
|
|
* |
422
|
|
|
* @param \Closure $callback |
423
|
|
|
* |
424
|
|
|
* @return string |
425
|
|
|
*/ |
426
|
|
|
public function detectEnvironment(Closure $callback) |
427
|
|
|
{ |
428
|
|
|
return $this->env = (new EnvironmentDetector)->detect($callback); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Determine if application is in local environment. |
433
|
|
|
* |
434
|
|
|
* @return bool |
435
|
|
|
*/ |
436
|
|
|
public function isLocal() |
437
|
|
|
{ |
438
|
|
|
return $this->env === 'local'; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Determine if application is in production environment. |
443
|
|
|
* |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
|
|
public function isProduction() |
447
|
|
|
{ |
448
|
|
|
return $this->env === 'production'; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Determine if the application is unit tests. |
453
|
|
|
* |
454
|
|
|
* @return bool |
455
|
|
|
*/ |
456
|
|
|
public function isUnitTests() |
457
|
|
|
{ |
458
|
|
|
return $this->env === 'testing'; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* You can load different configurations depending on your |
463
|
|
|
* current environment. Setting the environment also influences |
464
|
|
|
* things like logging and error reporting. |
465
|
|
|
* |
466
|
|
|
* This can be set to anything, but default usage is: |
467
|
|
|
* |
468
|
|
|
* local (development) |
469
|
|
|
* testing |
470
|
|
|
* production |
471
|
|
|
* |
472
|
|
|
* @return string |
473
|
|
|
*/ |
474
|
|
|
public function bootEnvironment() |
475
|
|
|
{ |
476
|
|
|
if (file_exists(SYS_PATH.'src'.DIRECTORY_SEPARATOR.'environment'.DIRECTORY_SEPARATOR.$this->environment().'.php')) { |
477
|
|
|
require_once SYS_PATH.'src'.DIRECTORY_SEPARATOR.'environment'.DIRECTORY_SEPARATOR.$this->environment().'.php'; |
478
|
|
|
} else { |
479
|
|
|
header('HTTP/1.1 503 Service Unavailable.', true, 503); |
480
|
|
|
print('<style> |
481
|
|
|
body { |
482
|
|
|
align-items: center; |
483
|
|
|
background: #FBFCFC; |
484
|
|
|
display: flex; |
485
|
|
|
font-family: verdana, sans-seif; |
486
|
|
|
font-size: .9em; |
487
|
|
|
font-weight: 600; |
488
|
|
|
justify-content: center; |
489
|
|
|
} |
490
|
|
|
p { |
491
|
|
|
background: #F0F3F4 ; |
492
|
|
|
border-radius: 5px; |
493
|
|
|
box-shadow: 0 1px 4px #333333; |
494
|
|
|
color: #34495E; |
495
|
|
|
padding: 10px; |
496
|
|
|
text-align: center; |
497
|
|
|
text-shadow: 0 1px 0 #424949; |
498
|
|
|
width: 25%; |
499
|
|
|
} |
500
|
|
|
</style> |
501
|
|
|
<p>The application environment is not set correctly.</p>'); |
502
|
|
|
exit; // EXIT_ERROR |
|
|
|
|
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Determine if the application has been bootstrapped before. |
508
|
|
|
* |
509
|
|
|
* @return bool |
510
|
|
|
*/ |
511
|
|
|
public function hasBeenBootstrapped() |
512
|
|
|
{ |
513
|
|
|
return $this->hasBeenBootstrapped; |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* You can empty out this file, if you are certain that you match all requirements. |
518
|
|
|
* You can remove this if you are confident that your PHP version is sufficient. |
519
|
|
|
* |
520
|
|
|
* @return string |
521
|
|
|
*/ |
522
|
|
|
protected function requerimentVersion($version) |
523
|
|
|
{ |
524
|
|
|
if (version_compare(PHP_VERSION, $version) < 0) { |
525
|
|
|
if (PHP_SAPI == 'cli') { |
526
|
|
|
$string = "\033[1;36m"; |
527
|
|
|
$string .= "$version\033[0m"; |
528
|
|
|
trigger_error("Your PHP version must be equal or higher than {$string} to use Lenevor Framework.".PHP_EOL, E_USER_ERROR); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
exit("Your PHP version must be equal or higher than <b>{$version}</b> to use Lenevor Framework."); |
|
|
|
|
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* You can remove this if you are confident you have mbstring installed. |
537
|
|
|
* |
538
|
|
|
* @return string |
539
|
|
|
*/ |
540
|
|
|
protected function getExtensionLoaded(array $extensionLoaded) |
541
|
|
|
{ |
542
|
|
|
foreach ($extensionLoaded as $value) { |
543
|
|
|
if ( ! extension_loaded($value)) { |
544
|
|
|
if (PHP_SAPI == 'cli') { |
545
|
|
|
$string = "\033[1;36m"; |
546
|
|
|
$string .= "$value\033[0m"; |
547
|
|
|
trigger_error("You must enable the {$string} extension to use Lenevor Framework.".PHP_EOL, E_USER_ERROR); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
exit("You must enable the <b>{$value}</b> extension to use Lenevor Framework."); |
|
|
|
|
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Resolve the given type from the container. |
557
|
|
|
* |
558
|
|
|
* (Overriding Container::make) |
559
|
|
|
* |
560
|
|
|
* @param string $id |
561
|
|
|
* @param array $parameters |
562
|
|
|
* |
563
|
|
|
* @return mixed |
564
|
|
|
*/ |
565
|
|
|
public function make($id, array $parameters = []) |
566
|
|
|
{ |
567
|
|
|
$id = $this->getAlias($id); |
568
|
|
|
|
569
|
|
|
return parent::make($id, $parameters); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Register all of the configured providers. |
574
|
|
|
* |
575
|
|
|
* @return void |
576
|
|
|
*/ |
577
|
|
|
public function registerConfiguredProviders() |
578
|
|
|
{ |
579
|
|
|
(new ProviderRepository($this)) |
580
|
|
|
->load($this['config']->get('services.providers')); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* Register a service provider. |
585
|
|
|
* |
586
|
|
|
* @param \Syscodes\Support\ServiceProvider|string $provider |
587
|
|
|
* @param bool $force |
588
|
|
|
* |
589
|
|
|
* @return \Syscodes\Support\ServiceProvider |
590
|
|
|
*/ |
591
|
|
|
public function register($provider, $force = false) |
592
|
|
|
{ |
593
|
|
|
if (($registered = $this->getProviderHasBeenLoaded($provider)) && ! $force) { |
594
|
|
|
return $registered; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
if (is_string($provider)) { |
598
|
|
|
$provider = $this->resolveProviderClass($provider); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
$provider->register(); |
602
|
|
|
|
603
|
|
|
$this->markAsRegistered($provider); |
604
|
|
|
|
605
|
|
|
return $provider; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Get the registered service provider instance if it exists. |
610
|
|
|
* |
611
|
|
|
* @param \Syscodes\Support\ServiceProvider|string $provider |
612
|
|
|
* |
613
|
|
|
* @return \Syscodes\Support\ServiceProvider |
614
|
|
|
*/ |
615
|
|
|
protected function getProviderHasBeenLoaded($provider) |
616
|
|
|
{ |
617
|
|
|
$name = is_string($provider) ? $provider : get_class($provider); |
618
|
|
|
|
619
|
|
|
if (array_key_exists($name, $this->loadServiceProviders)) { |
620
|
|
|
return Arr::first($this->serviceProviders, function($key, $value) use ($name) { |
621
|
|
|
return get_class($value) == $name; |
622
|
|
|
}); |
623
|
|
|
} |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Resolve a service provider instance from the class name. |
628
|
|
|
* |
629
|
|
|
* @param string $provider |
630
|
|
|
* |
631
|
|
|
* @return \Syscodes\Support\ServiceProvider |
632
|
|
|
*/ |
633
|
|
|
public function resolveProviderClass($provider) |
634
|
|
|
{ |
635
|
|
|
return new $provider($this); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Mark the given provider as registered. |
640
|
|
|
* |
641
|
|
|
* @param \Syscodes\Support\ServiceProvider $provider |
642
|
|
|
* |
643
|
|
|
* @return void |
644
|
|
|
*/ |
645
|
|
|
protected function markAsRegistered($provider) |
646
|
|
|
{ |
647
|
|
|
$this->serviceProviders[] = $provider; |
648
|
|
|
|
649
|
|
|
$this->loadServiceProviders[get_class($provider)] = true; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Determine if the given id type has been bound. |
654
|
|
|
* |
655
|
|
|
* @param string $id |
656
|
|
|
* |
657
|
|
|
* @return bool |
658
|
|
|
*/ |
659
|
|
|
public function bound($id) |
660
|
|
|
{ |
661
|
|
|
return parent::bound($id); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Determine if the application has booted. |
666
|
|
|
* |
667
|
|
|
* @return bool |
668
|
|
|
*/ |
669
|
|
|
public function isBooted() |
670
|
|
|
{ |
671
|
|
|
return $this->booted; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
/** |
675
|
|
|
* Boot the application´s service providers. |
676
|
|
|
* |
677
|
|
|
* @return void |
678
|
|
|
*/ |
679
|
|
|
public function boot() |
680
|
|
|
{ |
681
|
|
|
if ($this->isbooted()) { |
682
|
|
|
return; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
$this->bootAppCallbacks($this->bootingCallbacks); |
686
|
|
|
|
687
|
|
|
array_walk($this->serviceProviders, function ($provider) { |
688
|
|
|
$this->bootProviderClass($provider); |
689
|
|
|
}); |
690
|
|
|
|
691
|
|
|
$this->booted = true; |
692
|
|
|
|
693
|
|
|
$this->bootAppCallbacks($this->bootedCallbacks); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* Call the booting callbacks for the application. |
698
|
|
|
* |
699
|
|
|
* @param callable[] $callbacks |
700
|
|
|
* |
701
|
|
|
* @return void |
702
|
|
|
*/ |
703
|
|
|
protected function bootAppCallbacks(array $callbacks) |
704
|
|
|
{ |
705
|
|
|
foreach ($callbacks as $callback) { |
706
|
|
|
$callback($this); |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
/** |
711
|
|
|
* Boot the given service provider. |
712
|
|
|
* |
713
|
|
|
* @param \Syscodes\Support\ServiceProvider $provider |
714
|
|
|
* |
715
|
|
|
* @return mixed |
716
|
|
|
*/ |
717
|
|
|
protected function bootProviderClass(ServiceProvider $provider) |
718
|
|
|
{ |
719
|
|
|
if (method_exists($provider, 'boot')) |
720
|
|
|
{ |
721
|
|
|
$provider->boot(); |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Register a new boot listener. |
727
|
|
|
* |
728
|
|
|
* @param callable $callback |
729
|
|
|
* |
730
|
|
|
* @return void |
731
|
|
|
*/ |
732
|
|
|
public function booting($callback) |
733
|
|
|
{ |
734
|
|
|
$this->bootingCallbacks[] = $callback; |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* Register a new 'booted' listener. |
739
|
|
|
* |
740
|
|
|
* @param callable $callback |
741
|
|
|
* |
742
|
|
|
* @return void |
743
|
|
|
*/ |
744
|
|
|
public function booted($callback) |
745
|
|
|
{ |
746
|
|
|
$this->bootedCallbacks[] = $callback; |
747
|
|
|
|
748
|
|
|
if ($this->isBooted()) { |
749
|
|
|
$this->bootAppCallbacks([$callback]); |
750
|
|
|
} |
751
|
|
|
} |
752
|
|
|
|
753
|
|
|
/** |
754
|
|
|
* Get the current application locale. |
755
|
|
|
* |
756
|
|
|
* @return string |
757
|
|
|
*/ |
758
|
|
|
public function getLocale() |
759
|
|
|
{ |
760
|
|
|
return $this['config']->get('app.locale'); |
761
|
|
|
} |
762
|
|
|
|
763
|
|
|
/** |
764
|
|
|
* Get the current application fallback locale. |
765
|
|
|
* |
766
|
|
|
* @return string |
767
|
|
|
*/ |
768
|
|
|
public function getFallbackLocale() |
769
|
|
|
{ |
770
|
|
|
return $this['config']->get('app.fallbackLocale'); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
/** |
774
|
|
|
* Determine if application locale is the given locale. |
775
|
|
|
* |
776
|
|
|
* @param string $locale |
777
|
|
|
* |
778
|
|
|
* @return bool |
779
|
|
|
*/ |
780
|
|
|
public function isLocale($locale) |
781
|
|
|
{ |
782
|
|
|
return $this->getLocale() == $locale; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Register the basic bindings into the container. |
787
|
|
|
* |
788
|
|
|
* @return void |
789
|
|
|
*/ |
790
|
|
|
public function registerBaseBindings() |
791
|
|
|
{ |
792
|
|
|
static::setInstance($this); |
793
|
|
|
|
794
|
|
|
$this->instance('app', $this); |
795
|
|
|
|
796
|
|
|
$this->instance('config', $this[\Syscodes\Config\Configure::class]); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* Register the core class aliases in the container. |
801
|
|
|
* |
802
|
|
|
* @return void |
803
|
|
|
*/ |
804
|
|
|
public function registerCoreContainerAliases() |
805
|
|
|
{ |
806
|
|
|
foreach ([ |
807
|
|
|
'app' => [self::class, \Syscodes\Contracts\Container\Container::class, \Syscodes\Contracts\Core\Application::class, \Psr\Container\ContainerInterface::class], |
808
|
|
|
'cache' => [\Syscodes\Cache\CacheManager::class, \Syscodes\Contracts\Cache\Manager::class], |
809
|
|
|
'cache.store' => [\Syscodes\Cache\CacheRepository::class], |
810
|
|
|
'config' => [\Syscodes\Config\Configure::class, \Syscodes\Contracts\Config\Configure::class], |
811
|
|
|
'db' => [\Syscodes\Database\DatabaseManager::class, \Syscodes\Database\ConnectionResolverInterface::class], |
812
|
|
|
'db.connection' => [\Syscodes\Database\Connection::class, \Syscodes\Database\ConnectionInterface::class], |
813
|
|
|
'encrypter' => [\Syscodes\Encryption\Encrypter::class, \Syscodes\Contracts\Encryption\Encrypter::class], |
814
|
|
|
'events' => [\Syscodes\Events\Dispatcher::class, \Syscodes\Contracts\Events\Dispatcher::class], |
815
|
|
|
'files' => [\Syscodes\Filesystem\Filesystem::class], |
816
|
|
|
'log' => [\Syscodes\Log\LogManager::class, \Psr\Log\LoggerInterface::class], |
817
|
|
|
'plaze.transpiler' => [\Syscodes\View\Transpilers\PlazeTranspiler::class], |
818
|
|
|
'redirect' => [\Syscodes\Routing\Redirector::class], |
819
|
|
|
'redis' => [\Syscodes\Redis\RedisManager::class], |
820
|
|
|
'request' => [\Syscodes\Http\Request::class], |
821
|
|
|
'router' => [\Syscodes\Routing\Router::class], |
822
|
|
|
'session' => [\Syscodes\Session\SessionManager::class], |
823
|
|
|
'session.store' => [\Syscodes\Session\Store::class, \Syscodes\Contracts\Session\Session::class], |
824
|
|
|
'translator' => [\Syscodes\Translation\Translator::class], |
825
|
|
|
'url' => [\Syscodes\Routing\UrlGenerator::class], |
826
|
|
|
'view' => [\Syscodes\View\Factory::class, \Syscodes\Contracts\View\Factory::class] |
827
|
|
|
] as $key => $aliases) { |
828
|
|
|
foreach ((array) $aliases as $alias) { |
829
|
|
|
$this->alias($key, $alias); |
830
|
|
|
} |
831
|
|
|
} |
832
|
|
|
} |
833
|
|
|
} |