From 41a3864f304794a455c895c868c8265c1294ffbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 28 Mar 2017 10:07:34 +0200 Subject: [PATCH 01/12] Issue #1 Fix loading of .env file --- CHANGELOG.md | 5 +++++ src/Config.php | 10 +++++----- tests/ConsoleConfigTest.php | 2 +- tests/EnvTest.php | 16 ++++++++-------- tests/WebConfigTest.php | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) 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/src/Config.php b/src/Config.php index f8b32a4..385ceaa 100644 --- a/src/Config.php +++ b/src/Config.php @@ -235,14 +235,14 @@ public static function initEnv($directory = null) } // Define main Yii environment variables - if (isset($_ENV['YII_DEBUG'])) { - define('YII_DEBUG', (bool)$_ENV['YII_DEBUG']); + if (getenv('YII_DEBUG') !== false) { + define('YII_DEBUG', (bool)getenv('YII_DEBUG')); if (YII_DEBUG) { error_reporting(E_ALL); } } - if (isset($_ENV['YII_ENV'])) { - define('YII_ENV', $_ENV['YII_ENV']); + if (getenv('YII_ENV') !== false) { + define('YII_ENV', getenv('YII_ENV')); } } @@ -261,7 +261,7 @@ public static function env($name, $default = null, $required = false) if ($required) { \Dotenv::required($name); } - return isset($_ENV[$name]) ? $_ENV[$name] : $default; + return getenv($name) === false ? $default : getenv($name); } } diff --git a/tests/ConsoleConfigTest.php b/tests/ConsoleConfigTest.php index a037896..5cac0c7 100644 --- a/tests/ConsoleConfigTest.php +++ b/tests/ConsoleConfigTest.php @@ -47,7 +47,7 @@ public function testCanMergeCustomConfig() public function testCanMergeLocalConfigByEnvVar() { $config = new Config(__DIR__ . '/app'); - $_ENV['ENABLE_LOCALCONF'] = 1; + putenv('ENABLE_LOCALCONF=1'); $console = $config->console([ 'key5' => 'test', ]); diff --git a/tests/EnvTest.php b/tests/EnvTest.php index 77bb2f3..e595073 100644 --- a/tests/EnvTest.php +++ b/tests/EnvTest.php @@ -5,8 +5,8 @@ class EnvTest extends \PHPUnit_Framework_TestCase { public function testCanInitYiiEnvFromEnvVars() { - $_ENV['YII_DEBUG'] = true; - $_ENV['YII_ENV'] = 'prod'; + putenv('YII_DEBUG=1'); + putenv('YII_ENV=prod'); Config::initEnv(); $this->assertTrue(defined('YII_DEBUG')); @@ -27,8 +27,8 @@ 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')); @@ -48,8 +48,8 @@ public function testCanGetEnvVarsFromEnvFile() public function testEnvFileDoesNotClearEnvVars() { - $_ENV['TEST1'] = 654; - $_ENV['TEST2'] = 'xyz'; + putenv('TEST1=654'); + putenv('TEST2=xyz'); Config::initEnv(__DIR__ . '/app'); $this->assertEquals(654, Config::env('TEST1')); @@ -61,8 +61,8 @@ public function testEnvFileDoesNotClearEnvVars() public function testEnvFileDoesNotOverrideEnvVars() { - $_ENV['VAR1'] = 654; - $_ENV['VAR2'] = 'xyz'; + putenv('VAR1=654'); + putenv('VAR2=xyz'); Config::initEnv(__DIR__ . '/app'); $this->assertEquals('', Config::env('YII_DEBUG')); diff --git a/tests/WebConfigTest.php b/tests/WebConfigTest.php index c8ebc30..c1b3c8e 100644 --- a/tests/WebConfigTest.php +++ b/tests/WebConfigTest.php @@ -43,7 +43,7 @@ public function testCanMergeCustomConfig() public function testCanMergeLocalConfigByEnvVar() { $config = new Config(__DIR__ . '/app'); - $_ENV['ENABLE_LOCALCONF'] = 1; + putenv('ENABLE_LOCALCONF=1'); $web = $config->web([ 'key5' => 'test', ]); From a40409ce0ac72c3fb781dc7fa51518b2d3a814eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 28 Mar 2017 11:08:45 +0200 Subject: [PATCH 02/12] Fix phpunit config for V6 --- phpunit.xml.dist | 2 +- tests/bootstrap.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5887fb6..79b8204 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,7 @@ processIsolation="true" stopOnFailure="false" syntaxCheck="false" - bootstrap="vendor/autoload.php" + bootstrap="tests/bootstrap.php" > 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); +} + From bb2f8270505fa937a822260b26fb41f0973f1514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 28 Mar 2017 11:26:26 +0200 Subject: [PATCH 03/12] Revert "Fix phpunit config for V6" This reverts commit a40409ce0ac72c3fb781dc7fa51518b2d3a814eb. --- phpunit.xml.dist | 2 +- tests/bootstrap.php | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 79b8204..5887fb6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,7 @@ processIsolation="true" stopOnFailure="false" syntaxCheck="false" - bootstrap="tests/bootstrap.php" + bootstrap="vendor/autoload.php" > diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8666e52..b3d9bbc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,10 +1 @@ 6 -$newClass = '\PHPUnit\Framework\TestCase'; -$oldClass = '\PHPUnit_Framework_TestCase'; -if (!class_exists($newClass) && class_exists($oldClass)) { - class_alias($oldClass, $newClass); -} - From e517590ebf3c9f83d8ddcf27aea78c7450415736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 28 Mar 2017 11:39:10 +0200 Subject: [PATCH 04/12] Migrate to namespaced tests (Requires PHPUnit > 5.7) --- tests/BootstrapTest.php | 2 +- tests/ConsoleConfigTest.php | 2 +- tests/EnvTest.php | 2 +- tests/WebConfigTest.php | 2 +- tests/bootstrap.php | 1 - 5 files changed, 4 insertions(+), 5 deletions(-) delete mode 100644 tests/bootstrap.php diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php index 2e3179d..8b4ebec 100644 --- a/tests/BootstrapTest.php +++ b/tests/BootstrapTest.php @@ -1,7 +1,7 @@ Date: Tue, 28 Mar 2017 11:45:33 +0200 Subject: [PATCH 05/12] Fix test setup for PHPUnit < 5.7 (non-namespaced test classes) --- phpunit.xml.dist | 2 +- tests/bootstrap.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/bootstrap.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5887fb6..79b8204 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,7 @@ processIsolation="true" stopOnFailure="false" syntaxCheck="false" - bootstrap="vendor/autoload.php" + bootstrap="tests/bootstrap.php" > diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..8666e52 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,10 @@ + 6 +$newClass = '\PHPUnit\Framework\TestCase'; +$oldClass = '\PHPUnit_Framework_TestCase'; +if (!class_exists($newClass) && class_exists($oldClass)) { + class_alias($oldClass, $newClass); +} + From 283ea755d44d1d7076e41519a76e2de5839c311e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 17 Apr 2018 15:06:44 +0200 Subject: [PATCH 06/12] Fix issue with child processes where $_ENV is not set --- src/Config.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Config.php b/src/Config.php index 385ceaa..45ee7e8 100644 --- a/src/Config.php +++ b/src/Config.php @@ -235,14 +235,16 @@ public static function initEnv($directory = null) } // Define main Yii environment variables - if (getenv('YII_DEBUG') !== false) { - define('YII_DEBUG', (bool)getenv('YII_DEBUG')); + $debug = self::env('YII_DEBUG', false); + if ($debug !== false) { + define('YII_DEBUG', (bool)$debug); if (YII_DEBUG) { error_reporting(E_ALL); } } - if (getenv('YII_ENV') !== false) { - define('YII_ENV', getenv('YII_ENV')); + $env = self::env('YII_ENV', false); + if ($env !== false) { + define('YII_ENV', $env); } } @@ -261,7 +263,14 @@ public static function env($name, $default = null, $required = false) if ($required) { \Dotenv::required($name); } - return getenv($name) === false ? $default : getenv($name); + if (array_key_exists($name, $_ENV)) { + return $_ENV[$name]; + } + if (array_key_exists($name, $_SERVER)) { + return $_SERVER[$name]; + } + $value = getenv($name); + return $value === false ? $default : $value; } } From d4b3bf892f2749c214a075cba89a76ffd0ea5902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 17 Apr 2018 15:07:35 +0200 Subject: [PATCH 07/12] Remove HHVM check flag --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 131a3fb..b9c7cc2 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. From 551b1514e650937e935d32c86c2e2d939f8f9f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 17 Apr 2018 16:20:07 +0200 Subject: [PATCH 08/12] Remove HHVM from travis tests --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d5c2e2d..f3b5e35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,6 @@ php: - "5.6" - "5.5" - "5.4" - - "hhvm" install: - sudo apt-get update From 555a41d5d02c7c02e3ad97584bfc0d44c2f4e433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Wed, 21 Nov 2018 11:27:02 +0100 Subject: [PATCH 09/12] Fix local config in README --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b9c7cc2..b396760 100644 --- a/README.md +++ b/README.md @@ -98,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: From 0fe56708742e0775dc1d5175ae2b93910c2ce381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Thu, 29 Nov 2018 09:04:14 +0100 Subject: [PATCH 10/12] Issue #4 Upgrade to Dotenv 2.5.x --- composer.json | 2 +- src/Config.php | 10 +++++----- tests/EnvTest.php | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 747377f..f2afa34 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "yiisoft/yii2": "*", - "vlucas/phpdotenv": "1.0.*" + "vlucas/phpdotenv": "2.5.*" }, "autoload": { "psr-4": { diff --git a/src/Config.php b/src/Config.php index 45ee7e8..c2b8ec4 100644 --- a/src/Config.php +++ b/src/Config.php @@ -231,7 +231,8 @@ 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 = new \Dotenv\Dotenv($directory); + $dotenv->load(); } // Define main Yii environment variables @@ -260,9 +261,6 @@ 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]; } @@ -270,7 +268,9 @@ public static function env($name, $default = null, $required = false) 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/EnvTest.php b/tests/EnvTest.php index cd1e94b..84ee6ed 100644 --- a/tests/EnvTest.php +++ b/tests/EnvTest.php @@ -35,11 +35,21 @@ public function testCanGetEnvVars() $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')); @@ -65,7 +75,7 @@ public function testEnvFileDoesNotOverrideEnvVars() putenv('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('xyz', Config::env('VAR2')); From 31142fe0a91717631933144060088311d8da22cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20H=C3=A4rtl?= Date: Tue, 7 Jan 2020 09:15:06 +0100 Subject: [PATCH 11/12] Test against newer PHP versions and use require/autoload-dev --- .travis.yml | 4 ++++ composer.json | 44 ++++++++++++++++++++++--------------- tests/BootstrapTest.php | 5 ++++- tests/ConsoleConfigTest.php | 5 ++++- tests/EnvTest.php | 5 ++++- tests/WebConfigTest.php | 5 ++++- 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index f3b5e35..4f44c9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,9 @@ language: php +dist: trusty php: + - "7.4" + - "7.3" + - "7.2" - "7.1" - "7.0" - "5.6" diff --git a/composer.json b/composer.json index f2afa34..a8dc1ab 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,29 @@ { - "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": "2.5.*" - }, - "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": { + "yiisoft/yii2": "*", + "vlucas/phpdotenv": "2.5.*" + }, + "require-dev": { + "phpunit/phpunit": ">4.0 <8" + }, + "autoload": { + "psr-4": { + "codemix\\yii2confload\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "tests\\": "tests" + } + } } diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php index 8b4ebec..42c21e6 100644 --- a/tests/BootstrapTest.php +++ b/tests/BootstrapTest.php @@ -1,7 +1,10 @@ Date: Wed, 5 Aug 2020 12:48:07 +0200 Subject: [PATCH 12/12] Upgrade to DotEnv 5.x --- .travis.yml | 4 ---- composer.json | 3 ++- phpunit.xml.dist | 1 - src/Config.php | 2 +- tests/EnvTest.php | 12 ++++++------ 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4f44c9a..34f3c2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,10 +5,6 @@ php: - "7.3" - "7.2" - "7.1" - - "7.0" - - "5.6" - - "5.5" - - "5.4" install: - sudo apt-get update diff --git a/composer.json b/composer.json index a8dc1ab..2dd69e9 100644 --- a/composer.json +++ b/composer.json @@ -10,8 +10,9 @@ } ], "require": { + "php": "^7.1.3 || ^8.0", "yiisoft/yii2": "*", - "vlucas/phpdotenv": "2.5.*" + "vlucas/phpdotenv": "^5.0" }, "require-dev": { "phpunit/phpunit": ">4.0 <8" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 79b8204..a814c26 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,6 @@ convertWarningsToExceptions="true" processIsolation="true" stopOnFailure="false" - syntaxCheck="false" bootstrap="tests/bootstrap.php" > diff --git a/src/Config.php b/src/Config.php index c2b8ec4..99ea28b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -231,7 +231,7 @@ 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 = new \Dotenv\Dotenv($directory); + $dotenv = \Dotenv\Dotenv::createImmutable($directory); $dotenv->load(); } diff --git a/tests/EnvTest.php b/tests/EnvTest.php index e864485..60d28d4 100644 --- a/tests/EnvTest.php +++ b/tests/EnvTest.php @@ -61,11 +61,11 @@ public function testCanGetEnvVarsFromEnvFile() public function testEnvFileDoesNotClearEnvVars() { - putenv('TEST1=654'); - putenv('TEST2=xyz'); + $_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')); @@ -74,13 +74,13 @@ public function testEnvFileDoesNotClearEnvVars() public function testEnvFileDoesNotOverrideEnvVars() { - putenv('VAR1=654'); - putenv('VAR2=xyz'); + $_ENV['VAR1'] = '654'; + $_ENV['VAR2'] = 'xyz'; Config::initEnv(__DIR__ . '/app'); $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')); }