Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit 55f291a

Browse files
author
Andrey Helldar
committed
Implemented "soft" configuration
1 parent 4d6d6c0 commit 55f291a

File tree

11 files changed

+294
-35
lines changed

11 files changed

+294
-35
lines changed

README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ You can also specify the invocation when executing the `composer update` command
6060

6161
Now, every time you run the `composer update` command, the environment settings file will be synchronized.
6262

63+
If you want to define default values or specify which key values should be saved, publish the configuration file by running the artisan command:
64+
65+
```bash
66+
php artisan vendor:publish --provider="Helldar\EnvSync\ServiceProvider"
67+
```
68+
69+
Now you can change the file `config/env-sync.php`.
70+
6371
### Other using
6472

6573
To call a command in your application, you need to do the following:
@@ -69,12 +77,14 @@ use Helldar\EnvSync\Services\Compiler;
6977
use Helldar\EnvSync\Services\Parser;
7078
use Helldar\EnvSync\Services\Stringify;
7179
use Helldar\EnvSync\Services\Syncer;
80+
use Helldar\EnvSync\Support\Config;
7281

7382
protected function syncer(): Syncer
7483
{
7584
$parser = new Parser();
7685
$stringify = new Stringify();
77-
$compiler = new Compiler($stringify);
86+
$config = new Config();
87+
$compiler = new Compiler($stringify, $config);
7888

7989
return new Syncer($parser, $compiler);
8090
}
@@ -93,6 +103,31 @@ protected function sync()
93103
}
94104
```
95105

106+
If you want to define default values or specify which key values should be stored, you need to pass an array to the constructor of the `Config` class:
107+
108+
```php
109+
use Helldar\EnvSync\Services\Compiler;
110+
use Helldar\EnvSync\Services\Parser;
111+
use Helldar\EnvSync\Services\Stringify;
112+
use Helldar\EnvSync\Services\Syncer;
113+
use Helldar\EnvSync\Support\Config;
114+
115+
protected function syncer(): Syncer
116+
{
117+
$parser = new Parser();
118+
$stringify = new Stringify();
119+
$config = new Config($this->config());
120+
$compiler = new Compiler($stringify, $config);
121+
122+
return new Syncer($parser, $compiler);
123+
}
124+
125+
protected function config(): array
126+
{
127+
return require realpath(__DIR__ . '/your-path/your-config.php');
128+
}
129+
```
130+
96131
### Example
97132

98133
<p align="center">

config/env-sync.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
return [
4+
'keep' => [
5+
'APP_NAME',
6+
],
7+
8+
'forces' => [
9+
'APP_ENV' => 'production',
10+
'APP_DEBUG' => false,
11+
'APP_URL' => 'http://localhost',
12+
13+
'LOG_CHANNEL' => 'daily',
14+
15+
'DB_CONNECTION' => 'mysql',
16+
'DB_HOST' => '127.0.0.1',
17+
'DB_PORT' => 3306,
18+
'DB_DATABASE' => 'default',
19+
20+
'BROADCAST_DRIVER' => 'redis',
21+
'CACHE_DRIVER' => 'redis',
22+
'QUEUE_CONNECTION' => 'redis',
23+
'SESSION_DRIVER' => 'redis',
24+
'SESSION_LIFETIME' => 120,
25+
26+
'REDIS_HOST' => '127.0.0.1',
27+
'REDIS_PORT' => 6379,
28+
29+
'MAIL_MAILER' => 'smtp',
30+
'MAIL_HOST' => 'mailhog',
31+
'MAIL_PORT' => 1025,
32+
],
33+
];

src/ServiceProvider.php

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Helldar\EnvSync;
44

55
use Helldar\EnvSync\Console\Sync;
6+
use Helldar\EnvSync\Support\Config;
67
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
78

89
final class ServiceProvider extends BaseServiceProvider
@@ -12,8 +13,29 @@ public function register()
1213
$this->registerCommands();
1314
}
1415

16+
public function boot()
17+
{
18+
$this->bootConfig();
19+
}
20+
1521
protected function registerCommands(): void
1622
{
1723
$this->commands(Sync::class);
1824
}
25+
26+
protected function registerConfig(): void
27+
{
28+
$this->mergeConfigFrom(__DIR__ . '/../config/env-sync.php', 'env-sync');
29+
30+
$this->app->singleton(Config::class, static function ($app) {
31+
return new Config($app['config']->get('env-sync'));
32+
});
33+
}
34+
35+
protected function bootConfig(): void
36+
{
37+
$this->publishes([
38+
__DIR__ . '/../config/env-sync.php' => $this->app->configPath('env-sync.php'),
39+
], 'config');
40+
}
1941
}

src/Services/Compiler.php

+7-31
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,23 @@
22

33
namespace Helldar\EnvSync\Services;
44

5+
use Helldar\EnvSync\Support\Config;
56
use Helldar\Support\Facades\Helpers\Str;
67

78
final class Compiler
89
{
9-
protected $keeping = ['APP_NAME'];
10-
11-
protected $forces = [
12-
'APP_ENV' => 'production',
13-
'APP_DEBUG' => false,
14-
'APP_URL' => 'http://localhost',
15-
16-
'LOG_CHANNEL' => 'daily',
17-
18-
'DB_CONNECTION' => 'mysql',
19-
'DB_HOST' => '127.0.0.1',
20-
'DB_PORT' => 3306,
21-
'DB_DATABASE' => 'default',
22-
23-
'BROADCAST_DRIVER' => 'redis',
24-
'CACHE_DRIVER' => 'redis',
25-
'QUEUE_CONNECTION' => 'redis',
26-
'SESSION_DRIVER' => 'redis',
27-
'SESSION_LIFETIME' => 120,
28-
29-
'REDIS_HOST' => '127.0.0.1',
30-
'REDIS_PORT' => 6379,
31-
32-
'MAIL_MAILER' => 'smtp',
33-
'MAIL_HOST' => 'mailhog',
34-
'MAIL_PORT' => 1025,
35-
];
36-
3710
protected $separator = "\n";
3811

3912
protected $stringify;
4013

14+
protected $config;
15+
4116
protected $items;
4217

43-
public function __construct(Stringify $stringify)
18+
public function __construct(Stringify $stringify, Config $config)
4419
{
4520
$this->stringify = $stringify;
21+
$this->config = $config;
4622
}
4723

4824
public function items(array $items): self
@@ -78,12 +54,12 @@ protected function replace(string $key, $value)
7854

7955
protected function isKeeping(string $key): bool
8056
{
81-
return in_array($key, $this->keeping);
57+
return in_array($key, $this->config->keep());
8258
}
8359

8460
protected function value(string $key)
8561
{
86-
foreach ($this->forces as $forced_key => $value) {
62+
foreach ($this->config->forces() as $forced_key => $value) {
8763
if (Str::endsWith($key, $forced_key)) {
8864
return $value;
8965
}

src/Support/Config.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Helldar\EnvSync\Support;
4+
5+
final class Config
6+
{
7+
/** @var array */
8+
protected $config;
9+
10+
public function __construct(array $config = null)
11+
{
12+
$this->config = $config ?: $this->load();
13+
}
14+
15+
public function keep(): array
16+
{
17+
return $this->get('keep', []);
18+
}
19+
20+
public function forces(): array
21+
{
22+
return $this->get('forces', []);
23+
}
24+
25+
protected function get(string $key, $default = null)
26+
{
27+
return $this->config[$key] ?? $default;
28+
}
29+
30+
protected function load(): array
31+
{
32+
return require realpath(__DIR__ . '/../../config/env-sync.php');
33+
}
34+
}

tests/Concerns/Files.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ protected function source(): string
99
return $this->fixtures('source');
1010
}
1111

12-
protected function expected(): string
12+
protected function expected(bool $config = false): string
1313
{
14-
return $this->fixtures('expected');
14+
$filename = $config ? 'expected-config' : 'expected';
15+
16+
return $this->fixtures($filename);
1517
}
1618

1719
protected function actual(): string

tests/Other/ConfigurableTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Tests\Other;
4+
5+
use Helldar\EnvSync\Services\Compiler;
6+
use Helldar\EnvSync\Services\Parser;
7+
use Helldar\EnvSync\Services\Stringify;
8+
use Helldar\EnvSync\Services\Syncer;
9+
use Helldar\EnvSync\Support\Config;
10+
use Tests\Cases\OtherTestCase;
11+
12+
final class ConfigurableTest extends OtherTestCase
13+
{
14+
public function testContent()
15+
{
16+
$service = $this->service();
17+
18+
$service->from($this->source());
19+
20+
$this->assertStringEqualsFile($this->expected(true), $service->cleaned());
21+
}
22+
23+
public function testStoring()
24+
{
25+
$service = $this->service();
26+
27+
$service->from($this->source());
28+
$service->to($this->actual());
29+
30+
$service->store();
31+
32+
$this->assertFileExists($this->actual());
33+
$this->assertFileEquals($this->expected(true), $this->actual());
34+
}
35+
36+
protected function service(): Syncer
37+
{
38+
$parser = new Parser();
39+
$stringify = new Stringify();
40+
$config = new Config($this->config());
41+
$compiler = new Compiler($stringify, $config);
42+
43+
return new Syncer($parser, $compiler);
44+
}
45+
46+
protected function config(): array
47+
{
48+
return require realpath(__DIR__ . '/../fixtures/config.php');
49+
}
50+
}

tests/Other/OtherTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Helldar\EnvSync\Services\Parser;
77
use Helldar\EnvSync\Services\Stringify;
88
use Helldar\EnvSync\Services\Syncer;
9+
use Helldar\EnvSync\Support\Config;
910
use Tests\Cases\OtherTestCase;
1011

1112
final class OtherTest extends OtherTestCase
@@ -36,7 +37,8 @@ protected function service(): Syncer
3637
{
3738
$parser = new Parser();
3839
$stringify = new Stringify();
39-
$compiler = new Compiler($stringify);
40+
$config = new Config();
41+
$compiler = new Compiler($stringify, $config);
4042

4143
return new Syncer($parser, $compiler);
4244
}

tests/fixtures/config.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
'keep' => ['APP_ENV', 'LOG_CHANNEL'],
5+
6+
'forces' => [
7+
'DB_HOST' => 'foo',
8+
],
9+
];

tests/fixtures/expected-config

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
APP_NAME=null
2+
APP_ENV=local
3+
APP_KEY=null
4+
APP_DEBUG=null
5+
APP_URL=null
6+
7+
LOG_CHANNEL=stack
8+
9+
DB_CONNECTION=null
10+
DB_HOST=foo
11+
DB_PORT=null
12+
DB_DATABASE=null
13+
DB_USERNAME=null
14+
DB_PASSWORD=null
15+
16+
AUDITS_DB_CONNECTION=null
17+
AUDITS_DB_HOST=foo
18+
AUDITS_DB_PORT=null
19+
AUDITS_DB_DATABASE=null
20+
AUDITS_DB_USERNAME=null
21+
AUDITS_DB_PASSWORD=null
22+
23+
BROADCAST_DRIVER=null
24+
CACHE_DRIVER=null
25+
QUEUE_CONNECTION=null
26+
SESSION_DRIVER=null
27+
SESSION_LIFETIME=null
28+
29+
REDIS_HOST=null
30+
REDIS_PASSWORD=null
31+
REDIS_PORT=null
32+
33+
MAIL_MAILER=null
34+
MAIL_HOST=null
35+
MAIL_PORT=null
36+
MAIL_USERNAME=null
37+
MAIL_PASSWORD=null
38+
MAIL_ENCRYPTION=null
39+
MAIL_FROM_ADDRESS=null
40+
MAIL_FROM_NAME=null
41+
42+
GIT_WEBHOOK_SECRET=null
43+
44+
GITHUB_CLIENT_ID=null
45+
GITHUB_CLIENT_SECRET=null
46+
47+
FOO_ONE=null
48+
FOO_TWO=null

0 commit comments

Comments
 (0)