Skip to content

Make it easier to swap out the loader, to allow for a custom loader #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Dotenv
/**
* The loader instance.
*
* @var \Dotenv\Loader|null
* @var \Dotenv\LoaderInterface|null
*/
protected $loader;

Expand All @@ -35,7 +35,7 @@ class Dotenv
public function __construct($path, $file = '.env')
{
$this->filePath = $this->getFilePath($path, $file);
$this->loader = new Loader($this->filePath, true);
$this->loader = $this->getLoader();
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ protected function getFilePath($path, $file)
*/
protected function loadData($overload = false)
{
$this->loader = new Loader($this->filePath, !$overload);
$this->loader = $this->getLoader(! $overload);

return $this->loader->load();
}
Expand All @@ -102,4 +102,14 @@ public function required($variable)
{
return new Validator((array) $variable, $this->loader);
}

/**
* @param bool $immutable
*
* @return LoaderInterface
*/
protected function getLoader($immutable = true)
{
return new Loader($this->filePath, $immutable);
}
}
2 changes: 1 addition & 1 deletion src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* - stripping comments beginning with a `#`,
* - parsing lines that look shell variable setters, e.g `export key = value`, `key="value"`.
*/
class Loader
class Loader implements LoaderInterface
{
/**
* The file path.
Expand Down
72 changes: 72 additions & 0 deletions src/LoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Dotenv;

/**
* This is the loaded interface.
*/
interface LoaderInterface
{
/**
* Load config data into array
*
* @return array
*/
public function load();

/**
* Process the runtime filters.
*
* Called from the `VariableFactory`, passed as a callback in `$this->loadFromFile()`.
*
* @param string $name
* @param string $value
*
* @return array
*/
public function processFilters($name, $value);

/**
* Search the different places for environment variables and return first value found.
*
* @param string $name
*
* @return string|null
*/
public function getEnvironmentVariable($name);

/**
* Set an environment variable.
*
* This is done using:
* - putenv,
* - $_ENV,
* - $_SERVER.
*
* The environment variable value is stripped of single and double quotes.
*
* @param string $name
* @param string|null $value
*
* @return void
*/
public function setEnvironmentVariable($name, $value = null);

/**
* Clear an environment variable.
*
* This is not (currently) used by Dotenv but is provided as a utility
* method for 3rd party code.
*
* This is done using:
* - putenv,
* - unset($_ENV, $_SERVER).
*
* @param string $name
*
* @see setEnvironmentVariable()
*
* @return void
*/
public function clearEnvironmentVariable($name);
}
8 changes: 4 additions & 4 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ class Validator
/**
* The loader instance.
*
* @var \Dotenv\Loader
* @var \Dotenv\LoaderInterface
*/
protected $loader;

/**
* Create a new validator instance.
*
* @param array $variables
* @param \Dotenv\Loader $loader
* @param array $variables
* @param \Dotenv\LoaderInterface $loader
*
* @return void
*/
public function __construct(array $variables, Loader $loader)
public function __construct(array $variables, LoaderInterface $loader)
{
$this->variables = $variables;
$this->loader = $loader;
Expand Down