dependencies-upgrade

This commit is contained in:
RafficMohammed
2023-01-08 02:20:59 +05:30
parent 7870479b18
commit 49021a4497
1711 changed files with 74994 additions and 70803 deletions

View File

@@ -1,180 +0,0 @@
<?php
namespace Dotenv\Repository;
use Dotenv\Repository\Adapter\ArrayAdapter;
use InvalidArgumentException;
use ReturnTypeWillChange;
abstract class AbstractRepository implements RepositoryInterface
{
/**
* Are we immutable?
*
* @var bool
*/
private $immutable;
/**
* The record of loaded variables.
*
* @var \Dotenv\Repository\Adapter\ArrayAdapter
*/
private $loaded;
/**
* Create a new repository instance.
*
* @param bool $immutable
*
* @return void
*/
public function __construct($immutable)
{
$this->immutable = $immutable;
$this->loaded = new ArrayAdapter();
}
/**
* Get an environment variable.
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get($name)
{
if (!is_string($name) || '' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return $this->getInternal($name);
}
/**
* Get an environment variable.
*
* @param non-empty-string $name
*
* @return string|null
*/
abstract protected function getInternal($name);
/**
* Set an environment variable.
*
* @param string $name
* @param string|null $value
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function set($name, $value = null)
{
if (!is_string($name) || '' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
// Don't overwrite existing environment variables if we're immutable
// Ruby's dotenv does this with `ENV[key] ||= value`.
if ($this->immutable && $this->get($name) !== null && $this->loaded->get($name)->isEmpty()) {
return;
}
$this->setInternal($name, $value);
$this->loaded->set($name, '');
}
/**
* Set an environment variable.
*
* @param non-empty-string $name
* @param string|null $value
*
* @return void
*/
abstract protected function setInternal($name, $value = null);
/**
* Clear an environment variable.
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function clear($name)
{
if (!is_string($name) || '' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
// Don't clear anything if we're immutable.
if ($this->immutable) {
return;
}
$this->clearInternal($name);
}
/**
* Clear an environment variable.
*
* @param non-empty-string $name
*
* @return void
*/
abstract protected function clearInternal($name);
/**
* Tells whether environment variable has been defined.
*
* @param string $name
*
* @return bool
*/
public function has($name)
{
return is_string($name) && $name !== '' && $this->get($name) !== null;
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->has($offset);
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->clear($offset);
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
interface AdapterInterface extends ReaderInterface, WriterInterface
{
/**
* Create a new instance of the adapter, if it is available.
*
* @return \PhpOption\Option<\Dotenv\Repository\Adapter\AdapterInterface>
*/
public static function create();
}

View File

@@ -1,11 +1,40 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
class ApacheAdapter implements AvailabilityInterface, ReaderInterface, WriterInterface
final class ApacheAdapter implements AdapterInterface
{
/**
* Create a new apache adapter instance.
*
* @return void
*/
private function __construct()
{
//
}
/**
* Create a new instance of the adapter, if it is available.
*
* @return \PhpOption\Option<\Dotenv\Repository\Adapter\AdapterInterface>
*/
public static function create()
{
if (self::isSupported()) {
/** @var \PhpOption\Option<AdapterInterface> */
return Some::create(new self());
}
return None::create();
}
/**
* Determines if the adapter is supported.
*
@@ -13,54 +42,48 @@ class ApacheAdapter implements AvailabilityInterface, ReaderInterface, WriterInt
*
* @return bool
*/
public function isSupported()
private static function isSupported()
{
return function_exists('apache_getenv') && function_exists('apache_setenv');
return \function_exists('apache_getenv') && \function_exists('apache_setenv');
}
/**
* Get an environment variable, if it exists.
*
* This is intentionally not implemented, since this adapter exists only as
* a means to overwrite existing apache environment variables.
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string|null>
* @return \PhpOption\Option<string>
*/
public function get($name)
public function read(string $name)
{
return None::create();
/** @var \PhpOption\Option<string> */
return Option::fromValue(apache_getenv($name))->filter(static function ($value) {
return \is_string($value) && $value !== '';
});
}
/**
* Set an environment variable.
*
* Only if an existing apache variable exists do we overwrite it.
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string|null $value
* @param string $value
*
* @return void
* @return bool
*/
public function set($name, $value = null)
public function write(string $name, string $value)
{
if (apache_getenv($name) === false) {
return;
}
apache_setenv($name, (string) $value);
return apache_setenv($name, $value);
}
/**
* Clear an environment variable.
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return void
* @return bool
*/
public function clear($name)
public function delete(string $name)
{
// Nothing to do here.
return apache_setenv($name, '');
}
}

View File

@@ -1,67 +1,80 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
class ArrayAdapter implements AvailabilityInterface, ReaderInterface, WriterInterface
final class ArrayAdapter implements AdapterInterface
{
/**
* The variables and their values.
*
* @var array<non-empty-string,string|null>
* @var array<string,string>
*/
private $variables = [];
private $variables;
/**
* Determines if the adapter is supported.
* Create a new array adapter instance.
*
* @return void
*/
private function __construct()
{
$this->variables = [];
}
/**
* Create a new instance of the adapter, if it is available.
*
* @return \PhpOption\Option<\Dotenv\Repository\Adapter\AdapterInterface>
*/
public static function create()
{
/** @var \PhpOption\Option<AdapterInterface> */
return Some::create(new self());
}
/**
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
public function read(string $name)
{
return Option::fromArraysValue($this->variables, $name);
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function isSupported()
public function write(string $name, string $value)
{
$this->variables[$name] = $value;
return true;
}
/**
* Get an environment variable, if it exists.
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string|null>
* @return bool
*/
public function get($name)
{
if (!array_key_exists($name, $this->variables)) {
return None::create();
}
return Some::create($this->variables[$name]);
}
/**
* Set an environment variable.
*
* @param non-empty-string $name
* @param string|null $value
*
* @return void
*/
public function set($name, $value = null)
{
$this->variables[$name] = $value;
}
/**
* Clear an environment variable.
*
* @param non-empty-string $name
*
* @return void
*/
public function clear($name)
public function delete(string $name)
{
unset($this->variables[$name]);
return true;
}
}

View File

@@ -1,13 +0,0 @@
<?php
namespace Dotenv\Repository\Adapter;
interface AvailabilityInterface
{
/**
* Determines if the adapter is supported.
*
* @return bool
*/
public function isSupported();
}

View File

@@ -1,72 +1,89 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
class EnvConstAdapter implements AvailabilityInterface, ReaderInterface, WriterInterface
final class EnvConstAdapter implements AdapterInterface
{
/**
* Determines if the adapter is supported.
* Create a new env const adapter instance.
*
* @return void
*/
private function __construct()
{
//
}
/**
* Create a new instance of the adapter, if it is available.
*
* @return \PhpOption\Option<\Dotenv\Repository\Adapter\AdapterInterface>
*/
public static function create()
{
/** @var \PhpOption\Option<AdapterInterface> */
return Some::create(new self());
}
/**
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
public function read(string $name)
{
/** @var \PhpOption\Option<string> */
return Option::fromArraysValue($_ENV, $name)
->filter(static function ($value) {
return \is_scalar($value);
})
->map(static function ($value) {
if ($value === false) {
return 'false';
}
if ($value === true) {
return 'true';
}
/** @psalm-suppress PossiblyInvalidCast */
return (string) $value;
});
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function isSupported()
public function write(string $name, string $value)
{
$_ENV[$name] = $value;
return true;
}
/**
* Get an environment variable, if it exists.
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string|null>
* @return bool
*/
public function get($name)
{
if (!array_key_exists($name, $_ENV)) {
return None::create();
}
$value = $_ENV[$name];
if (is_scalar($value)) {
/** @var \PhpOption\Option<string|null> */
return Some::create((string) $value);
}
if (null === $value) {
/** @var \PhpOption\Option<string|null> */
return Some::create(null);
}
return None::create();
}
/**
* Set an environment variable.
*
* @param non-empty-string $name
* @param string|null $value
*
* @return void
*/
public function set($name, $value = null)
{
$_ENV[$name] = $value;
}
/**
* Clear an environment variable.
*
* @param non-empty-string $name
*
* @return void
*/
public function clear($name)
public function delete(string $name)
{
unset($_ENV[$name]);
return true;
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
final class GuardedWriter implements WriterInterface
{
/**
* The inner writer to use.
*
* @var \Dotenv\Repository\Adapter\WriterInterface
*/
private $writer;
/**
* The variable name allow list.
*
* @var string[]
*/
private $allowList;
/**
* Create a new guarded writer instance.
*
* @param \Dotenv\Repository\Adapter\WriterInterface $writer
* @param string[] $allowList
*
* @return void
*/
public function __construct(WriterInterface $writer, array $allowList)
{
$this->writer = $writer;
$this->allowList = $allowList;
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function write(string $name, string $value)
{
// Don't set non-allowed variables
if (!$this->isAllowed($name)) {
return false;
}
// Set the value on the inner writer
return $this->writer->write($name, $value);
}
/**
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return bool
*/
public function delete(string $name)
{
// Don't clear non-allowed variables
if (!$this->isAllowed($name)) {
return false;
}
// Set the value on the inner writer
return $this->writer->delete($name);
}
/**
* Determine if the given variable is allowed.
*
* @param non-empty-string $name
*
* @return bool
*/
private function isAllowed(string $name)
{
return \in_array($name, $this->allowList, true);
}
}

View File

@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
final class ImmutableWriter implements WriterInterface
{
/**
* The inner writer to use.
*
* @var \Dotenv\Repository\Adapter\WriterInterface
*/
private $writer;
/**
* The inner reader to use.
*
* @var \Dotenv\Repository\Adapter\ReaderInterface
*/
private $reader;
/**
* The record of loaded variables.
*
* @var array<string,string>
*/
private $loaded;
/**
* Create a new immutable writer instance.
*
* @param \Dotenv\Repository\Adapter\WriterInterface $writer
* @param \Dotenv\Repository\Adapter\ReaderInterface $reader
*
* @return void
*/
public function __construct(WriterInterface $writer, ReaderInterface $reader)
{
$this->writer = $writer;
$this->reader = $reader;
$this->loaded = [];
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function write(string $name, string $value)
{
// Don't overwrite existing environment variables
// Ruby's dotenv does this with `ENV[key] ||= value`
if ($this->isExternallyDefined($name)) {
return false;
}
// Set the value on the inner writer
if (!$this->writer->write($name, $value)) {
return false;
}
// Record that we have loaded the variable
$this->loaded[$name] = '';
return true;
}
/**
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return bool
*/
public function delete(string $name)
{
// Don't clear existing environment variables
if ($this->isExternallyDefined($name)) {
return false;
}
// Clear the value on the inner writer
if (!$this->writer->delete($name)) {
return false;
}
// Leave the variable as fair game
unset($this->loaded[$name]);
return true;
}
/**
* Determine if the given variable is externally defined.
*
* That is, is it an "existing" variable.
*
* @param non-empty-string $name
*
* @return bool
*/
private function isExternallyDefined(string $name)
{
return $this->reader->read($name)->isDefined() && !isset($this->loaded[$name]);
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
use PhpOption\None;
final class MultiReader implements ReaderInterface
{
/**
* The set of readers to use.
*
* @var \Dotenv\Repository\Adapter\ReaderInterface[]
*/
private $readers;
/**
* Create a new multi-reader instance.
*
* @param \Dotenv\Repository\Adapter\ReaderInterface[] $readers
*
* @return void
*/
public function __construct(array $readers)
{
$this->readers = $readers;
}
/**
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
public function read(string $name)
{
foreach ($this->readers as $reader) {
$result = $reader->read($name);
if ($result->isDefined()) {
return $result;
}
}
return None::create();
}
}

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
final class MultiWriter implements WriterInterface
{
/**
* The set of writers to use.
*
* @var \Dotenv\Repository\Adapter\WriterInterface[]
*/
private $writers;
/**
* Create a new multi-writer instance.
*
* @param \Dotenv\Repository\Adapter\WriterInterface[] $writers
*
* @return void
*/
public function __construct(array $writers)
{
$this->writers = $writers;
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function write(string $name, string $value)
{
foreach ($this->writers as $writers) {
if (!$writers->write($name, $value)) {
return false;
}
}
return true;
}
/**
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return bool
*/
public function delete(string $name)
{
foreach ($this->writers as $writers) {
if (!$writers->delete($name)) {
return false;
}
}
return true;
}
}

View File

@@ -1,56 +1,91 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
class PutenvAdapter implements AvailabilityInterface, ReaderInterface, WriterInterface
final class PutenvAdapter implements AdapterInterface
{
/**
* Create a new putenv adapter instance.
*
* @return void
*/
private function __construct()
{
//
}
/**
* Create a new instance of the adapter, if it is available.
*
* @return \PhpOption\Option<\Dotenv\Repository\Adapter\AdapterInterface>
*/
public static function create()
{
if (self::isSupported()) {
/** @var \PhpOption\Option<AdapterInterface> */
return Some::create(new self());
}
return None::create();
}
/**
* Determines if the adapter is supported.
*
* @return bool
*/
public function isSupported()
private static function isSupported()
{
return function_exists('getenv') && function_exists('putenv');
return \function_exists('getenv') && \function_exists('putenv');
}
/**
* Get an environment variable, if it exists.
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string|null>
* @return \PhpOption\Option<string>
*/
public function get($name)
public function read(string $name)
{
/** @var \PhpOption\Option<string|null> */
return Option::fromValue(getenv($name), false);
/** @var \PhpOption\Option<string> */
return Option::fromValue(\getenv($name), false)->filter(static function ($value) {
return \is_string($value);
});
}
/**
* Set an environment variable.
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string|null $value
* @param string $value
*
* @return void
* @return bool
*/
public function set($name, $value = null)
public function write(string $name, string $value)
{
putenv("$name=$value");
\putenv("$name=$value");
return true;
}
/**
* Clear an environment variable.
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return void
* @return bool
*/
public function clear($name)
public function delete(string $name)
{
putenv($name);
\putenv($name);
return true;
}
}

View File

@@ -1,15 +1,17 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
interface ReaderInterface extends AvailabilityInterface
interface ReaderInterface
{
/**
* Get an environment variable, if it exists.
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string|null>
* @return \PhpOption\Option<string>
*/
public function get($name);
public function read(string $name);
}

View File

@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
final class ReplacingWriter implements WriterInterface
{
/**
* The inner writer to use.
*
* @var \Dotenv\Repository\Adapter\WriterInterface
*/
private $writer;
/**
* The inner reader to use.
*
* @var \Dotenv\Repository\Adapter\ReaderInterface
*/
private $reader;
/**
* The record of seen variables.
*
* @var array<string,string>
*/
private $seen;
/**
* Create a new replacement writer instance.
*
* @param \Dotenv\Repository\Adapter\WriterInterface $writer
* @param \Dotenv\Repository\Adapter\ReaderInterface $reader
*
* @return void
*/
public function __construct(WriterInterface $writer, ReaderInterface $reader)
{
$this->writer = $writer;
$this->reader = $reader;
$this->seen = [];
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function write(string $name, string $value)
{
if ($this->exists($name)) {
return $this->writer->write($name, $value);
}
// succeed if nothing to do
return true;
}
/**
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return bool
*/
public function delete(string $name)
{
if ($this->exists($name)) {
return $this->writer->delete($name);
}
// succeed if nothing to do
return true;
}
/**
* Does the given environment variable exist.
*
* Returns true if it currently exists, or existed at any point in the past
* that we are aware of.
*
* @param non-empty-string $name
*
* @return bool
*/
private function exists(string $name)
{
if (isset($this->seen[$name])) {
return true;
}
if ($this->reader->read($name)->isDefined()) {
$this->seen[$name] = '';
return true;
}
return false;
}
}

View File

@@ -1,72 +1,89 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
use PhpOption\None;
use PhpOption\Option;
use PhpOption\Some;
class ServerConstAdapter implements AvailabilityInterface, ReaderInterface, WriterInterface
final class ServerConstAdapter implements AdapterInterface
{
/**
* Determines if the adapter is supported.
* Create a new server const adapter instance.
*
* @return void
*/
private function __construct()
{
//
}
/**
* Create a new instance of the adapter, if it is available.
*
* @return \PhpOption\Option<\Dotenv\Repository\Adapter\AdapterInterface>
*/
public static function create()
{
/** @var \PhpOption\Option<AdapterInterface> */
return Some::create(new self());
}
/**
* Read an environment variable, if it exists.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string>
*/
public function read(string $name)
{
/** @var \PhpOption\Option<string> */
return Option::fromArraysValue($_SERVER, $name)
->filter(static function ($value) {
return \is_scalar($value);
})
->map(static function ($value) {
if ($value === false) {
return 'false';
}
if ($value === true) {
return 'true';
}
/** @psalm-suppress PossiblyInvalidCast */
return (string) $value;
});
}
/**
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string $value
*
* @return bool
*/
public function isSupported()
public function write(string $name, string $value)
{
$_SERVER[$name] = $value;
return true;
}
/**
* Get an environment variable, if it exists.
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return \PhpOption\Option<string|null>
* @return bool
*/
public function get($name)
{
if (!array_key_exists($name, $_SERVER)) {
return None::create();
}
$value = $_SERVER[$name];
if (is_scalar($value)) {
/** @var \PhpOption\Option<string|null> */
return Some::create((string) $value);
}
if (null === $value) {
/** @var \PhpOption\Option<string|null> */
return Some::create(null);
}
return None::create();
}
/**
* Set an environment variable.
*
* @param non-empty-string $name
* @param string|null $value
*
* @return void
*/
public function set($name, $value = null)
{
$_SERVER[$name] = $value;
}
/**
* Clear an environment variable.
*
* @param non-empty-string $name
*
* @return void
*/
public function clear($name)
public function delete(string $name)
{
unset($_SERVER[$name]);
return true;
}
}

View File

@@ -1,25 +1,27 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository\Adapter;
interface WriterInterface extends AvailabilityInterface
interface WriterInterface
{
/**
* Set an environment variable.
* Write to an environment variable, if possible.
*
* @param non-empty-string $name
* @param string|null $value
* @param string $value
*
* @return void
* @return bool
*/
public function set($name, $value = null);
public function write(string $name, string $value);
/**
* Clear an environment variable.
* Delete an environment variable, if possible.
*
* @param non-empty-string $name
*
* @return void
* @return bool
*/
public function clear($name);
public function delete(string $name);
}

View File

@@ -1,86 +1,107 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository;
class AdapterRepository extends AbstractRepository
use Dotenv\Repository\Adapter\ReaderInterface;
use Dotenv\Repository\Adapter\WriterInterface;
use InvalidArgumentException;
final class AdapterRepository implements RepositoryInterface
{
/**
* The set of readers to use.
* The reader to use.
*
* @var \Dotenv\Repository\Adapter\ReaderInterface[]
* @var \Dotenv\Repository\Adapter\ReaderInterface
*/
protected $readers;
private $reader;
/**
* The set of writers to use.
* The writer to use.
*
* @var \Dotenv\Repository\Adapter\WriterInterface[]
* @var \Dotenv\Repository\Adapter\WriterInterface
*/
protected $writers;
private $writer;
/**
* Create a new adapter repository instance.
*
* @param \Dotenv\Repository\Adapter\ReaderInterface[] $readers
* @param \Dotenv\Repository\Adapter\WriterInterface[] $writers
* @param bool $immutable
* @param \Dotenv\Repository\Adapter\ReaderInterface $reader
* @param \Dotenv\Repository\Adapter\WriterInterface $writer
*
* @return void
*/
public function __construct(array $readers, array $writers, $immutable)
public function __construct(ReaderInterface $reader, WriterInterface $writer)
{
$this->readers = $readers;
$this->writers = $writers;
parent::__construct($immutable);
$this->reader = $reader;
$this->writer = $writer;
}
/**
* Determine if the given environment variable is defined.
*
* @param string $name
*
* @return bool
*/
public function has(string $name)
{
return '' !== $name && $this->reader->read($name)->isDefined();
}
/**
* Get an environment variable.
*
* We do this by querying our readers sequentially.
* @param string $name
*
* @param non-empty-string $name
* @throws \InvalidArgumentException
*
* @return string|null
*/
protected function getInternal($name)
public function get(string $name)
{
foreach ($this->readers as $reader) {
$result = $reader->get($name);
if ($result->isDefined()) {
return $result->get();
}
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return null;
return $this->reader->read($name)->getOrElse(null);
}
/**
* Set an environment variable.
*
* @param non-empty-string $name
* @param string|null $value
* @param string $name
* @param string $value
*
* @return void
* @throws \InvalidArgumentException
*
* @return bool
*/
protected function setInternal($name, $value = null)
public function set(string $name, string $value)
{
foreach ($this->writers as $writers) {
$writers->set($name, $value);
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return $this->writer->write($name, $value);
}
/**
* Clear an environment variable.
*
* @param non-empty-string $name
* @param string $name
*
* @return void
* @throws \InvalidArgumentException
*
* @return bool
*/
protected function clearInternal($name)
public function clear(string $name)
{
foreach ($this->writers as $writers) {
$writers->clear($name);
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
}
return $this->writer->delete($name);
}
}

View File

@@ -1,26 +1,43 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository;
use Dotenv\Repository\Adapter\ApacheAdapter;
use Dotenv\Repository\Adapter\AvailabilityInterface;
use Dotenv\Repository\Adapter\AdapterInterface;
use Dotenv\Repository\Adapter\EnvConstAdapter;
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\Adapter\GuardedWriter;
use Dotenv\Repository\Adapter\ImmutableWriter;
use Dotenv\Repository\Adapter\MultiReader;
use Dotenv\Repository\Adapter\MultiWriter;
use Dotenv\Repository\Adapter\ReaderInterface;
use Dotenv\Repository\Adapter\ServerConstAdapter;
use Dotenv\Repository\Adapter\WriterInterface;
use InvalidArgumentException;
use PhpOption\Some;
use ReflectionClass;
class RepositoryBuilder
final class RepositoryBuilder
{
/**
* The set of default adapters.
*/
private const DEFAULT_ADAPTERS = [
ServerConstAdapter::class,
EnvConstAdapter::class,
];
/**
* The set of readers to use.
*
* @var \Dotenv\Repository\Adapter\ReaderInterface[]|null
* @var \Dotenv\Repository\Adapter\ReaderInterface[]
*/
private $readers;
/**
* The set of writers to use.
*
* @var \Dotenv\Repository\Adapter\WriterInterface[]|null
* @var \Dotenv\Repository\Adapter\WriterInterface[]
*/
private $writers;
@@ -32,57 +49,182 @@ class RepositoryBuilder
private $immutable;
/**
* Create a new repository builder instance.
* The variable name allow list.
*
* @param \Dotenv\Repository\Adapter\ReaderInterface[]|null $readers
* @param \Dotenv\Repository\Adapter\WriterInterface[]|null $writers
* @param bool $immutable
*
* @return void
* @var string[]|null
*/
private function __construct(array $readers = null, array $writers = null, $immutable = false)
{
$this->readers = $readers;
$this->writers = $writers;
$this->immutable = $immutable;
}
private $allowList;
/**
* Create a new repository builder instance.
*
* @param \Dotenv\Repository\Adapter\ReaderInterface[] $readers
* @param \Dotenv\Repository\Adapter\WriterInterface[] $writers
* @param bool $immutable
* @param string[]|null $allowList
*
* @return void
*/
private function __construct(array $readers = [], array $writers = [], bool $immutable = false, array $allowList = null)
{
$this->readers = $readers;
$this->writers = $writers;
$this->immutable = $immutable;
$this->allowList = $allowList;
}
/**
* Create a new repository builder instance with no adapters added.
*
* @return \Dotenv\Repository\RepositoryBuilder
*/
public static function create()
public static function createWithNoAdapters()
{
return new self();
}
/**
* Creates a repository builder with the given readers.
*
* @param \Dotenv\Repository\Adapter\ReaderInterface[]|null $readers
* Create a new repository builder instance with the default adapters added.
*
* @return \Dotenv\Repository\RepositoryBuilder
*/
public function withReaders(array $readers = null)
public static function createWithDefaultAdapters()
{
$readers = $readers === null ? null : self::filterByAvailability($readers);
$adapters = \iterator_to_array(self::defaultAdapters());
return new self($readers, $this->writers, $this->immutable);
return new self($adapters, $adapters);
}
/**
* Creates a repository builder with the given writers.
* Return the array of default adapters.
*
* @param \Dotenv\Repository\Adapter\WriterInterface[]|null $writers
* @return \Generator<\Dotenv\Repository\Adapter\AdapterInterface>
*/
private static function defaultAdapters()
{
foreach (self::DEFAULT_ADAPTERS as $adapter) {
$instance = $adapter::create();
if ($instance->isDefined()) {
yield $instance->get();
}
}
}
/**
* Determine if the given name if of an adapterclass.
*
* @param string $name
*
* @return bool
*/
private static function isAnAdapterClass(string $name)
{
if (!\class_exists($name)) {
return false;
}
return (new ReflectionClass($name))->implementsInterface(AdapterInterface::class);
}
/**
* Creates a repository builder with the given reader added.
*
* Accepts either a reader instance, or a class-string for an adapter. If
* the adapter is not supported, then we silently skip adding it.
*
* @param \Dotenv\Repository\Adapter\ReaderInterface|string $reader
*
* @throws \InvalidArgumentException
*
* @return \Dotenv\Repository\RepositoryBuilder
*/
public function withWriters(array $writers = null)
public function addReader($reader)
{
$writers = $writers === null ? null : self::filterByAvailability($writers);
if (!(\is_string($reader) && self::isAnAdapterClass($reader)) && !($reader instanceof ReaderInterface)) {
throw new InvalidArgumentException(
\sprintf(
'Expected either an instance of %s or a class-string implementing %s',
ReaderInterface::class,
AdapterInterface::class
)
);
}
return new self($this->readers, $writers, $this->immutable);
$optional = Some::create($reader)->flatMap(static function ($reader) {
return \is_string($reader) ? $reader::create() : Some::create($reader);
});
$readers = \array_merge($this->readers, \iterator_to_array($optional));
return new self($readers, $this->writers, $this->immutable, $this->allowList);
}
/**
* Creates a repository builder with the given writer added.
*
* Accepts either a writer instance, or a class-string for an adapter. If
* the adapter is not supported, then we silently skip adding it.
*
* @param \Dotenv\Repository\Adapter\WriterInterface|string $writer
*
* @throws \InvalidArgumentException
*
* @return \Dotenv\Repository\RepositoryBuilder
*/
public function addWriter($writer)
{
if (!(\is_string($writer) && self::isAnAdapterClass($writer)) && !($writer instanceof WriterInterface)) {
throw new InvalidArgumentException(
\sprintf(
'Expected either an instance of %s or a class-string implementing %s',
WriterInterface::class,
AdapterInterface::class
)
);
}
$optional = Some::create($writer)->flatMap(static function ($writer) {
return \is_string($writer) ? $writer::create() : Some::create($writer);
});
$writers = \array_merge($this->writers, \iterator_to_array($optional));
return new self($this->readers, $writers, $this->immutable, $this->allowList);
}
/**
* Creates a repository builder with the given adapter added.
*
* Accepts either an adapter instance, or a class-string for an adapter. If
* the adapter is not supported, then we silently skip adding it. We will
* add the adapter as both a reader and a writer.
*
* @param \Dotenv\Repository\Adapter\WriterInterface|string $adapter
*
* @throws \InvalidArgumentException
*
* @return \Dotenv\Repository\RepositoryBuilder
*/
public function addAdapter($adapter)
{
if (!(\is_string($adapter) && self::isAnAdapterClass($adapter)) && !($adapter instanceof AdapterInterface)) {
throw new InvalidArgumentException(
\sprintf(
'Expected either an instance of %s or a class-string implementing %s',
WriterInterface::class,
AdapterInterface::class
)
);
}
$optional = Some::create($adapter)->flatMap(static function ($adapter) {
return \is_string($adapter) ? $adapter::create() : Some::create($adapter);
});
$readers = \array_merge($this->readers, \iterator_to_array($optional));
$writers = \array_merge($this->writers, \iterator_to_array($optional));
return new self($readers, $writers, $this->immutable, $this->allowList);
}
/**
@@ -92,7 +234,19 @@ class RepositoryBuilder
*/
public function immutable()
{
return new self($this->readers, $this->writers, true);
return new self($this->readers, $this->writers, true, $this->allowList);
}
/**
* Creates a repository builder with the given allow list.
*
* @param string[]|null $allowList
*
* @return \Dotenv\Repository\RepositoryBuilder
*/
public function allowList(array $allowList = null)
{
return new self($this->readers, $this->writers, $this->immutable, $allowList);
}
/**
@@ -102,43 +256,17 @@ class RepositoryBuilder
*/
public function make()
{
if ($this->readers === null || $this->writers === null) {
$defaults = self::defaultAdapters();
$reader = new MultiReader($this->readers);
$writer = new MultiWriter($this->writers);
if ($this->immutable) {
$writer = new ImmutableWriter($writer, $reader);
}
return new AdapterRepository(
$this->readers === null ? $defaults : $this->readers,
$this->writers === null ? $defaults : $this->writers,
$this->immutable
);
}
if ($this->allowList !== null) {
$writer = new GuardedWriter($writer, $this->allowList);
}
/**
* Return the array of default adapters.
*
* @return \Dotenv\Repository\Adapter\AvailabilityInterface[]
*/
private static function defaultAdapters()
{
return self::filterByAvailability([
new ApacheAdapter(),
new EnvConstAdapter(),
new ServerConstAdapter(),
new PutenvAdapter(),
]);
}
/**
* Filter an array of adapters to only those that are supported.
*
* @param \Dotenv\Repository\Adapter\AvailabilityInterface[] $adapters
*
* @return \Dotenv\Repository\Adapter\AvailabilityInterface[]
*/
private static function filterByAvailability(array $adapters)
{
return array_filter($adapters, function (AvailabilityInterface $adapter) {
return $adapter->isSupported();
});
return new AdapterRepository($reader, $writer);
}
}

View File

@@ -1,22 +1,19 @@
<?php
declare(strict_types=1);
namespace Dotenv\Repository;
use ArrayAccess;
/**
* @extends \ArrayAccess<string,string|null>
*/
interface RepositoryInterface extends ArrayAccess
interface RepositoryInterface
{
/**
* Tells whether environment variable has been defined.
* Determine if the given environment variable is defined.
*
* @param string $name
*
* @return bool
*/
public function has($name);
public function has(string $name);
/**
* Get an environment variable.
@@ -27,19 +24,19 @@ interface RepositoryInterface extends ArrayAccess
*
* @return string|null
*/
public function get($name);
public function get(string $name);
/**
* Set an environment variable.
*
* @param string $name
* @param string|null $value
* @param string $name
* @param string $value
*
* @throws \InvalidArgumentException
*
* @return void
* @return bool
*/
public function set($name, $value = null);
public function set(string $name, string $value);
/**
* Clear an environment variable.
@@ -48,7 +45,7 @@ interface RepositoryInterface extends ArrayAccess
*
* @throws \InvalidArgumentException
*
* @return void
* @return bool
*/
public function clear($name);
public function clear(string $name);
}