1 | <?php declare(strict_types = 1); |
||
29 | abstract class AbstractKernel implements Kernel |
||
30 | { |
||
31 | const VERSION = '0.1.0'; |
||
32 | |||
33 | /** |
||
34 | * Service container class name. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $containerClass = \Venta\Container\Container::class; |
||
39 | |||
40 | /** |
||
41 | * @inheritDoc |
||
42 | */ |
||
43 | public function boot(): Container |
||
44 | { |
||
45 | $container = $this->initServiceContainer(); |
||
46 | |||
47 | foreach ($this->getBootstraps() as $bootstrapClass) { |
||
48 | $this->invokeBootstrap($bootstrapClass, $container); |
||
49 | } |
||
50 | |||
51 | // Here we boot service providers on by one. The correct order is ensured by resolver. |
||
52 | /** @var ServiceProviderDependencyResolver $resolver */ |
||
53 | $resolver = $container->get(ServiceProviderDependencyResolver::class); |
||
54 | $configBuilder = $this->createConfigurationBuilder($container); |
||
55 | foreach ($resolver($this->registerServiceProviders()) as $providerClass) { |
||
56 | $this->bootServiceProvider($providerClass, $container, $configBuilder); |
||
57 | } |
||
58 | |||
59 | // When all service providers have been booted |
||
60 | // we can be sure that all possible config changes were already made. |
||
61 | // At this point we are creating Config class instance from Config Builder. |
||
62 | $container->bindInstance(Config::class, $configBuilder->build()); |
||
63 | |||
64 | return $container; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | public function environment(): string |
||
71 | { |
||
72 | return getenv('APP_ENV') ?: 'local'; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | abstract public function rootPath(): string; |
||
79 | |||
80 | /** |
||
81 | * @inheritDoc |
||
82 | */ |
||
83 | public function version(): string |
||
84 | { |
||
85 | return self::VERSION; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | public function isCli(): bool |
||
95 | |||
96 | /** |
||
97 | * Boots service provider with base config. |
||
98 | * |
||
99 | * @param string $providerClass |
||
100 | * @param Container $container |
||
101 | * @param ConfigBuilderContract $configBuilder |
||
102 | * @throws InvalidArgumentException |
||
103 | */ |
||
104 | protected function bootServiceProvider(string $providerClass, Container $container, ConfigBuilderContract $configBuilder) |
||
112 | |||
113 | /** |
||
114 | * Returns list of kernel bootstraps. |
||
115 | * This is a main place to tune default kernel behavior. |
||
116 | * Change carefully, as it may cause kernel failure. |
||
117 | * |
||
118 | * @return string[] |
||
119 | */ |
||
120 | protected function getBootstraps(): array |
||
136 | |||
137 | /** |
||
138 | * Invokes kernel bootstrap. |
||
139 | * This is the point where specific kernel functionality defined by bootstrap is enabled. |
||
140 | * |
||
141 | * @param string $bootstrapClass |
||
142 | * @param Container $container |
||
143 | * @throws InvalidArgumentException |
||
144 | */ |
||
145 | protected function invokeBootstrap(string $bootstrapClass, Container $container) |
||
151 | |||
152 | /** |
||
153 | * Returns a list of all registered service providers. |
||
154 | * |
||
155 | * @return string[] |
||
156 | */ |
||
157 | abstract protected function registerServiceProviders(): array; |
||
158 | |||
159 | /** |
||
160 | * Adds default service inflections. |
||
161 | * |
||
162 | * @param Container $container |
||
163 | */ |
||
164 | private function addDefaultInflections(Container $container) |
||
170 | |||
171 | /** |
||
172 | * Binds default services to container. |
||
173 | * |
||
174 | * @param Container $container |
||
175 | */ |
||
176 | private function bindDefaultServices(Container $container) |
||
183 | |||
184 | /** |
||
185 | * Ensures bootstrap class extends abstract kernel bootstrap. |
||
186 | * |
||
187 | * @param string $bootstrapClass |
||
188 | * @throws InvalidArgumentException |
||
189 | */ |
||
190 | private function ensureBootstrap(string $bootstrapClass) |
||
198 | |||
199 | /** |
||
200 | * Ensures service provider implements contract. |
||
201 | * |
||
202 | * @param string $providerClass |
||
203 | * @throws InvalidArgumentException |
||
204 | */ |
||
205 | private function ensureServiceProvider(string $providerClass) |
||
213 | |||
214 | /** |
||
215 | * Initializes service container. |
||
216 | */ |
||
217 | private function initServiceContainer(): Container |
||
227 | |||
228 | /** |
||
229 | * Creates and returns configuration builder instance. |
||
230 | * |
||
231 | * @param Container $container |
||
232 | * @return ConfigBuilderContract |
||
233 | */ |
||
234 | private function createConfigurationBuilder(Container $container) |
||
242 | } |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.