Skip to content

Commit 58f3384

Browse files
committed
Refactoring loader
1 parent 36576bd commit 58f3384

3 files changed

Lines changed: 27 additions & 22 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: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Bow\Support\Arraydotify;
1111
use Bow\Session\SessionConfiguration;
1212
use Bow\Configuration\EnvConfiguration;
13-
use Bow\Container\ContainerConfiguration;
1413
use Bow\Application\Exception\ApplicationException;
14+
use Bow\Container\CompassConfiguration;
1515

1616
class Loader implements ArrayAccess
1717
{
@@ -175,32 +175,20 @@ public function boot(): Loader
175175
return $this;
176176
}
177177

178-
$services = array_merge(
179-
[ContainerConfiguration::class, EnvConfiguration::class],
180-
$this->configurations(),
181-
);
182-
183-
$service_collection = [];
178+
$services = array_merge([CompassConfiguration::class], $this->configurations());
184179

185180
$container = Capsule::getInstance();
186181

182+
$this->loadConfiguration(EnvConfiguration::class, $container);
183+
187184
// Configuration of services
188185
foreach ($services as $service) {
189186
if ($this->without_session && $service === SessionConfiguration::class) {
190187
continue;
191188
}
192189

193-
if (!class_exists($service)) {
194-
continue;
195-
}
196-
197-
$service_instance = new $service($container);
198-
$service_instance->create($this);
199-
$service_collection[] = $service_instance;
200-
201-
// Encure that the .env file is loaded before others services
202-
if ($service === EnvConfiguration::class) {
203-
$this->loadEnvfile();
190+
if (class_exists($service)) {
191+
$service_collection[] = $this->loadConfiguration($service, $container);
204192
}
205193
}
206194

@@ -213,7 +201,7 @@ public function boot(): Loader
213201
foreach ($this->events() as $name => $handlers) {
214202
$handlers = (array) $handlers;
215203
foreach ($handlers as $handler) {
216-
Event::on($name, $handler);
204+
app_event($name, $handler);
217205
}
218206
}
219207

@@ -223,6 +211,22 @@ public function boot(): Loader
223211
return $this;
224212
}
225213

214+
/**
215+
* Load a configuration service
216+
*
217+
* @param string $service
218+
* @param Capsule $container
219+
* @return Configuration
220+
*/
221+
private function loadConfiguration(string $service, Capsule $container): Configuration
222+
{
223+
$service_instance = new $service($container);
224+
225+
$service_instance->create($this);
226+
227+
return $service_instance;
228+
}
229+
226230
/**
227231
* Load the .env file
228232
*
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)