Skip to content

Commit

Permalink
commit 2.0 to 3.0
Browse files Browse the repository at this point in the history
Co-Authored-By: Shahzada Modassir <[email protected]>
Co-Authored-By: Coding Hasnain <[email protected]>
  • Loading branch information
3 people committed Nov 2, 2024
1 parent 801c18b commit 6776e95
Show file tree
Hide file tree
Showing 36 changed files with 1,165 additions and 325 deletions.
62 changes: 62 additions & 0 deletions src/Backup/Backup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Dotenv\Backup;

use Dotenv\Exception\VarNotFoundException;
use Dotenv\Resolver\Resolve;
use Dotenv\Resolver\Reject;
use Dotenv\Parser\Parser;
use Dotenv\Parser\ParserInterface;

final class Backup implements BackupInterface
{
private $parser;
private $variables;
public function __construct(array $variables, ParserInterface $parser)
{
$this->variables = $variables;
$this->parser = $parser;
}

private function setEnv(string $name, $value)
{
if ($this->isSupport()) {
\putenv("$name=$value");
}

$_SERVER[$name] = $_ENV = $value;
}

private function isSupport()
{
return \function_exists('putenv') && \function_exists('getenv');
}

public static function create()
{
return new self([], new Parser());
}

public function get(string $name)
{
return $this->getOrElse($name)->failMap(static function($error) {
throw new VarNotFoundException($error);
})->done()->get();
}

public function set(string $name, $value)
{
$this->variables[$name] = $value;
$this->setEnv($name, $value);
return Resolve::create($this->variables);
}

private function getOrElse(string $name)
{
return isset($this->variables[$name]) ? Resolve::create($this->variables[$name]) :
Reject::create('Try to get undefined environment variable [%s].', \sprintf($name));
}
}
?>
11 changes: 11 additions & 0 deletions src/Backup/BackupInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Dotenv\Backup;

interface BackupInterface
{

}
?>
164 changes: 88 additions & 76 deletions src/Dotenv.php
Original file line number Diff line number Diff line change
@@ -1,137 +1,149 @@
<?php

declare(strict_types=1);
/*
│––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Dotenv v1.0.3
│––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*/

/**
* The PHP Dotenv loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automatically.
*
* The (dotenv) Github repository.
* @see https://github.com/lazervel/dotenv
*
* @author Shahzada Modassir <shahzadamodassir@gmail.com>
* @author Shahzadi Afsara <shahzadiafsara@gmail.com>
*
* @copyright (c) Shahzada Modassir
* @copyright (c) Shahzadi Afsara
*
* @license MIT License
* @see https://github.com/lazervel/dotenv/blob/main/LICENSE
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Dotenv;

use Dotenv\Loader\LoaderInterface;
use Dotenv\Loader\Loader;
use Dotenv\Parser\ParserInterface;
use Dotenv\Parser\Parser;
use Dotenv\Env\EnvInterface;
use Dotenv\Env\Env;
use Dotenv\Env\EnvBuilder;
use Dotenv\Env\ContentStore;
use Dotenv\Backup\BackupInterface;
use Dotenv\Backup\Backup;
use Dotenv\Validator\Validator;
use Dotenv\Reader\StoreBuilder;
use Dotenv\Reader\ReaderInterface;

class Dotenv
{
/**
*
* @var \Dotenv\Env\EnvInterface $env
* @var \Dotenv\Loader\LoaderInterface $loader
* @var \Dotenv\Parser\ParserInterface $parser
*/
private $env, $loader, $parser;

public const VERSION = '1.2.0';
private $reader, $loader, $parser, $backup;

/**
* Create a new dotenv instance.
*
* @param \Dotenv\Env\EnvInterface $env [required]
* @param \Dotenv\Loader\LoaderInterface $loader [required]
* @param \Dotenv\Parser\ParserInterface $parser [required]
* @param \Dotenv\Reader\ReaderInterface $reader [required]
* @param \Dotenv\Loader\LoaderInterface $loader [required]
* @param \Dotenv\Parser\ParserInterface $parser [required]
* @param \Dotenv\Backup\BackupInterface $backup [required]
*
* @return void
*/
public function __construct(EnvInterface $env, LoaderInterface $loader, ParserInterface $parser)
public function __construct(ReaderInterface $reader, LoaderInterface $loader, ParserInterface $parser, BackupInterface $backup)
{
$this->reader = $reader;
$this->loader = $loader;
$this->parser = $parser;
$this->env = $env;
$this->backup = $backup;
}

/**
* Create a new dotenv instance.
*
* @param string $path [required]
* @param string|null $name [optional]
* @param string|null $fileEncoding [optional]
* @param string|string[] $paths [required]
* @param string|string[]|null $names [optional]
* @param bool $shortCircuit [optional]
* @param string|null $fileEncoding [optional]
*
* @return \Dotenv\Dotenv
*/
public static function doConfig(string $path, ?string $name = null, ?string $fileEncoding = null)
public static function process($paths, $names = null, bool $shortCircuit = true, ?string $fileEncoding = null)
{
return self::create($path, $name, $fileEncoding);
return self::create(Backup::create(), $paths, $names, $shortCircuit, $fileEncoding);
}

/**
*
* @param string|array $variables [required]
* @return \Validator\Validator
*
* @param \Dotenv\Backup\BackupInterface $backup [required]
* @param string|string[] $paths [required]
* @param string|string[]|null $names [optional]
* @param bool $shortCircuit [optional]
* @param string|null $fileEncoding [optional]
*
* @return \Dotenv\Dotenv
*/
public function required($variables)
public static function create(BackupInterface $backup, $paths, $names = null, bool $shortCircuit = true, ?string $fileEncoding = null)
{
return (new Validator((array) $variables))->required();
($reader = ($names===null ? StoreBuilder::createWithName() : StoreBuilder::createWithoutName())
->addPath($paths)
->addName($names)
->shortCircuit($shortCircuit));
return new self($reader->fileEncoding($fileEncoding)->store(), new Loader(), new Parser(), $backup);
}

/**
*
* @param string|array $variables [required]
* @return \Validator\Validator
*
* @return array
*/
public function ifPresent($variables)
public function load()
{
return new Validator((array) $variables);
$entries = $this->parser->parse($this->reader->read());
$this->loader->load($this->backup, $entries);
return $entries;
}

/**
*
* @param string $content [required]
* @return array<string,string|null>
* @param string $content [required]
* @return array
*/
public function parse(string $content)
{
$dotenv = new self(new ContentStore($content), new Loader(), new Parser());
return $dotenv->load();
$entries = $this->parser->parse($content);
$this->loader->load($this->backup, $entries);
return $entries;
}

/*
│––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
│––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*/
public function load()
/**
*
* @return array
*/
public function safeLoad()
{
return $this->loader->load($this->parser->parse($this->env->read()));
try {
return $this->load();
} catch (InvalidPathException $e) {
// suppressing exception
return [];
}
}

/*
│––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
│––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*/
public function safeLoad()
/**
*
* @param string|string[] $variables [required]
* @return \Dotenv\Validator\Validator
*/
public function ifPresent($variables)
{
try { return $this->load(); } catch(\Exception $e) { return []; }
return new Validator($this->backup, (array) $variables);
}

/**
* Create a new dotenv instance.
*
* @param string $path [required]
* @param string|null $name [optional]
* @param string|null $fileEncoding [optional]
* @param string|string[] $variables [required]
*
* @return \Dotenv\Dotenv
* @throws \Dotenv\Exception\ValidationException
*
* @return \Dotenv\Validator\Validator
*/
public static function create(string $path, ?string $name = null, ?string $fileEncoding = null)
public function required($variables)
{
$EnvBuilder = EnvBuilder::make($path, $name);
return new self($EnvBuilder::fileEncoding($fileEncoding)->store(), new Loader(), new Parser());
return (new Validator($this->backup, (array) $variables))->required();
}
}
?>
?>
35 changes: 0 additions & 35 deletions src/Env/ContentStore.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/Env/EnvBuilder.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Env/EnvInterface.php

This file was deleted.

Loading

0 comments on commit 6776e95

Please sign in to comment.