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

Commit bfba9b2

Browse files
author
Andrey Helldar
committed
Changed the principle of collecting keys
1 parent a5f05b0 commit bfba9b2

31 files changed

+819
-643
lines changed

.github/images/compare.png

-28.2 KB
Binary file not shown.

README.md

+16-22
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
[![Total Downloads][badge_downloads]][link_packagist]
1515
[![License][badge_license]][link_license]
1616

17-
> What is he doing?
18-
> In the current implementation, the package copies the `.env` file with the environment settings to the `.env.example` file and hides the private data.
19-
>
20-
> Soon, the file compilation algorithm will be slightly different.
21-
2217
## Table of contents
2318

2419
* [Installation](#installation)
@@ -40,13 +35,17 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
4035
```json
4136
{
4237
"require-dev": {
43-
"andrey-helldar/env-sync": "^1.0"
38+
"andrey-helldar/env-sync": "^1.2"
4439
}
4540
}
4641
```
4742

4843
## How to use
4944

45+
> Note
46+
> This package scans files with `*.php`, `*.json`, `*.yml`, `*.yaml` and `*.twig` extensions in the specified folder, receiving from them calls to the `env` and `getenv` functions.
47+
> Based on the received values, the package creates a key-value array. When saving, the keys are split into blocks by the first word before the `_` character.
48+
5049
### Laravel / Lumen Frameworks
5150

5251
Just execute the `php artisan env:sync` command.
@@ -65,7 +64,7 @@ You can also specify the invocation when executing the `composer update` command
6564

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

68-
If you want to define default values or specify which key values should be saved, publish the configuration file by running the artisan command:
67+
If you want to force the stored values, you can change the configuration file by publishing it with the command:
6968

7069
```bash
7170
php artisan vendor:publish --provider="Helldar\EnvSync\ServiceProvider"
@@ -79,31 +78,29 @@ To call a command in your application, you need to do the following:
7978

8079
```php
8180
use Helldar\EnvSync\Services\Compiler;
81+
use Helldar\EnvSync\Services\Finder;
8282
use Helldar\EnvSync\Services\Parser;
8383
use Helldar\EnvSync\Services\Stringify;
8484
use Helldar\EnvSync\Services\Syncer;
8585
use Helldar\EnvSync\Support\Config;
86+
use Symfony\Component\Finder\Finder as SymfonyFinder;
8687

8788
protected function syncer(): Syncer
8889
{
8990
$parser = new Parser();
9091
$stringify = new Stringify();
9192
$config = new Config();
9293
$compiler = new Compiler($stringify, $config);
94+
$finder = new Finder(SymfonyFinder::create());
9395

94-
return new Syncer($parser, $compiler);
96+
return new Syncer($parser, $compiler, $finder);
9597
}
9698

9799
protected function sync()
98100
{
99-
// $this->syncer()
100-
// ->from('/** path to .env file */')
101-
// ->to('/** path to .env.example file */')
102-
// ->store();
103-
104101
$this->syncer()
105-
->from(__DIR__ . '/../.env')
106-
->to(__DIR__ . '/../.env.example')
102+
->path(__DIR__)
103+
->filename('.env.example')
107104
->store();
108105
}
109106
```
@@ -112,19 +109,22 @@ If you want to define default values or specify which key values should be store
112109

113110
```php
114111
use Helldar\EnvSync\Services\Compiler;
112+
use Helldar\EnvSync\Services\Finder;
115113
use Helldar\EnvSync\Services\Parser;
116114
use Helldar\EnvSync\Services\Stringify;
117115
use Helldar\EnvSync\Services\Syncer;
118116
use Helldar\EnvSync\Support\Config;
117+
use Symfony\Component\Finder\Finder as SymfonyFinder;
119118

120119
protected function syncer(): Syncer
121120
{
122121
$parser = new Parser();
123122
$stringify = new Stringify();
124123
$config = new Config($this->config());
125124
$compiler = new Compiler($stringify, $config);
125+
$finder = new Finder(SymfonyFinder::create());
126126

127-
return new Syncer($parser, $compiler);
127+
return new Syncer($parser, $compiler, $finder);
128128
}
129129

130130
protected function config(): array
@@ -133,12 +133,6 @@ protected function config(): array
133133
}
134134
```
135135

136-
### Example
137-
138-
<p align="center">
139-
<img src="/.github/images/compare.png?raw=true" alt="Example"/>
140-
</p>
141-
142136
You can also suggest your implementation by sending a PR. We will be glad 😊
143137

144138
[badge_build]: https://img.shields.io/github/workflow/status/andrey-helldar/env-sync/native?style=flat-square

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"require": {
1818
"php": "^7.3|^8.0",
19-
"andrey-helldar/support": "^2.5"
19+
"andrey-helldar/support": "^2.5",
20+
"symfony/finder": "^4.0|^5.0"
2021
},
2122
"require-dev": {
2223
"mockery/mockery": "^1.3.1",
@@ -42,7 +43,7 @@
4243
"extra": {
4344
"laravel": {
4445
"providers": [
45-
"Helldar\\EnvSync\\ServiceProvider"
46+
"Helldar\\EnvSync\\Frameworks\\Laravel\\ServiceProvider"
4647
]
4748
}
4849
}

config/env-sync.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<?php
22

33
return [
4-
'keep' => [
5-
'APP_NAME',
6-
],
7-
84
'forces' => [
95
'APP_ENV' => 'production',
106
'APP_DEBUG' => false,
117
'APP_URL' => 'http://localhost',
128

139
'LOG_CHANNEL' => 'daily',
10+
'LOG_LEVEL' => 'debug',
1411

1512
'DB_CONNECTION' => 'mysql',
1613
'DB_HOST' => '127.0.0.1',

src/Console/Sync.php

-40
This file was deleted.
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Helldar\EnvSync\Frameworks\Laravel\Console;
4+
5+
use Helldar\EnvSync\Services\Syncer;
6+
use Illuminate\Console\Command;
7+
8+
final class Sync extends Command
9+
{
10+
protected $signature = 'env:sync {--path=}';
11+
12+
protected $description = 'Synchronizing environment settings with a preset.';
13+
14+
public function handle(Syncer $syncer)
15+
{
16+
$syncer
17+
->path($this->path())
18+
->filename($this->filename())
19+
->store();
20+
}
21+
22+
protected function path(): string
23+
{
24+
return $this->optionPath() ?: $this->realPath();
25+
}
26+
27+
protected function filename(): string
28+
{
29+
return '.env.example';
30+
}
31+
32+
protected function optionPath(): ?string
33+
{
34+
return $this->option('path');
35+
}
36+
37+
protected function realPath(): string
38+
{
39+
return realpath(base_path());
40+
}
41+
}

src/ServiceProvider.php renamed to src/Frameworks/Laravel/ServiceProvider.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Helldar\EnvSync;
3+
namespace Helldar\EnvSync\Frameworks\Laravel;
44

5-
use Helldar\EnvSync\Console\Sync;
5+
use Helldar\EnvSync\Frameworks\Laravel\Console\Sync;
66
use Helldar\EnvSync\Support\Config;
77
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
88

99
final class ServiceProvider extends BaseServiceProvider
1010
{
11+
protected $config_path = __DIR__ . '/../../../config/env-sync.php';
12+
1113
public function register()
1214
{
1315
$this->registerCommands();
@@ -26,7 +28,7 @@ protected function registerCommands(): void
2628

2729
protected function registerConfig(): void
2830
{
29-
$this->mergeConfigFrom(__DIR__ . '/../config/env-sync.php', 'env-sync');
31+
$this->mergeConfigFrom($this->config_path, 'env-sync');
3032

3133
$this->app->singleton(Config::class, static function ($app) {
3234
return new Config($app['config']->get('env-sync'));
@@ -36,7 +38,7 @@ protected function registerConfig(): void
3638
protected function bootConfig(): void
3739
{
3840
$this->publishes([
39-
__DIR__ . '/../config/env-sync.php' => $this->app->configPath('env-sync.php'),
41+
$this->config_path => $this->app->configPath('env-sync.php'),
4042
], 'config');
4143
}
4244
}

src/Services/Compiler.php

+42-41
Original file line numberDiff line numberDiff line change
@@ -32,83 +32,84 @@ public function items(array $items): self
3232

3333
public function get(): string
3434
{
35-
foreach ($this->items as $key => &$value) {
36-
if ($this->isEmptyRow($key, $value)) {
37-
continue;
38-
}
35+
$this->map();
36+
$this->split();
37+
38+
return $this->compile();
39+
}
3940

41+
protected function map(): void
42+
{
43+
foreach ($this->items as $key => &$value) {
4044
$replaced = $this->replace($key, $value);
4145

4246
$value = $this->stringify($replaced);
4347
}
44-
45-
return $this->compile();
4648
}
4749

48-
protected function replace(string $key, $value)
50+
protected function split(): void
4951
{
50-
switch (true) {
51-
case $this->isForceHiding($key):
52-
return null;
52+
$items = [];
5353

54-
case $this->isKeeping($key):
55-
return $value;
54+
foreach ($this->items as $key => $value) {
55+
$section = $this->section($key);
5656

57-
default:
58-
return $this->value($key);
57+
$items[$section][$key] = $value;
5958
}
60-
}
6159

62-
protected function isKeeping(string $key): bool
63-
{
64-
return $this->inArray($key, $this->config->keep());
60+
$this->items = $items;
6561
}
6662

67-
protected function isForceHiding(string $key): bool
63+
protected function compile(): string
6864
{
69-
return $this->inArray($key, $this->hides);
70-
}
65+
$result = '';
7166

72-
protected function value(string $key)
73-
{
74-
foreach ($this->config->forces() as $forced_key => $value) {
75-
if (Str::endsWith($key, $forced_key)) {
76-
return $value;
67+
foreach ($this->items as $values) {
68+
ksort($values);
69+
70+
foreach ($values as $key => $value) {
71+
$result .= "{$key}={$value}{$this->separator}";
7772
}
73+
74+
$result .= $this->separator;
7875
}
7976

80-
return null;
77+
return trim($result) . $this->separator;
8178
}
8279

83-
protected function stringify($value): string
80+
protected function replace(string $key, $value)
8481
{
85-
return $this->stringify->get($value);
82+
return $this->isForceHiding($key) ? null : $this->value($key, $value);
8683
}
8784

88-
protected function compile(): string
85+
protected function isForceHiding(string $key): bool
8986
{
90-
$result = [];
91-
92-
foreach ($this->items as $key => $value) {
93-
if ($this->isEmptyRow($key, $value)) {
94-
$result[] = '';
87+
return $this->inArray($key, $this->hides);
88+
}
9589

96-
continue;
90+
protected function value(string $key, $value)
91+
{
92+
foreach ($this->config->forces() as $force_key => $force_value) {
93+
if (Str::contains($key, $force_key)) {
94+
return $force_value;
9795
}
98-
99-
$result[] = $key . '=' . $value;
10096
}
10197

102-
return implode($this->separator, $result);
98+
return $value;
10399
}
104100

105-
protected function isEmptyRow($key, $value): bool
101+
protected function stringify($value): string
106102
{
107-
return is_numeric($key) && empty($value);
103+
return $this->stringify->get($value);
108104
}
109105

110106
protected function inArray(string $key, array $array): bool
111107
{
112108
return Str::contains($key, $array);
113109
}
110+
111+
protected function section(string $key): string
112+
{
113+
return explode('_', $key, 2)[0];
114+
}
114115
}

0 commit comments

Comments
 (0)