Skip to content

Commit

Permalink
refactor: add EntryObj (#24)
Browse files Browse the repository at this point in the history
Increase testability

breaking change: config('paths.backupDirectory') need to use storage_path() function
  • Loading branch information
GeoSot authored Mar 28, 2022
1 parent 40d899c commit 0cf5ebe
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 211 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.0.14, 8.1]
laravel: [9.*]
include:
- laravel: 9.*
testbench: ^7
laravel: [8.*, 9.*]
# include:
# - laravel: 9.*
# testbench: ^7

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.os }}

Expand Down
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@ The inspiration for this package was, [Brotzka/laravel-dotenv-editor](https://gi
```bash
composer require geo-sot/laravel-env-editor
```
2. Edit config/app.php (*Skip this step if you are using laravel 5.5+*)
Service provider:
```php
GeoSot\EnvEditor\ServiceProvider::class
```
Class aliases:
```php
'EnvEditor' => GeoSot\\EnvEditor\\Facades\\EnvEditor::class
```
3. Publish assets
2. Publish assets
```bash
php artisan vendor:publish --provider=GeoSot\EnvEditor\ServiceProvider
```
Expand Down Expand Up @@ -64,6 +55,9 @@ The inspiration for this package was, [Brotzka/laravel-dotenv-editor](https://gi
>* deleteBackup
>* restoreBackUp

<Details>
<Summary>Example</Summary>

```php
EnvEditor::getEnvFileContent($fileName='')
Expand Down Expand Up @@ -106,7 +100,8 @@ The inspiration for this package was, [Brotzka/laravel-dotenv-editor](https://gi
````
```
</Details>
<br/>


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^7|^8",
"php": "^8",
"laravel/framework": "^8|^9"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion config/env-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// .env file directory
'env' => base_path(),
//backup files directory
'backupDirectory' => 'env-editor',
'backupDirectory' => storage_path('env-editor'),
],
// .env file name
'envFileName' => '.env',
Expand Down
56 changes: 17 additions & 39 deletions src/EnvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GeoSot\EnvEditor;

use GeoSot\EnvEditor\Exceptions\EnvException;
use GeoSot\EnvEditor\Helpers\EntryObj;
use GeoSot\EnvEditor\Helpers\EnvFileContentManager;
use GeoSot\EnvEditor\Helpers\EnvFilesManager;
use GeoSot\EnvEditor\Helpers\EnvKeysManager;
Expand All @@ -17,25 +18,13 @@
*/
class EnvEditor
{
/**
* @var Repository
*/
protected $config;
protected Repository $config;

/**
* @var EnvKeysManager
*/
protected $keysManager;
protected EnvKeysManager $keysManager;

/**
* @var EnvFilesManager
*/
protected $filesManager;
protected EnvFilesManager $filesManager;

/**
* @var EnvFileContentManager
*/
protected $fileContentManager;
protected EnvFileContentManager $fileContentManager;

public function __construct(Repository $config, Filesystem $filesystem)
{
Expand All @@ -50,7 +39,7 @@ public function __construct(Repository $config, Filesystem $filesystem)
*
* @param string $fileName
*
* @return Collection
* @return Collection<int, EntryObj>
* @throws EnvException
*/
public function getEnvFileContent(string $fileName = ''): Collection
Expand All @@ -60,15 +49,10 @@ public function getEnvFileContent(string $fileName = ''): Collection

/**
* Check if key Exist in Current env.
*
* @param string $key
*
* @return bool
* @throws EnvException
*/
public function keyExists(string $key): bool
{
return $this->getKeysManager()->keyExists($key);
return $this->getKeysManager()->has($key);
}

/**
Expand All @@ -78,11 +62,10 @@ public function keyExists(string $key): bool
* @param mixed $default
*
* @return bool|float|int|string|null
* @throws EnvException
*/
public function getKey(string $key, $default = null)
public function getKey(string $key, mixed $default = null): float|bool|int|string|null
{
return $this->getKeysManager()->getKey($key, $default);
return $this->getKeysManager()->get($key, $default);
}

/**
Expand All @@ -95,9 +78,9 @@ public function getKey(string $key, $default = null)
* @return bool
* @throws EnvException
*/
public function addKey(string $key, $value, array $options = []): bool
public function addKey(string $key, mixed $value, array $options = []): bool
{
return $this->getKeysManager()->addKey($key, $value, $options);
return $this->getKeysManager()->add($key, $value, $options);
}

/**
Expand All @@ -109,9 +92,9 @@ public function addKey(string $key, $value, array $options = []): bool
* @return bool
* @throws EnvException
*/
public function editKey(string $keyToChange, $newValue): bool
public function editKey(string $keyToChange, mixed $newValue): bool
{
return $this->getKeysManager()->editKey($keyToChange, $newValue);
return $this->getKeysManager()->edit($keyToChange, $newValue);
}

/**
Expand All @@ -124,12 +107,13 @@ public function editKey(string $keyToChange, $newValue): bool
*/
public function deleteKey(string $key): bool
{
return $this->getKeysManager()->deleteKey($key);
return $this->getKeysManager()->delete($key);
}

/**
* Get all Backup Files.
*
* @return Collection<int, array{real_name:string, name:string, created_at:int, modified_at:int, created_at_formatted:string, modified_at_formatted:string, content:string, path:string,parsed_data:Collection<int, EntryObj>}>
* @throws EnvException
*/
public function getAllBackUps(): Collection
Expand All @@ -138,7 +122,7 @@ public function getAllBackUps(): Collection
}

/**
* uploadBackup.
* upload Backup.
*/
public function upload(UploadedFile $uploadedFile, bool $replaceCurrentEnv): File
{
Expand Down Expand Up @@ -196,13 +180,7 @@ public function restoreBackUp(string $fileName): bool
return $this->getFilesManager()->restoreBackup($fileName);
}

/**
* @param string $key
* @param mixed $default
*
* @return array|mixed
*/
public function config(string $key, $default = null)
public function config(string $key, mixed $default = null): mixed
{
return $this->config->get($key, $default);
}
Expand Down
96 changes: 96 additions & 0 deletions src/Helpers/EntryObj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace GeoSot\EnvEditor\Helpers;

use Illuminate\Support\Arr;

class EntryObj implements \JsonSerializable
{
public string $key;

/**
* @var int|string|null
*/
protected mixed $value;

public int $group = 0;

public int $index = 0;

protected bool $isSeparator = false;

/**
* @param string $key
* @param int|string|null $value
* @param int $group
* @param int $index
* @param bool $isSeparator
*/
public function __construct(string $key, mixed $value, int $group, int $index, bool $isSeparator = false)
{
$this->key = $key;
$this->value = $value;
$this->group = $group;
$this->index = $index;
$this->isSeparator = $isSeparator;
}

public static function parseEnvLine(string $line, int $group, int $index): self
{
$entry = explode('=', $line, 2);
$isSeparator = count($entry) === 1;

return new self(Arr::get($entry, 0), Arr::get($entry, 1), $group, $index, $isSeparator);
}

public static function makeKeysSeparator(int $groupIndex, int $index): self
{
return new self('', '', $groupIndex, $index, true);
}

public function getAsEnvLine(): string
{
return $this->isSeparator() ? '' : "$this->key=$this->value";
}

/**
* @return bool
*/
public function isSeparator(): bool
{
return $this->isSeparator;
}

/**
* @param mixed $default
* @return int|string|null|mixed
*/
public function getValue(mixed $default = null): mixed
{
return $this->value ?: $default;
}

/**
* @param int|string|null|mixed $value
*/
public function setValue(mixed $value): void
{
$this->value = $value;
}

/**
* @return array{key:string, value: int|string|null, group:int, index:int , isSeparator:bool}
*/
public function toArray(): array
{
return get_object_vars($this);
}

/**
* @return array{key:string, value: int|string|null, group:int, index:int , isSeparator:bool}
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}
Loading

0 comments on commit 0cf5ebe

Please sign in to comment.