diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..2365e66 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +STUFF=things +ASDF=qwer diff --git a/.travis.yml b/.travis.yml index 7e723b2..9d0abb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,3 +17,6 @@ before_install: install: - composer install --no-interaction + +script: + - ./vendor/bin/phing diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..e4ce229 --- /dev/null +++ b/build.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..cbf1b23 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,17 @@ + + + + ./tests + + + + + src + + + diff --git a/src/ZendPhpDotEnv/DotEnvFactory.php b/src/ZendPhpDotEnv/DotEnvFactory.php new file mode 100644 index 0000000..610c616 --- /dev/null +++ b/src/ZendPhpDotEnv/DotEnvFactory.php @@ -0,0 +1,53 @@ +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); + } + } +} diff --git a/src/ZendPhpDotEnv/Exception/ExceptionInterface.php b/src/ZendPhpDotEnv/Exception/ExceptionInterface.php new file mode 100644 index 0000000..ce659e4 --- /dev/null +++ b/src/ZendPhpDotEnv/Exception/ExceptionInterface.php @@ -0,0 +1,8 @@ +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); + } +} diff --git a/tests/ZendPhpDotEnv/DotEnvFactoryTest.php b/tests/ZendPhpDotEnv/DotEnvFactoryTest.php new file mode 100644 index 0000000..8f17eb1 --- /dev/null +++ b/tests/ZendPhpDotEnv/DotEnvFactoryTest.php @@ -0,0 +1,50 @@ +loadEnvironmentVariables(); + } + + /** + * @expectedException \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidEnvironmentVariablePathException + */ + public function testModuleLoadsEnvironmentVariablesFromEnvironmentVariable() + { + self::assertFalse(DotEnvLoader::hasEnvironmentVariable(__FUNCTION__)); + $_ENV[__FUNCTION__] = __DIR__; + + $module = new Module($constant = null, $variable = __FUNCTION__, $file = '.testEnv'); + $module->loadEnvironmentVariables(); + } + + /** + * @expectedException \Abacaphiliac\ZendPhpDotEnv\Exception\InvalidWorkingDirectoryPathException + */ + public function testModuleLoadsEnvironmentVariablesFromWorkingDirectory() + { + $module = new Module($constant = null, $variable = null, $file = '.testEnv'); + $module->loadEnvironmentVariables(); + } + + /** + * @expectedException \Dotenv\Exception\InvalidPathException + */ + public function testLoadModule() + { + $serviceLocator = new ServiceManager(); + $serviceLocator->setService('ApplicationConfig', array( + 'module_listener_options' => array(), + 'modules' => array( + 'Abacaphiliac\ZendPhpDotEnv', + ), + )); + $serviceLocator->setFactory('EventManager', new EventManagerFactory()); + $serviceLocator->setService('SharedEventManager', new SharedEventManager()); + + $moduleManagerFactory = new ModuleManagerFactory(); + $moduleManager = $moduleManagerFactory->createService($serviceLocator); + + $moduleManager->loadModules(); + } +}