-
Notifications
You must be signed in to change notification settings - Fork 3
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 #1 from abacaphiliac/load-dotenv
load dotenv from constant, env variable, or working directory... in that order.
- Loading branch information
Showing
15 changed files
with
486 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
STUFF=things | ||
ASDF=qwer |
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 |
---|---|---|
|
@@ -17,3 +17,6 @@ before_install: | |
|
||
install: | ||
- composer install --no-interaction | ||
|
||
script: | ||
- ./vendor/bin/phing |
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 @@ | ||
<project name="abacaphiliac/zend-phpdotenv" default="develop" basedir="."> | ||
|
||
<target name="develop"> | ||
<phingcall target="lint"/> | ||
<phingcall target="tests"/> | ||
</target> | ||
|
||
<target name="lint"> | ||
<phingcall target="php-lint"/> | ||
<phingcall target="phpcs"/> | ||
</target> | ||
|
||
<target name="php-lint"> | ||
<exec command="vendor/bin/parallel-lint src tests" | ||
passthru="true" | ||
output="/dev/stdout" | ||
error="/dev/stdout" | ||
checkreturn="true"/> | ||
</target> | ||
|
||
<target name="phpcs"> | ||
<exec command="vendor/bin/phpcs --standard=PSR2 --extensions=php --severity=1 --colors -p src/ tests/" | ||
passthru="true" | ||
output="/dev/stdout" | ||
error="/dev/stdout" | ||
checkreturn="true"/> | ||
</target> | ||
|
||
<target name="tests"> | ||
<phingcall target="unit-tests"/> | ||
</target> | ||
|
||
<target name="unit-tests"> | ||
<exec command="./vendor/bin/phpunit --coverage-text" | ||
passthru="true" | ||
output="/dev/stdout" | ||
error="/dev/stdout" | ||
checkreturn="true"/> | ||
</target> | ||
|
||
</project> |
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,17 @@ | ||
<phpunit bootstrap="./vendor/autoload.php" | ||
beStrictAboutTestsThatDoNotTestAnything="true" | ||
beStrictAboutOutputDuringTests="true" | ||
disallowChangesToGlobalState="true" | ||
colors="never" | ||
verbose="true"> | ||
<testsuites> | ||
<testsuite name="abacaphiliac/zend-phpdotenv"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,53 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv; | ||
|
||
use Dotenv\Dotenv; | ||
|
||
class DotEnvFactory | ||
{ | ||
/** | ||
* @param string $name | ||
* @param string $file | ||
* @return Dotenv | ||
*/ | ||
public static function createFromConstant($name, $file) | ||
{ | ||
$path = constant($name); | ||
|
||
return self::create($path, $file); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param string $file | ||
* @return Dotenv | ||
*/ | ||
public static function createFromEnvironmentVariable($name, $file) | ||
{ | ||
$path = DotEnvLoader::getEnvironmentVariable($name); | ||
|
||
return self::create($path, $file); | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* @return Dotenv | ||
*/ | ||
public static function createFromWorkingDirectory($file) | ||
{ | ||
$path = getcwd(); | ||
|
||
return self::create($path, $file); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param string $file | ||
* @return Dotenv | ||
*/ | ||
public static function create($path, $file) | ||
{ | ||
return new Dotenv($path, $file); | ||
} | ||
} |
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,76 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv; | ||
|
||
use Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException; | ||
use Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException; | ||
use Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException; | ||
use Dotenv\Exception\InvalidPathException; | ||
use Dotenv\Loader; | ||
|
||
class DotEnvLoader | ||
{ | ||
/** | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public static function getEnvironmentVariable($name) | ||
{ | ||
$loader = new Loader(__DIR__); | ||
|
||
return $loader->getEnvironmentVariable($name); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public static function hasEnvironmentVariable($name) | ||
{ | ||
return (bool) self::getEnvironmentVariable($name); | ||
} | ||
|
||
/** | ||
* @param string $constant | ||
* @param string $file | ||
* @return mixed[] | ||
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException | ||
*/ | ||
public static function loadFromConstant($constant, $file) | ||
{ | ||
try { | ||
return DotEnvFactory::createFromConstant($constant, $file)->load(); | ||
} catch (InvalidPathException $e) { | ||
throw new InvalidConstantPathException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $variable | ||
* @param string $file | ||
* @return mixed[] | ||
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException | ||
*/ | ||
public static function loadFromEnvironmentVariable($variable, $file) | ||
{ | ||
try { | ||
return DotEnvFactory::createFromEnvironmentVariable($variable, $file)->load(); | ||
} catch (InvalidPathException $e) { | ||
throw new InvalidEnvironmentVariablePathException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* @return mixed[] | ||
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException | ||
*/ | ||
public static function loadFromWorkingDirectory($file) | ||
{ | ||
try { | ||
return DotEnvFactory::createFromWorkingDirectory($file)->load(); | ||
} catch (InvalidPathException $e) { | ||
throw new InvalidWorkingDirectoryPathException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv\Exception; | ||
|
||
interface ExceptionInterface | ||
{ | ||
|
||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv\Exception; | ||
|
||
class InvalidConstantPathException extends InvalidPathException | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/ZendPhpDotEnv/Exception/InvalidEnvironmentVariablePathException.php
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,8 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv\Exception; | ||
|
||
class InvalidEnvironmentVariablePathException extends InvalidPathException | ||
{ | ||
|
||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv\Exception; | ||
|
||
class InvalidPathException extends \Dotenv\Exception\InvalidPathException implements ExceptionInterface | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/ZendPhpDotEnv/Exception/InvalidWorkingDirectoryPathException.php
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,8 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv\Exception; | ||
|
||
class InvalidWorkingDirectoryPathException extends InvalidPathException | ||
{ | ||
|
||
} |
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,71 @@ | ||
<?php | ||
|
||
namespace Abacaphiliac\ZendPhpDotEnv; | ||
|
||
use Zend\ModuleManager\ModuleEvent; | ||
use Zend\ModuleManager\ModuleManager; | ||
|
||
class Module | ||
{ | ||
/** @var string */ | ||
private $constant = 'APPLICATION_PATH'; | ||
|
||
/** @var string */ | ||
private $variable = 'APPLICATION_PATH'; | ||
|
||
/** @var string */ | ||
private $file = '.env'; | ||
|
||
/** | ||
* Module constructor. | ||
* @param string $constant | ||
* @param string $variable | ||
* @param string $file | ||
*/ | ||
public function __construct($constant = null, $variable = null, $file = null) | ||
{ | ||
if ($constant) { | ||
$this->constant = $constant; | ||
} | ||
|
||
if ($variable) { | ||
$this->variable = $variable; | ||
} | ||
|
||
if ($file) { | ||
$this->file = $file; | ||
} | ||
} | ||
|
||
/** | ||
* @param ModuleManager $moduleManager | ||
*/ | ||
public function init(ModuleManager $moduleManager) | ||
{ | ||
$events = $moduleManager->getEventManager(); | ||
|
||
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'loadEnvironmentVariables'), 2000); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidConstantPathException | ||
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException | ||
* @throws \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException | ||
*/ | ||
public function loadEnvironmentVariables() | ||
{ | ||
// Load from application-path defined by constant. | ||
if ($this->constant && defined($this->constant)) { | ||
return DotEnvLoader::loadFromConstant($this->constant, $this->file); | ||
} | ||
|
||
// Load from application-path defined by environment-variable. | ||
if ($this->variable && DotEnvLoader::hasEnvironmentVariable($this->variable)) { | ||
return DotEnvLoader::loadFromEnvironmentVariable($this->variable, $this->file); | ||
} | ||
|
||
// Load from working directory. ZF2 applications change the working directory to the application root. | ||
return DotEnvLoader::loadFromWorkingDirectory($this->file); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace AbacaphiliacTest\ZendPhpDotEnv; | ||
|
||
use Abacaphiliac\ZendPhpDotEnv\DotEnvFactory; | ||
|
||
class DotEnvFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testCreate() | ||
{ | ||
$actual = DotEnvFactory::create(__DIR__, '.testEnv'); | ||
|
||
self::assertInstanceOf('\Dotenv\Dotenv', $actual); | ||
} | ||
|
||
public function testCreateFromConstant() | ||
{ | ||
$constant = 'AbacaphiliacTest_ZendPhpDotEnv_DotEnvFactoryTest_testCreateFromConstant'; | ||
|
||
self::assertFalse(defined($constant)); | ||
|
||
define($constant, __DIR__); | ||
|
||
$actual = DotEnvFactory::createFromConstant($constant, '.testEnv'); | ||
|
||
self::assertInstanceOf('\Dotenv\Dotenv', $actual); | ||
} | ||
|
||
public function testCreateFromEnvironmentVariable() | ||
{ | ||
self::assertFalse(array_key_exists(__METHOD__, $_ENV)); | ||
|
||
$_ENV[__METHOD__] = __DIR__; | ||
|
||
$actual = DotEnvFactory::createFromEnvironmentVariable(__METHOD__, '.testEnv'); | ||
|
||
unset($_ENV[__METHOD__]); | ||
|
||
self::assertInstanceOf('\Dotenv\Dotenv', $actual); | ||
} | ||
|
||
public function testCreateFromWorkingDirectory() | ||
{ | ||
$actual = DotEnvFactory::createFromWorkingDirectory('.testEnv'); | ||
|
||
unset($_ENV[__METHOD__]); | ||
|
||
self::assertInstanceOf('\Dotenv\Dotenv', $actual); | ||
} | ||
} |
Oops, something went wrong.