diff --git a/.travis.yml b/.travis.yml index d5c2e2d..34f3c2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,10 @@ language: php +dist: trusty php: + - "7.4" + - "7.3" + - "7.2" - "7.1" - - "7.0" - - "5.6" - - "5.5" - - "5.4" - - "hhvm" install: - sudo apt-get update diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ef2b0b..6237a67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 0.9.1 + + * Fix Issue #1: `.env` file not always processed + +Initial release. ## 0.9.0 Initial release. diff --git a/README.md b/README.md index 131a3fb..b396760 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ Yii2 Configloader [![Latest Stable Version](https://poser.pugx.org/codemix/yii2-configloader/v/stable.svg)](https://packagist.org/packages/codemix/yii2-configloader) [![Latest Unstable Version](https://poser.pugx.org/codemix/yii2-configloader/v/unstable.svg)](https://packagist.org/packages/codemix/yii2-configloader) [![Total Downloads](https://poser.pugx.org/codemix/yii2-configloader/downloads)](https://packagist.org/packages/codemix/yii2-configloader) -[![HHVM Status](http://hhvm.h4cc.de/badge/yiisoft/yii2-dev.png)](http://hhvm.h4cc.de/package/codemix/yii2-configloader) [![License](https://poser.pugx.org/codemix/yii2-configloader/license.svg)](https://packagist.org/packages/codemix/yii2-configloader) Build configuration arrays from config files and environment variables. @@ -99,9 +98,10 @@ variable (either in your server environment or in `.env`): ENABLE_LOCALCONF=1 ``` -Now any call to `web()` or `console()` will return the merged result of `config/web.php` + -`config/console.php` or `config/console.php` + `config/local-console.php` respectively. +Now the methods will return the corresponding merged results: + * `web()`: `config/web.php` + `config/local.php` + * `console()`: `config/console.php` + `config/local-console.php` Alternatively you can explicitely ask for local configuration: diff --git a/composer.json b/composer.json index 747377f..2dd69e9 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,30 @@ { - "name": "codemix/yii2-configloader", - "description": "Build configuration arrays from config files and env vars.", - "keywords": ["yii2", "config", "environment"], - "license": "MIT", - "authors": [ - { - "name": "Michael Härtl", - "email": "haertl.mike@gmail.com" - } - ], - "require": { - "yiisoft/yii2": "*", - "vlucas/phpdotenv": "1.0.*" - }, - "autoload": { - "psr-4": { - "codemix\\yii2confload\\": "src/" - } + "name": "codemix/yii2-configloader", + "description": "Build configuration arrays from config files and env vars.", + "keywords": ["yii2", "config", "environment"], + "license": "MIT", + "authors": [ + { + "name": "Michael Härtl", + "email": "haertl.mike@gmail.com" } + ], + "require": { + "php": "^7.1.3 || ^8.0", + "yiisoft/yii2": "*", + "vlucas/phpdotenv": "^5.0" + }, + "require-dev": { + "phpunit/phpunit": ">4.0 <8" + }, + "autoload": { + "psr-4": { + "codemix\\yii2confload\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "tests\\": "tests" + } + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5887fb6..a814c26 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,8 +8,7 @@ convertWarningsToExceptions="true" processIsolation="true" stopOnFailure="false" - syntaxCheck="false" - bootstrap="vendor/autoload.php" + bootstrap="tests/bootstrap.php" > diff --git a/src/Config.php b/src/Config.php index f8b32a4..99ea28b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -231,18 +231,21 @@ public static function bootstrap($directory, $vendor = null, $initEnv = true) public static function initEnv($directory = null) { if ($directory !== null && file_exists($directory . DIRECTORY_SEPARATOR . '.env')) { - \Dotenv::load($directory); + $dotenv = \Dotenv\Dotenv::createImmutable($directory); + $dotenv->load(); } // Define main Yii environment variables - if (isset($_ENV['YII_DEBUG'])) { - define('YII_DEBUG', (bool)$_ENV['YII_DEBUG']); + $debug = self::env('YII_DEBUG', false); + if ($debug !== false) { + define('YII_DEBUG', (bool)$debug); if (YII_DEBUG) { error_reporting(E_ALL); } } - if (isset($_ENV['YII_ENV'])) { - define('YII_ENV', $_ENV['YII_ENV']); + $env = self::env('YII_ENV', false); + if ($env !== false) { + define('YII_ENV', $env); } } @@ -258,10 +261,16 @@ public static function initEnv($directory = null) */ public static function env($name, $default = null, $required = false) { - if ($required) { - \Dotenv::required($name); + if (array_key_exists($name, $_ENV)) { + return $_ENV[$name]; } - return isset($_ENV[$name]) ? $_ENV[$name] : $default; + if (array_key_exists($name, $_SERVER)) { + return $_SERVER[$name]; + } + $value = getenv($name); + if ($value === false && $required) { + throw new \Exception("Environment variable '$name' is not set"); + } + return $value === false ? $default : $value; } - } diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php index 2e3179d..42c21e6 100644 --- a/tests/BootstrapTest.php +++ b/tests/BootstrapTest.php @@ -1,7 +1,10 @@ console([ 'key5' => 'test', ]); diff --git a/tests/EnvTest.php b/tests/EnvTest.php index 77bb2f3..60d28d4 100644 --- a/tests/EnvTest.php +++ b/tests/EnvTest.php @@ -1,12 +1,15 @@ assertTrue(defined('YII_DEBUG')); @@ -27,19 +30,29 @@ public function testCanInitYiiEnvFromEnvFile() public function testCanGetEnvVars() { - $_ENV['TEST1'] = 987; - $_ENV['TEST2'] = 'demo'; + putenv('TEST1=987'); + putenv('TEST2=demo'); $this->assertEquals(987, Config::env('TEST1')); $this->assertEquals('demo', Config::env('TEST2')); $this->assertEquals('default', Config::env('TEST3', 'default')); } + public function testCanRequireEnvVar() + { + try { + $x = Config::env('DOESNOTEXIST', null, true); + $this->fail('No exception thrown for missing env variable'); + } catch (\Exception $e) { + $this->assertEquals("Environment variable 'DOESNOTEXIST' is not set", $e->getMessage()); + } + } + public function testCanGetEnvVarsFromEnvFile() { Config::initEnv(__DIR__ . '/app'); - $this->assertEquals('', Config::env('YII_DEBUG')); + $this->assertEquals(0, Config::env('YII_DEBUG')); $this->assertEquals('dev', Config::env('YII_ENV')); $this->assertEquals('dotenv1', Config::env('VAR1')); $this->assertEquals(2, Config::env('VAR2')); @@ -48,11 +61,11 @@ public function testCanGetEnvVarsFromEnvFile() public function testEnvFileDoesNotClearEnvVars() { - $_ENV['TEST1'] = 654; + $_ENV['TEST1'] = '654'; $_ENV['TEST2'] = 'xyz'; Config::initEnv(__DIR__ . '/app'); - $this->assertEquals(654, Config::env('TEST1')); + $this->assertEquals('654', Config::env('TEST1')); $this->assertEquals('xyz', Config::env('TEST2')); $this->assertEquals('dotenv1', Config::env('VAR1')); $this->assertEquals(2, Config::env('VAR2')); @@ -61,13 +74,13 @@ public function testEnvFileDoesNotClearEnvVars() public function testEnvFileDoesNotOverrideEnvVars() { - $_ENV['VAR1'] = 654; + $_ENV['VAR1'] = '654'; $_ENV['VAR2'] = 'xyz'; Config::initEnv(__DIR__ . '/app'); - $this->assertEquals('', Config::env('YII_DEBUG')); + $this->assertEquals(0, Config::env('YII_DEBUG')); $this->assertEquals('dev', Config::env('YII_ENV')); - $this->assertEquals(654, Config::env('VAR1')); + $this->assertEquals('654', Config::env('VAR1')); $this->assertEquals('xyz', Config::env('VAR2')); } diff --git a/tests/WebConfigTest.php b/tests/WebConfigTest.php index c8ebc30..67b9a49 100644 --- a/tests/WebConfigTest.php +++ b/tests/WebConfigTest.php @@ -1,7 +1,10 @@ web([ 'key5' => 'test', ]); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b3d9bbc..8666e52 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1 +1,10 @@ 6 +$newClass = '\PHPUnit\Framework\TestCase'; +$oldClass = '\PHPUnit_Framework_TestCase'; +if (!class_exists($newClass) && class_exists($oldClass)) { + class_alias($oldClass, $newClass); +} +