|
5 | 5 | namespace Bow\Configuration; |
6 | 6 |
|
7 | 7 | use ArrayAccess; |
8 | | -use Bow\Event\Event; |
9 | 8 | use Bow\Container\Capsule; |
10 | 9 | use Bow\Support\Arraydotify; |
11 | 10 | use Bow\Session\SessionConfiguration; |
12 | 11 | use Bow\Configuration\EnvConfiguration; |
13 | | -use Bow\Container\ContainerConfiguration; |
14 | 12 | use Bow\Application\Exception\ApplicationException; |
| 13 | +use Bow\Container\CompassConfiguration; |
15 | 14 |
|
16 | 15 | class Loader implements ArrayAccess |
17 | 16 | { |
@@ -175,52 +174,97 @@ public function boot(): Loader |
175 | 174 | return $this; |
176 | 175 | } |
177 | 176 |
|
178 | | - $services = array_merge( |
179 | | - [ContainerConfiguration::class, EnvConfiguration::class], |
180 | | - $this->configurations(), |
| 177 | + $container = Capsule::getInstance(); |
| 178 | + |
| 179 | + $this->createConfiguration(EnvConfiguration::class, $container); |
| 180 | + |
| 181 | + // Configuration of services |
| 182 | + $loaded_configurations = $this->createConfigurations( |
| 183 | + array_merge([CompassConfiguration::class], $this->configurations()), |
| 184 | + $container |
181 | 185 | ); |
182 | 186 |
|
183 | | - $service_collection = []; |
| 187 | + // Load configurations |
| 188 | + $this->runConfirmations($loaded_configurations); |
184 | 189 |
|
185 | | - $container = Capsule::getInstance(); |
| 190 | + // Load load events |
| 191 | + $this->loadEvents(); |
186 | 192 |
|
187 | | - // Configuration of services |
188 | | - foreach ($services as $service) { |
189 | | - if ($this->without_session && $service === SessionConfiguration::class) { |
190 | | - continue; |
191 | | - } |
| 193 | + // Set the load as booted |
| 194 | + $this->booted = true; |
192 | 195 |
|
193 | | - if (!class_exists($service)) { |
194 | | - continue; |
195 | | - } |
| 196 | + return $this; |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Load a configuration service |
| 201 | + * |
| 202 | + * @param string $configuration_class |
| 203 | + * @param Capsule $container |
| 204 | + * @return Configuration |
| 205 | + */ |
| 206 | + private function createConfiguration(string $configuration_class, Capsule $container): Configuration |
| 207 | + { |
| 208 | + if (!class_exists($configuration_class)) { |
| 209 | + throw new ApplicationException("The configuration class {$configuration_class} does not exists."); |
| 210 | + } |
| 211 | + |
| 212 | + $configuration = new $configuration_class($container); |
| 213 | + |
| 214 | + $configuration->create($this); |
196 | 215 |
|
197 | | - $service_instance = new $service($container); |
198 | | - $service_instance->create($this); |
199 | | - $service_collection[] = $service_instance; |
| 216 | + return $configuration; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Load configurations |
| 221 | + * |
| 222 | + * @param array $configurations |
| 223 | + * @param Capsule $container |
| 224 | + * @return array |
| 225 | + */ |
| 226 | + private function createConfigurations(array $configurations, Capsule $container): array |
| 227 | + { |
| 228 | + $loaded_configurations = []; |
200 | 229 |
|
201 | | - // Encure that the .env file is loaded before others services |
202 | | - if ($service === EnvConfiguration::class) { |
203 | | - $this->loadEnvfile(); |
| 230 | + foreach ($configurations as $configuration) { |
| 231 | + if ($this->without_session && $configuration === SessionConfiguration::class) { |
| 232 | + continue; |
204 | 233 | } |
| 234 | + |
| 235 | + $loaded_configurations[] = $this->createConfiguration($configuration, $container); |
205 | 236 | } |
206 | 237 |
|
| 238 | + return $loaded_configurations; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Run the loaded configurations |
| 243 | + * |
| 244 | + * @param array $loaded_configurations |
| 245 | + * @return void |
| 246 | + */ |
| 247 | + private function runConfirmations(array $loaded_configurations): void |
| 248 | + { |
207 | 249 | // Start of services or initial code |
208 | | - foreach ($service_collection as $service) { |
| 250 | + foreach ($loaded_configurations as $service) { |
209 | 251 | $service->run(); |
210 | 252 | } |
| 253 | + } |
211 | 254 |
|
| 255 | + /** |
| 256 | + * Load events |
| 257 | + * |
| 258 | + * @return void |
| 259 | + */ |
| 260 | + private function loadEvents(): void |
| 261 | + { |
212 | 262 | // Bind the define events |
213 | 263 | foreach ($this->events() as $name => $handlers) { |
214 | | - $handlers = (array) $handlers; |
215 | | - foreach ($handlers as $handler) { |
216 | | - Event::on($name, $handler); |
| 264 | + foreach ((array) $handlers as $handler) { |
| 265 | + app_event($name, $handler); |
217 | 266 | } |
218 | 267 | } |
219 | | - |
220 | | - // Set the load as booted |
221 | | - $this->booted = true; |
222 | | - |
223 | | - return $this; |
224 | 268 | } |
225 | 269 |
|
226 | 270 | /** |
|
0 commit comments