Upgrade framework
This commit is contained in:
280
vendor/vlucas/phpdotenv/src/Dotenv.php
vendored
280
vendor/vlucas/phpdotenv/src/Dotenv.php
vendored
@@ -1,105 +1,267 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dotenv;
|
||||
|
||||
/**
|
||||
* This is the dotenv class.
|
||||
*
|
||||
* It's responsible for loading a `.env` file in the given directory and
|
||||
* setting the environment vars.
|
||||
*/
|
||||
use Dotenv\Exception\InvalidPathException;
|
||||
use Dotenv\Loader\Loader;
|
||||
use Dotenv\Loader\LoaderInterface;
|
||||
use Dotenv\Parser\Parser;
|
||||
use Dotenv\Parser\ParserInterface;
|
||||
use Dotenv\Repository\Adapter\ArrayAdapter;
|
||||
use Dotenv\Repository\Adapter\PutenvAdapter;
|
||||
use Dotenv\Repository\RepositoryBuilder;
|
||||
use Dotenv\Repository\RepositoryInterface;
|
||||
use Dotenv\Store\StoreBuilder;
|
||||
use Dotenv\Store\StoreInterface;
|
||||
use Dotenv\Store\StringStore;
|
||||
|
||||
class Dotenv
|
||||
{
|
||||
/**
|
||||
* The file path.
|
||||
* The store instance.
|
||||
*
|
||||
* @var string
|
||||
* @var \Dotenv\Store\StoreInterface
|
||||
*/
|
||||
protected $filePath;
|
||||
private $store;
|
||||
|
||||
/**
|
||||
* The parser instance.
|
||||
*
|
||||
* @var \Dotenv\Parser\ParserInterface
|
||||
*/
|
||||
private $parser;
|
||||
|
||||
/**
|
||||
* The loader instance.
|
||||
*
|
||||
* @var \Dotenv\Loader|null
|
||||
* @var \Dotenv\Loader\LoaderInterface
|
||||
*/
|
||||
protected $loader;
|
||||
private $loader;
|
||||
|
||||
/**
|
||||
* The repository instance.
|
||||
*
|
||||
* @var \Dotenv\Repository\RepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Create a new dotenv instance.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $file
|
||||
* @param \Dotenv\Store\StoreInterface $store
|
||||
* @param \Dotenv\Parser\ParserInterface $parser
|
||||
* @param \Dotenv\Loader\LoaderInterface $loader
|
||||
* @param \Dotenv\Repository\RepositoryInterface $repository
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($path, $file = '.env')
|
||||
{
|
||||
$this->filePath = $this->getFilePath($path, $file);
|
||||
$this->loader = new Loader($this->filePath, true);
|
||||
public function __construct(
|
||||
StoreInterface $store,
|
||||
ParserInterface $parser,
|
||||
LoaderInterface $loader,
|
||||
RepositoryInterface $repository
|
||||
) {
|
||||
$this->store = $store;
|
||||
$this->parser = $parser;
|
||||
$this->loader = $loader;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load environment file in given directory.
|
||||
* Create a new dotenv instance.
|
||||
*
|
||||
* @return array
|
||||
* @param \Dotenv\Repository\RepositoryInterface $repository
|
||||
* @param string|string[] $paths
|
||||
* @param string|string[]|null $names
|
||||
* @param bool $shortCircuit
|
||||
* @param string|null $fileEncoding
|
||||
*
|
||||
* @return \Dotenv\Dotenv
|
||||
*/
|
||||
public static function create(RepositoryInterface $repository, $paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
|
||||
{
|
||||
$builder = $names === null ? StoreBuilder::createWithDefaultName() : StoreBuilder::createWithNoNames();
|
||||
|
||||
foreach ((array) $paths as $path) {
|
||||
$builder = $builder->addPath($path);
|
||||
}
|
||||
|
||||
foreach ((array) $names as $name) {
|
||||
$builder = $builder->addName($name);
|
||||
}
|
||||
|
||||
if ($shortCircuit) {
|
||||
$builder = $builder->shortCircuit();
|
||||
}
|
||||
|
||||
return new self($builder->fileEncoding($fileEncoding)->make(), new Parser(), new Loader(), $repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mutable dotenv instance with default repository.
|
||||
*
|
||||
* @param string|string[] $paths
|
||||
* @param string|string[]|null $names
|
||||
* @param bool $shortCircuit
|
||||
* @param string|null $fileEncoding
|
||||
*
|
||||
* @return \Dotenv\Dotenv
|
||||
*/
|
||||
public static function createMutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
|
||||
{
|
||||
$repository = RepositoryBuilder::createWithDefaultAdapters()->make();
|
||||
|
||||
return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mutable dotenv instance with default repository with the putenv adapter.
|
||||
*
|
||||
* @param string|string[] $paths
|
||||
* @param string|string[]|null $names
|
||||
* @param bool $shortCircuit
|
||||
* @param string|null $fileEncoding
|
||||
*
|
||||
* @return \Dotenv\Dotenv
|
||||
*/
|
||||
public static function createUnsafeMutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
|
||||
{
|
||||
$repository = RepositoryBuilder::createWithDefaultAdapters()
|
||||
->addAdapter(PutenvAdapter::class)
|
||||
->make();
|
||||
|
||||
return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new immutable dotenv instance with default repository.
|
||||
*
|
||||
* @param string|string[] $paths
|
||||
* @param string|string[]|null $names
|
||||
* @param bool $shortCircuit
|
||||
* @param string|null $fileEncoding
|
||||
*
|
||||
* @return \Dotenv\Dotenv
|
||||
*/
|
||||
public static function createImmutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
|
||||
{
|
||||
$repository = RepositoryBuilder::createWithDefaultAdapters()->immutable()->make();
|
||||
|
||||
return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new immutable dotenv instance with default repository with the putenv adapter.
|
||||
*
|
||||
* @param string|string[] $paths
|
||||
* @param string|string[]|null $names
|
||||
* @param bool $shortCircuit
|
||||
* @param string|null $fileEncoding
|
||||
*
|
||||
* @return \Dotenv\Dotenv
|
||||
*/
|
||||
public static function createUnsafeImmutable($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
|
||||
{
|
||||
$repository = RepositoryBuilder::createWithDefaultAdapters()
|
||||
->addAdapter(PutenvAdapter::class)
|
||||
->immutable()
|
||||
->make();
|
||||
|
||||
return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new dotenv instance with an array backed repository.
|
||||
*
|
||||
* @param string|string[] $paths
|
||||
* @param string|string[]|null $names
|
||||
* @param bool $shortCircuit
|
||||
* @param string|null $fileEncoding
|
||||
*
|
||||
* @return \Dotenv\Dotenv
|
||||
*/
|
||||
public static function createArrayBacked($paths, $names = null, bool $shortCircuit = true, string $fileEncoding = null)
|
||||
{
|
||||
$repository = RepositoryBuilder::createWithNoAdapters()->addAdapter(ArrayAdapter::class)->make();
|
||||
|
||||
return self::create($repository, $paths, $names, $shortCircuit, $fileEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given content and resolve nested variables.
|
||||
*
|
||||
* This method behaves just like load(), only without mutating your actual
|
||||
* environment. We do this by using an array backed repository.
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @throws \Dotenv\Exception\InvalidFileException
|
||||
*
|
||||
* @return array<string,string|null>
|
||||
*/
|
||||
public static function parse(string $content)
|
||||
{
|
||||
$repository = RepositoryBuilder::createWithNoAdapters()->addAdapter(ArrayAdapter::class)->make();
|
||||
|
||||
$phpdotenv = new self(new StringStore($content), new Parser(), new Loader(), $repository);
|
||||
|
||||
return $phpdotenv->load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and load environment file(s).
|
||||
*
|
||||
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidEncodingException|\Dotenv\Exception\InvalidFileException
|
||||
*
|
||||
* @return array<string,string|null>
|
||||
*/
|
||||
public function load()
|
||||
{
|
||||
return $this->loadData();
|
||||
$entries = $this->parser->parse($this->store->read());
|
||||
|
||||
return $this->loader->load($this->repository, $entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load environment file in given directory.
|
||||
* Read and load environment file(s), silently failing if no files can be read.
|
||||
*
|
||||
* @return array
|
||||
* @throws \Dotenv\Exception\InvalidEncodingException|\Dotenv\Exception\InvalidFileException
|
||||
*
|
||||
* @return array<string,string|null>
|
||||
*/
|
||||
public function overload()
|
||||
public function safeLoad()
|
||||
{
|
||||
return $this->loadData(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full path to the file.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getFilePath($path, $file)
|
||||
{
|
||||
if (!is_string($file)) {
|
||||
$file = '.env';
|
||||
try {
|
||||
return $this->load();
|
||||
} catch (InvalidPathException $e) {
|
||||
// suppressing exception
|
||||
return [];
|
||||
}
|
||||
|
||||
$filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
|
||||
|
||||
return $filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually load the data.
|
||||
*
|
||||
* @param bool $overload
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function loadData($overload = false)
|
||||
{
|
||||
$this->loader = new Loader($this->filePath, !$overload);
|
||||
|
||||
return $this->loader->load();
|
||||
}
|
||||
|
||||
/**
|
||||
* Required ensures that the specified variables exist, and returns a new validator object.
|
||||
*
|
||||
* @param string|string[] $variable
|
||||
* @param string|string[] $variables
|
||||
*
|
||||
* @return \Dotenv\Validator
|
||||
*/
|
||||
public function required($variable)
|
||||
public function required($variables)
|
||||
{
|
||||
return new Validator((array) $variable, $this->loader);
|
||||
return (new Validator($this->repository, (array) $variables))->required();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new validator object that won't check if the specified variables exist.
|
||||
*
|
||||
* @param string|string[] $variables
|
||||
*
|
||||
* @return \Dotenv\Validator
|
||||
*/
|
||||
public function ifPresent($variables)
|
||||
{
|
||||
return new Validator($this->repository, (array) $variables);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user