Files
apimacro/vendor/vlucas/phpdotenv/src/Repository/AdapterRepository.php

108 lines
2.3 KiB
PHP
Raw Normal View History

2024-05-07 12:17:25 +02:00
<?php
2024-08-13 13:44:16 +00:00
declare(strict_types=1);
2024-05-07 12:17:25 +02:00
namespace Dotenv\Repository;
2024-08-13 13:44:16 +00:00
use Dotenv\Repository\Adapter\ReaderInterface;
use Dotenv\Repository\Adapter\WriterInterface;
use InvalidArgumentException;
final class AdapterRepository implements RepositoryInterface
2024-05-07 12:17:25 +02:00
{
/**
2024-08-13 13:44:16 +00:00
* The reader to use.
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @var \Dotenv\Repository\Adapter\ReaderInterface
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
private $reader;
2024-05-07 12:17:25 +02:00
/**
2024-08-13 13:44:16 +00:00
* The writer to use.
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @var \Dotenv\Repository\Adapter\WriterInterface
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
private $writer;
2024-05-07 12:17:25 +02:00
/**
* Create a new adapter repository instance.
*
2024-08-13 13:44:16 +00:00
* @param \Dotenv\Repository\Adapter\ReaderInterface $reader
* @param \Dotenv\Repository\Adapter\WriterInterface $writer
2024-05-07 12:17:25 +02:00
*
* @return void
*/
2024-08-13 13:44:16 +00:00
public function __construct(ReaderInterface $reader, WriterInterface $writer)
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
$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();
2024-05-07 12:17:25 +02:00
}
/**
* Get an environment variable.
*
2024-08-13 13:44:16 +00:00
* @param string $name
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @throws \InvalidArgumentException
2024-05-07 12:17:25 +02:00
*
* @return string|null
*/
2024-08-13 13:44:16 +00:00
public function get(string $name)
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
return $this->reader->read($name)->getOrElse(null);
2024-05-07 12:17:25 +02:00
}
/**
* Set an environment variable.
*
2024-08-13 13:44:16 +00:00
* @param string $name
* @param string $value
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @throws \InvalidArgumentException
*
* @return bool
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function set(string $name, string $value)
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
return $this->writer->write($name, $value);
2024-05-07 12:17:25 +02:00
}
/**
* Clear an environment variable.
*
2024-08-13 13:44:16 +00:00
* @param string $name
2024-05-07 12:17:25 +02:00
*
2024-08-13 13:44:16 +00:00
* @throws \InvalidArgumentException
*
* @return bool
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function clear(string $name)
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
if ('' === $name) {
throw new InvalidArgumentException('Expected name to be a non-empty string.');
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
return $this->writer->delete($name);
2024-05-07 12:17:25 +02:00
}
}