Skip to content

Commit aed421b

Browse files
authored
Merge pull request #343 from bowphp/refactor/code-base
Refactoring env loading and code formatting
2 parents b6c1e56 + 36576bd commit aed421b

15 files changed

Lines changed: 77 additions & 109 deletions

src/Auth/AuthenticationConfiguration.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ class AuthenticationConfiguration extends Configuration
1414
*/
1515
public function create(Loader $config): void
1616
{
17-
$this->container->bind(
18-
'auth',
19-
function () use ($config) {
20-
return Auth::configure($config['auth']);
21-
}
22-
);
17+
$this->container->bind('auth', function () use ($config) {
18+
return Auth::configure($config['auth']);
19+
});
2320
}
2421

2522
/**

src/Cache/CacheConfiguration.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ class CacheConfiguration extends Configuration
1414
*/
1515
public function create(Loader $config): void
1616
{
17-
$this->container->bind(
18-
'cache',
19-
function () use ($config) {
20-
return Cache::configure($config['cache']);
21-
}
22-
);
17+
$this->container->bind('cache', function () use ($config) {
18+
return Cache::configure($config['cache']);
19+
});
2320
}
2421

2522
/**

src/Configuration/EnvConfiguration.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@ class EnvConfiguration extends Configuration
1313
*/
1414
public function create(Loader $config): void
1515
{
16-
$this->container->bind('env', function () use ($config) {
17-
Env::configure($config['app.env_file']);
16+
Env::configure(base_path('.env.json'));
17+
$event = Env::getInstance();
1818

19-
return Env::getInstance();
20-
});
19+
$this->container->instance('env', $event);
2120
}
2221

2322
/**
2423
* @inheritdoc
2524
*/
2625
public function run(): void
2726
{
28-
$this->container->make('env');
27+
// Nothing to do
2928
}
3029
}

src/Configuration/Loader.php

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

77
use ArrayAccess;
8-
use Bow\Application\Exception\ApplicationException;
9-
use Bow\Container\Capsule;
10-
use Bow\Container\ContainerConfiguration;
118
use Bow\Event\Event;
12-
use Bow\Session\SessionConfiguration;
9+
use Bow\Container\Capsule;
1310
use Bow\Support\Arraydotify;
11+
use Bow\Session\SessionConfiguration;
12+
use Bow\Configuration\EnvConfiguration;
13+
use Bow\Container\ContainerConfiguration;
14+
use Bow\Application\Exception\ApplicationException;
1415

1516
class Loader implements ArrayAccess
1617
{
@@ -174,10 +175,8 @@ public function boot(): Loader
174175
return $this;
175176
}
176177

177-
$this->loadEnvfile();
178-
179178
$services = array_merge(
180-
[ContainerConfiguration::class],
179+
[ContainerConfiguration::class, EnvConfiguration::class],
181180
$this->configurations(),
182181
);
183182

@@ -198,6 +197,11 @@ public function boot(): Loader
198197
$service_instance = new $service($container);
199198
$service_instance->create($this);
200199
$service_collection[] = $service_instance;
200+
201+
// Encure that the .env file is loaded before others services
202+
if ($service === EnvConfiguration::class) {
203+
$this->loadEnvfile();
204+
}
201205
}
202206

203207
// Start of services or initial code

src/Configuration/LoggerConfiguration.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,15 @@ class LoggerConfiguration extends Configuration
2525
*/
2626
public function create(Loader $config): void
2727
{
28-
$this->container->bind(
29-
'logger',
30-
function () use ($config) {
31-
$monolog = $this->loadFileLogger(
32-
realpath($config['storage.log']),
33-
$config['app.name'] ?? 'Bow'
34-
);
35-
36-
if (php_sapi_name() != "cli") {
37-
$this->loadFrontLogger($monolog, $config['app.error_handle']);
38-
}
28+
$this->container->bind('logger', function () use ($config) {
29+
$monolog = $this->loadFileLogger(realpath($config['storage.log']), $config['app.name'] ?? 'Bow');
3930

40-
return $monolog;
31+
if (php_sapi_name() != "cli") {
32+
$this->loadFrontLogger($monolog, $config['app.error_handle']);
4133
}
42-
);
34+
35+
return $monolog;
36+
});
4337
}
4438

4539
/**

src/Container/ContainerConfiguration.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ class ContainerConfiguration extends Configuration
2121
*/
2222
public function create(Loader $config): void
2323
{
24-
$this->container->bind(
25-
'action',
26-
function () use ($config) {
27-
$middlewares = array_merge($config->getMiddlewares(), $this->middlewares);
24+
$this->container->bind('container', function () use ($config) {
25+
$middlewares = array_merge($config->getMiddlewares(), $this->middlewares);
2826

29-
return Compass::configure($config->namespaces(), $middlewares);
30-
}
31-
);
27+
return Compass::configure($config->namespaces(), $middlewares);
28+
});
3229
}
3330

3431
/**

src/Database/DatabaseConfiguration.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class DatabaseConfiguration extends Configuration
1414
*/
1515
public function create(Loader $config): void
1616
{
17-
$this->container->bind(
18-
'db',
19-
function () use ($config) {
20-
return Database::configure($config['database'] ?? $config['db']);
21-
}
22-
);
17+
$this->container->bind('db', function () use ($config) {
18+
return Database::configure($config['database'] ?? $config['db']);
19+
});
20+
$this->container->bind('database', function () use ($config) {
21+
return Database::configure($config['database'] ?? $config['db']);
22+
});
2323
}
2424

2525
/**
@@ -28,5 +28,6 @@ function () use ($config) {
2828
public function run(): void
2929
{
3030
$this->container->make('db');
31+
$this->container->make('database');
3132
}
3233
}

src/Mail/MailConfiguration.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ class MailConfiguration extends Configuration
1414
*/
1515
public function create(Loader $config): void
1616
{
17-
$this->container->bind(
18-
'mail',
19-
function () use ($config) {
20-
return Mail::configure($config['mail']);
21-
}
22-
);
17+
$this->container->bind('mail', function () use ($config) {
18+
return Mail::configure($config['mail']);
19+
});
2320
}
2421

2522
/**

src/Queue/QueueConfiguration.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ class QueueConfiguration extends Configuration
1515
*/
1616
public function create(Loader $config): void
1717
{
18-
$this->container->bind(
19-
'queue',
20-
function () use ($config) {
21-
return new QueueConnection($config["worker"] ?? $config["queue"]);
22-
}
23-
);
18+
$this->container->bind('queue', function () use ($config) {
19+
return new QueueConnection($config["worker"] ?? $config["queue"]);
20+
});
2421
}
2522

2623
/**

src/Security/CryptoConfiguration.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ class CryptoConfiguration extends Configuration
1414
*/
1515
public function create(Loader $config): void
1616
{
17-
$this->container->bind(
18-
'security',
19-
function () use ($config) {
20-
Crypto::setkey($config['security.key'], $config['security.cipher']);
17+
$this->container->bind('security', function () use ($config) {
18+
Crypto::setkey($config['security.key'], $config['security.cipher']);
2119

22-
return Crypto::class;
23-
}
24-
);
20+
return Crypto::class;
21+
});
2522
}
2623

2724
/**

0 commit comments

Comments
 (0)