Skip to content

Commit 33a90c4

Browse files
authored
Merge pull request #354 from bowphp/refactor/code-base
Add more test for container system and upgrade sms config and configuration
2 parents 766443c + 024c7aa commit 33a90c4

File tree

14 files changed

+508
-22
lines changed

14 files changed

+508
-22
lines changed

src/Configuration/EnvConfiguration.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@ 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'] ?? null);
16+
Env::configure($config->getPath('.env.json') ?? null);
1817

19-
$event = Env::getInstance();
18+
$event = Env::getInstance();
2019

21-
$this->container->instance('env', $event);
22-
});
20+
$this->container->instance('env', $event);
2321
}
2422

2523
/**
2624
* @inheritdoc
2725
*/
2826
public function run(): void
2927
{
30-
// $this->container->make('env');
28+
//
3129
}
3230
}

src/Configuration/Loader.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class Loader implements ArrayAccess
2929
*/
3030
protected string $base_path;
3131

32+
/**
33+
* @var string
34+
*/
35+
protected string $config_path;
36+
3237
/**
3338
* @var bool
3439
*/
@@ -56,6 +61,7 @@ class Loader implements ArrayAccess
5661
private function __construct(string $base_path)
5762
{
5863
$this->base_path = $base_path;
64+
$this->config_path = $base_path . DIRECTORY_SEPARATOR . 'config';
5965
$this->config = new Arraydotify([]);
6066
}
6167

@@ -85,6 +91,17 @@ public function isCli(): bool
8591
return php_sapi_name() == 'cli';
8692
}
8793

94+
/**
95+
* Get the base path
96+
*
97+
* @param string $filename
98+
* @return string
99+
*/
100+
public function getPath(string $filename): string
101+
{
102+
return $this->base_path . DIRECTORY_SEPARATOR . $filename;
103+
}
104+
88105
/**
89106
* Get the base path
90107
*
@@ -95,6 +112,19 @@ public function getBasePath(): string
95112
return $this->base_path;
96113
}
97114

115+
/**
116+
* Set the configuration path
117+
*
118+
* @param string $path
119+
* @return Loader
120+
*/
121+
public function withConfigPath(string $path): Loader
122+
{
123+
$this->config_path = $path;
124+
125+
return $this;
126+
}
127+
98128
/**
99129
* Middleware collection
100130
*
@@ -177,9 +207,7 @@ public function boot(): Loader
177207
$container = Capsule::getInstance();
178208

179209
// Load the env configuration first
180-
$env_config = $this->createConfiguration(EnvConfiguration::class, $container);
181-
182-
$env_config->run();
210+
$this->createConfiguration(EnvConfiguration::class, $container);
183211

184212
// Load the .env or .env.json file
185213
$this->loadConfigFiles();
@@ -193,7 +221,7 @@ public function boot(): Loader
193221
// Load configurations
194222
$this->runConfirmations($loaded_configurations);
195223

196-
// Load load events
224+
// Load events
197225
$this->loadEvents();
198226

199227
// Set the load as booted
@@ -284,7 +312,7 @@ private function loadConfigFiles(): void
284312
/**
285313
* We load all Bow configuration
286314
*/
287-
$glob = glob($this->base_path . '/**.php');
315+
$glob = glob($this->config_path . '/**.php');
288316

289317
$config = [];
290318

src/Notifier/Adapters/SmsChannelAdapter.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SmsChannelAdapter implements ChannelAdapterInterface
3939
public function __construct()
4040
{
4141
$config = config('notifier.sms');
42-
$this->setting = $config['setting'] ?? [];
42+
$this->setting = $config;
4343
$this->sms_provider = $config['provider'] ?? 'callisto';
4444
}
4545

@@ -77,10 +77,11 @@ public function send(Model $context, Notifier $notifier): void
7777
private function sendWithTwilio(Model $context, Notifier $notifier): void
7878
{
7979
$data = $notifier->toSms($context);
80+
$config = $this->setting['twilio'] ?? [];
8081

81-
$account_sid = $this->setting['account_sid'] ?? null;
82-
$auth_token = $this->setting['auth_token'] ?? null;
83-
$this->from_number = $this->setting['from'] ?? null;
82+
$account_sid = $config['account_sid'] ?? null;
83+
$auth_token = $config['auth_token'] ?? null;
84+
$this->from_number = $config['from'] ?? null;
8485

8586
if (!$account_sid || !$auth_token || !$this->from_number) {
8687
throw new InvalidArgumentException('Twilio credentials are required');
@@ -110,17 +111,20 @@ private function sendWithTwilio(Model $context, Notifier $notifier): void
110111
*/
111112
private function sendWithCallisto(Model $context, Notifier $notifier): void
112113
{
113-
$access_key = $this->setting['access_key'] ?? null;
114-
$access_secret = $this->setting['access_secret'] ?? null;
115-
$notify_url = $this->setting['notify_url'] ?? null;
114+
$config = $this->setting['callisto'] ?? [];
115+
116+
$access_key = $config['access_key'] ?? null;
117+
$access_secret = $config['access_secret'] ?? null;
118+
$notify_url = $config['notify_url'] ?? null;
119+
$sender = $config['sender'] ?? null;
116120

117121
if (!$access_key || !$access_secret) {
118122
throw new InvalidArgumentException('Callisto credentials are required');
119123
}
120124

121125
$data = $notifier->toSms($context);
122126

123-
if (!isset($data['to']) || !isset($data['message']) || !isset($data['sender'])) {
127+
if (!isset($data['to']) || !isset($data['message'])) {
124128
throw new InvalidArgumentException('The phone number and notifier are required');
125129
}
126130

@@ -133,7 +137,7 @@ private function sendWithCallisto(Model $context, Notifier $notifier): void
133137
$payload = [
134138
'to' => (array) $data['to'],
135139
'message' => $data['message'],
136-
'sender' => $data['sender'],
140+
'sender' => $data['sender'] ?? $sender,
137141
];
138142

139143
if ($data['notify_url']) {

src/Support/Env.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public function __construct(string $filename)
8484
*/
8585
public static function configure(string $filename)
8686
{
87+
if (static::$instance !== null) {
88+
return;
89+
}
90+
8791
if (!file_exists($filename)) {
8892
throw new InvalidArgumentException(
8993
"The application environment file [.env.json] cannot be empty or is not define."

tests/Config/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function test_invoke_method()
165165
public function test_get_base_path()
166166
{
167167
$basePath = $this->config->getBasePath();
168-
$this->assertEquals(__DIR__ . '/stubs/config', $basePath);
168+
$this->assertEquals(__DIR__ . '/stubs', $basePath);
169169
$this->assertIsString($basePath);
170170
}
171171

tests/Config/TestingConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ public static function getConfig(): ConfigurationLoader
5858
{
5959
Env::configure(__DIR__ . '/stubs/env.json');
6060

61-
return KernelTesting::configure(__DIR__ . '/stubs/config')->boot();
61+
return KernelTesting::configure(__DIR__ . '/stubs')->withConfigPath(__DIR__ . '/stubs/config')->boot();
6262
}
6363
}

0 commit comments

Comments
 (0)