Skip to content

Commit

Permalink
Merge pull request #1 from abacaphiliac/load-dotenv
Browse files Browse the repository at this point in the history
load dotenv from constant, env variable, or working directory... in that order.
  • Loading branch information
abacaphiliac committed Feb 26, 2016
2 parents c7625f4 + e7173d7 commit c9fe988
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
STUFF=things
ASDF=qwer
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ before_install:

install:
- composer install --no-interaction

script:
- ./vendor/bin/phing
41 changes: 41 additions & 0 deletions build.xml
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>
17 changes: 17 additions & 0 deletions phpunit.xml.dist
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>
53 changes: 53 additions & 0 deletions src/ZendPhpDotEnv/DotEnvFactory.php
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);
}
}
76 changes: 76 additions & 0 deletions src/ZendPhpDotEnv/DotEnvLoader.php
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);
}
}
}
8 changes: 8 additions & 0 deletions src/ZendPhpDotEnv/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Exception;

interface ExceptionInterface
{

}
8 changes: 8 additions & 0 deletions src/ZendPhpDotEnv/Exception/InvalidConstantPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Exception;

class InvalidConstantPathException extends InvalidPathException
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Exception;

class InvalidEnvironmentVariablePathException extends InvalidPathException
{

}
8 changes: 8 additions & 0 deletions src/ZendPhpDotEnv/Exception/InvalidPathException.php
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
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Abacaphiliac\ZendPhpDotEnv\Exception;

class InvalidWorkingDirectoryPathException extends InvalidPathException
{

}
71 changes: 71 additions & 0 deletions src/ZendPhpDotEnv/Module.php
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);
}
}
50 changes: 50 additions & 0 deletions tests/ZendPhpDotEnv/DotEnvFactoryTest.php
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);
}
}
Loading

0 comments on commit c9fe988

Please sign in to comment.