Skip to content

Commit 201266c

Browse files
authored
Merge pull request #344 from bowphp/refactor/code-base
Refactoring loader
2 parents aed421b + e27a622 commit 201266c

3 files changed

Lines changed: 78 additions & 33 deletions

File tree

src/Configuration/EnvConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class EnvConfiguration extends Configuration
1414
public function create(Loader $config): void
1515
{
1616
Env::configure(base_path('.env.json'));
17+
1718
$event = Env::getInstance();
1819

1920
$this->container->instance('env', $event);

src/Configuration/Loader.php

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace Bow\Configuration;
66

77
use ArrayAccess;
8-
use Bow\Event\Event;
98
use Bow\Container\Capsule;
109
use Bow\Support\Arraydotify;
1110
use Bow\Session\SessionConfiguration;
1211
use Bow\Configuration\EnvConfiguration;
13-
use Bow\Container\ContainerConfiguration;
1412
use Bow\Application\Exception\ApplicationException;
13+
use Bow\Container\CompassConfiguration;
1514

1615
class Loader implements ArrayAccess
1716
{
@@ -175,52 +174,97 @@ public function boot(): Loader
175174
return $this;
176175
}
177176

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
181185
);
182186

183-
$service_collection = [];
187+
// Load configurations
188+
$this->runConfirmations($loaded_configurations);
184189

185-
$container = Capsule::getInstance();
190+
// Load load events
191+
$this->loadEvents();
186192

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;
192195

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);
196215

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 = [];
200229

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;
204233
}
234+
235+
$loaded_configurations[] = $this->createConfiguration($configuration, $container);
205236
}
206237

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+
{
207249
// Start of services or initial code
208-
foreach ($service_collection as $service) {
250+
foreach ($loaded_configurations as $service) {
209251
$service->run();
210252
}
253+
}
211254

255+
/**
256+
* Load events
257+
*
258+
* @return void
259+
*/
260+
private function loadEvents(): void
261+
{
212262
// Bind the define events
213263
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);
217266
}
218267
}
219-
220-
// Set the load as booted
221-
$this->booted = true;
222-
223-
return $this;
224268
}
225269

226270
/**
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Bow\Configuration\Configuration;
88
use Bow\Configuration\Loader;
99

10-
class ContainerConfiguration extends Configuration
10+
class CompassConfiguration extends Configuration
1111
{
1212
/**
1313
* @var array
@@ -21,7 +21,7 @@ class ContainerConfiguration extends Configuration
2121
*/
2222
public function create(Loader $config): void
2323
{
24-
$this->container->bind('container', function () use ($config) {
24+
$this->container->bind('compass', function () use ($config) {
2525
$middlewares = array_merge($config->getMiddlewares(), $this->middlewares);
2626

2727
return Compass::configure($config->namespaces(), $middlewares);
@@ -33,6 +33,6 @@ public function create(Loader $config): void
3333
*/
3434
public function run(): void
3535
{
36-
$this->container->make('action');
36+
$this->container->make('compass');
3737
}
3838
}

0 commit comments

Comments
 (0)