Skip to content

Commit

Permalink
Merge pull request #1 from MajidMohammadian/master
Browse files Browse the repository at this point in the history
change service for add some basic method
  • Loading branch information
MajidMohammadian authored Oct 24, 2023
2 parents 1f4a7e2 + 027825d commit 567a25e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/Facades/JEnvModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace JobMetric\EnvModifier\Facades;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Facade;

/**
* @method static mixed get(Model $model, string $key = null, string $locale = null)
* @method static void store(Model $model, array $data = [])
* @method static void delete(Model $model, string $locale = null)
* @method static static setPath(string $path)
* @method static array get(...$keys)
* @method static static set(array $data)
* @method static static delete(...$keys)
* @method static bool has($key)
*
* @see \JobMetric\EnvModifier\JEnvModifier
*/
Expand Down
150 changes: 130 additions & 20 deletions src/JEnvModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace JobMetric\EnvModifier;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use JobMetric\EnvModifier\Exceptions\EnvFileNotFoundException;

class JEnvModifier
{
Expand All @@ -17,54 +17,112 @@ class JEnvModifier
/**
* The env file path.
*/
private string $envPath;
private string $filePath;

/**
* Create a new EnvModifier instance.
*
* @param Application $app
*
* @return void
* @throws BindingResolutionException
*/
public function __construct(Application $app)
{
$this->app = $app;

$this->envPath = base_path('.env');
$this->filePath = base_path('.env');
}

/**
* set env file path
*
* @param string $path
*
* @return void
* @return static
* @throws EnvFileNotFoundException
*/
public function setPath(string $path): void
public function setPath(string $path): static
{
$this->envPath = $path;
if (!file_exists($path)) {
throw new EnvFileNotFoundException($path);
}

$this->filePath = $path;

return $this;
}

/**
* set env data
* get single env data
*
* @param string $key
*
* @return string
* @throws EnvFileNotFoundException
*/
private function getKey(string $key): string
{
$contentFile = $this->getContent();

preg_match("/^{$key}=(.*)$/m", $contentFile, $matches);

return trim($matches[1] ?? '');
}

/**
* get data in env data
*
* @param $keys
*
* @return array
*/
public function get(...$keys): array
{
$data = [];
foreach ($keys as $key) {
$data = array_merge($data, collect($key)->flatMap(function ($item) {
return [$item => $this->getKey($item)];
})->toArray());
}

return $data;
}

/**
* set single env data
*
* @param string $key
* @param mixed $value
*
* @return void
* @throws EnvFileNotFoundException
*/
public function set(string $key, mixed $value): void
private function setKey(string $key, mixed $value): void
{
$envFile = file_get_contents($this->envPath);
$contentFile = $this->getContent();

if (preg_match("/\n${key}=.*$/", $envFile)) {
$envFile = preg_replace("/\n${key}=.*$/", "\n${key}=${value}", $envFile);
if (preg_match("/^{$key}=/m", $contentFile)) {
$contentFile = preg_replace("/^{$key}=.*/m", "{$key}={$value}", $contentFile);
} else {
$envFile .= "\n${key}=${value}";
$contentFile .= PHP_EOL . "{$key}={$value}";
}

file_put_contents($this->envPath, $envFile);
file_put_contents($this->filePath, $contentFile);
}

/**
* set list of data in env data
*
* @param array $data
*
* @return static
* @throws EnvFileNotFoundException
*/
public function set(array $data): static
{
collect($data)->each(function ($value, $key) {
$this->setKey($key, $value);
});

return $this;
}

/**
Expand All @@ -73,11 +131,63 @@ public function set(string $key, mixed $value): void
* @param string $key
*
* @return void
* @throws EnvFileNotFoundException
*/
private function deleteKey(string $key): void
{
$contentFile = $this->getContent();

$contentFile = preg_replace("/^{$key}=.*/m", '', $contentFile);

file_put_contents($this->filePath, $contentFile);
}

/**
* get list of data in env data
*
* @param $keys
*
* @return static
* @throws EnvFileNotFoundException
*/
public function delete(...$keys): static
{
foreach ($keys as $key) {
collect($key)->each(function ($item) {
$this->deleteKey($item);
});
}

return $this;
}

/**
* has key in env data
*
* @param string $key
*
* @return bool
* @throws EnvFileNotFoundException
*/
public function delete(string $key): void
public function has(string $key): bool
{
$envFile = file_get_contents($this->envPath);
$envFile = preg_replace("/\n${key}=.*$/", '', $envFile);
file_put_contents($this->envPath, $envFile);
$contentFile = $this->getContent();

return preg_match("/^{$key}=(.*)$/m", $contentFile);
}

/**
* get env content
*
* @return string
* @throws EnvFileNotFoundException
*/
private function getContent(): string
{
if (!file_exists($this->filePath)) {
throw new EnvFileNotFoundException($this->filePath);
}

return file_get_contents($this->filePath);
}
}

0 comments on commit 567a25e

Please sign in to comment.