-
-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from GrahamCampbell/env-obj
Refactored to use environment variables object
- Loading branch information
Showing
19 changed files
with
889 additions
and
196 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
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
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
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,86 @@ | ||
<?php | ||
|
||
namespace Dotenv\Environment; | ||
|
||
use ArrayAccess; | ||
|
||
/** | ||
* This is the abstract variables implementation. | ||
* | ||
* Extend this as required, implementing "get", "set", and "clear". | ||
*/ | ||
abstract class AbstractVariables implements VariablesInterface | ||
{ | ||
/** | ||
* Are we immutable? | ||
* | ||
* @var bool | ||
*/ | ||
private $immutable; | ||
|
||
/** | ||
* Create a new environment variables instance. | ||
* | ||
* @param bool $immutable | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($immutable) | ||
{ | ||
$this->immutable = $immutable; | ||
} | ||
|
||
/** | ||
* Determine if the environment is immutable. | ||
* | ||
* @return bool | ||
*/ | ||
public function isImmutable() | ||
{ | ||
return $this->immutable; | ||
} | ||
|
||
/** | ||
* Tells whether environment variable has been defined. | ||
* | ||
* @param string $name | ||
* | ||
* @return bool | ||
*/ | ||
public function has($name) | ||
{ | ||
return !is_null($this->get($name)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
return $this->has($offset); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return $this->get($offset); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
$this->set($offset, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
$this->clear($offset); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Dotenv\Environment\Adapter; | ||
|
||
interface AdapterInterface | ||
{ | ||
/** | ||
* Determines if the adapter is supported. | ||
* | ||
* @return bool | ||
*/ | ||
public function isSupported(); | ||
|
||
/** | ||
* Get an environment variable, if it exists. | ||
* | ||
* @param string $name | ||
* | ||
* @return \PhpOption\Option | ||
*/ | ||
public function get($name); | ||
|
||
/** | ||
* Set an environment variable. | ||
* | ||
* @param string $name | ||
* @param string|null $value | ||
* | ||
* @return void | ||
*/ | ||
public function set($name, $value = null); | ||
|
||
/** | ||
* Clear an environment variable. | ||
* | ||
* @param string $name | ||
* | ||
* @return void | ||
*/ | ||
public function clear($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,64 @@ | ||
<?php | ||
|
||
namespace Dotenv\Environment\Adapter; | ||
|
||
use PhpOption\None; | ||
|
||
class ApacheAdapter implements AdapterInterface | ||
{ | ||
/** | ||
* Determines if the adapter is supported. | ||
* | ||
* This happens if PHP is running as an Apache module. | ||
* | ||
* @return bool | ||
*/ | ||
public function isSupported() | ||
{ | ||
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. | ||
* | ||
* @param string $name | ||
* | ||
* @return \PhpOption\Option | ||
*/ | ||
public function get($name) | ||
{ | ||
return None::create(); | ||
} | ||
|
||
/** | ||
* Set an environment variable. | ||
* | ||
* Only if an existing apache variable exists do we overwrite it. | ||
* | ||
* @param string $name | ||
* @param string|null $value | ||
* | ||
* @return void | ||
*/ | ||
public function set($name, $value = null) | ||
{ | ||
if (apache_getenv($name) !== false) { | ||
apache_setenv($name, $value); | ||
} | ||
} | ||
|
||
/** | ||
* Clear an environment variable. | ||
* | ||
* @param string $name | ||
* | ||
* @return void | ||
*/ | ||
public function clear($name) | ||
{ | ||
// Nothing to do here. | ||
} | ||
} |
Oops, something went wrong.