1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jellyfish\Kernel; |
6
|
|
|
|
7
|
|
|
use ArrayObject; |
8
|
|
|
use Jellyfish\Kernel\Exception\EnvVarNotSetException; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
|
11
|
|
|
class Kernel implements KernelInterface |
12
|
|
|
{ |
13
|
|
|
protected const SERVICE_PROVIDERS_FILE_NAME = 'service_providers.php'; |
14
|
|
|
protected const APP_DIRECTORY_NAME = 'app'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \Pimple\Container |
18
|
|
|
*/ |
19
|
|
|
protected $container; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $rootDir; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $appDir; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $environment; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $rootDir |
38
|
|
|
* |
39
|
|
|
* @throws \Jellyfish\Kernel\Exception\EnvVarNotSetException |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $rootDir) |
42
|
|
|
{ |
43
|
|
|
$this->rootDir = \rtrim($rootDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
44
|
|
|
$this->appDir = $this->rootDir . static::APP_DIRECTORY_NAME . DIRECTORY_SEPARATOR; |
45
|
|
|
$this->environment = $this->buildEnvironment(); |
46
|
|
|
$this->container = $this->buildContainer(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
* |
52
|
|
|
* @throws \Jellyfish\Kernel\Exception\EnvVarNotSetException |
53
|
|
|
*/ |
54
|
|
|
protected function buildEnvironment(): string |
55
|
|
|
{ |
56
|
|
|
$environment = \getenv('APPLICATION_ENV', true) ?: \getenv('APPLICATION_ENV'); |
57
|
|
|
|
58
|
|
|
if (!$environment) { |
59
|
|
|
throw new EnvVarNotSetException('Environment variable "APPLICATION_ENV" is not set.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $environment; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Pimple\Container |
67
|
|
|
*/ |
68
|
|
|
protected function buildContainer(): Container |
69
|
|
|
{ |
70
|
|
|
$container = new Container([ |
71
|
|
|
'root_dir' => $this->rootDir, |
72
|
|
|
'app_dir' => $this->appDir, |
73
|
|
|
'environment' => $this->environment, |
74
|
|
|
'commands' => function () { |
75
|
|
|
return []; |
76
|
|
|
} |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$serviceProviders = $this->buildServiceProviders(); |
80
|
|
|
|
81
|
|
|
foreach ($serviceProviders as $serviceProvider) { |
82
|
|
|
$container->register($serviceProvider); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $container; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return ArrayObject |
90
|
|
|
*/ |
91
|
|
|
protected function buildServiceProviders(): ArrayObject |
92
|
|
|
{ |
93
|
|
|
$serviceProviders = new ArrayObject(); |
94
|
|
|
$pathToServiceProvidersFile = $this->appDir . static::SERVICE_PROVIDERS_FILE_NAME; |
95
|
|
|
|
96
|
|
|
if (\file_exists($pathToServiceProvidersFile)) { |
97
|
|
|
include $pathToServiceProvidersFile; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $serviceProviders; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return \Pimple\Container |
105
|
|
|
*/ |
106
|
|
|
public function getContainer(): Container |
107
|
|
|
{ |
108
|
|
|
return $this->container; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|