-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Shahzada Modassir <[email protected]> Co-Authored-By: Coding Hasnain <[email protected]>
- Loading branch information
1 parent
801c18b
commit 6776e95
Showing
36 changed files
with
1,165 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dotenv\Backup; | ||
|
||
interface BackupInterface | ||
{ | ||
|
||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
?> | ||
?> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.