├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src └── Config.php └── tests ├── BootstrapTest.php ├── ConsoleConfigTest.php ├── EnvTest.php ├── WebConfigTest.php ├── app ├── .env ├── config │ ├── console.php │ ├── local-console.php │ ├── local.php │ └── web.php └── otherdir │ ├── console.php │ ├── local-console.php │ ├── local.php │ └── web.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | composer.lock 3 | /vendor 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | dist: trusty 3 | php: 4 | - "7.4" 5 | - "7.3" 6 | - "7.2" 7 | - "7.1" 8 | 9 | install: 10 | - sudo apt-get update 11 | - travis_retry composer self-update && composer --version 12 | # - travis_retry composer global require --no-progress "fxp/composer-asset-plugin:^1.2.2" 13 | # Let's speed things up: 14 | - travis_retry composer config repo.composer composer 'https://asset-packagist.org' 15 | - export PATH="$HOME/.composer/vendor/bin:$PATH" 16 | - travis_retry composer install --prefer-dist --no-interaction --no-progress 17 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## 0.9.1 4 | 5 | * Fix Issue #1: `.env` file not always processed 6 | 7 | Initial release. 8 | ## 0.9.0 9 | 10 | Initial release. 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Michael Härtl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 Configloader 2 | ================= 3 | 4 | [![Build Status](https://secure.travis-ci.org/codemix/yii2-configloader.png)](http://travis-ci.org/codemix/yii2-configloader) 5 | [![Latest Stable Version](https://poser.pugx.org/codemix/yii2-configloader/v/stable.svg)](https://packagist.org/packages/codemix/yii2-configloader) 6 | [![Latest Unstable Version](https://poser.pugx.org/codemix/yii2-configloader/v/unstable.svg)](https://packagist.org/packages/codemix/yii2-configloader) 7 | [![Total Downloads](https://poser.pugx.org/codemix/yii2-configloader/downloads)](https://packagist.org/packages/codemix/yii2-configloader) 8 | [![License](https://poser.pugx.org/codemix/yii2-configloader/license.svg)](https://packagist.org/packages/codemix/yii2-configloader) 9 | 10 | Build configuration arrays from config files and environment variables. 11 | 12 | ## Features 13 | 14 | You can use this extension to solve some or all of the following tasks: 15 | 16 | * Build Yii2 configuration arrays for web and console application 17 | * Initialize Yii environment (`YII_DEBUG`, `YII_ENV`) from environment variables 18 | * Load environment variables from a `.env` file 19 | * Get config options from environment variables 20 | * Load local configuration overrides 21 | * Streamline config initialization and Yii 2 bootstrapping 22 | 23 | ## Installation 24 | 25 | Install the package with [composer](http://getcomposer.org): 26 | 27 | composer require codemix/yii2-configloader 28 | 29 | 30 | ## Description 31 | 32 | We mainly use this extension to configure our dockerized yii2 applications. 33 | It's [good practice](https://12factor.net/) to build your docker applications in such a way, 34 | that the runtime configuration in productive mode happens solely via environment variables. 35 | 36 | But during local development we can loosen these strict requirement a little as we 37 | sometimes have to add debug options or the like that should not be part of the main 38 | configuration. Here the extension helps to override settings with local configuration 39 | files that live outside of version control. 40 | 41 | You have several options how to use this extension: 42 | 43 | 1. Use only the Yii environment initialization 44 | 2. Use only the configuration loader 45 | 3. Use both 46 | 47 | We first show how to use the first two options "standalone" and then a third, 48 | combined way that includes all features. 49 | 50 | 51 | ### 1. Initializing Yii environment 52 | 53 | This will set the `YII_DEBUG` and `YII_ENV` variables according to the respective 54 | environment variables if those are set. It can also load them from a `.env` file. 55 | 56 | In debug mode `error_reporting()` will also be set to `E_ALL`. 57 | 58 | ```php 59 | web(); 89 | ``` 90 | 91 | #### 2.1 Local configuration 92 | 93 | By default local configuration files `local.php` and `local-console.php` are not 94 | loaded. To activate this feature you can either set the `ENABLE_LOCALCONF` environment 95 | variable (either in your server environment or in `.env`): 96 | 97 | ``` 98 | ENABLE_LOCALCONF=1 99 | ``` 100 | 101 | Now the methods will return the corresponding merged results: 102 | 103 | * `web()`: `config/web.php` + `config/local.php` 104 | * `console()`: `config/console.php` + `config/local-console.php` 105 | 106 | Alternatively you can explicitely ask for local configuration: 107 | 108 | ```php 109 | web([], true); 114 | // Merges configuration from config/console.php and config/local-console.php if present 115 | $consoleConfig = $config->console([], true); 116 | ``` 117 | 118 | #### 2.2 Merging custom configuration 119 | 120 | You can also inject some other configuration when you fetch the web or console config: 121 | 122 | ```php 123 | web(['id' => 'test'], true); 127 | ``` 128 | 129 | 130 | ### 3. Initialize Yii environment and load configuration 131 | 132 | Let's finally show a full example that demonstrates how to use all the mentioned 133 | features in one go. A typical setup will use the following files: 134 | 135 | #### `.env` 136 | 137 | Here we define the Yii environment and DB credentials. You'd add more config options 138 | in the same manner: 139 | 140 | ``` 141 | YII_DEBUG=1 142 | YII_ENV=dev 143 | 144 | DB_DSN=mysql:host=db.example.com;dbname=web 145 | DB_USER=user 146 | DB_PASSWORD='**secret**' 147 | ``` 148 | 149 | #### `config/web.php` 150 | 151 | This file is later included in the scope of `codemix\yii2confload\Config`, so you 152 | can easily access instance and class methods: 153 | 154 | ```php 155 | [ 159 | 'db' => [ 160 | 'dsn' => self::env('DB_DSN', 'mysql:host=db;dbname=web'), 161 | 'username' => self::env('DB_USER', 'web'), 162 | 'password' => self::env('DB_PASSWORD', 'web'), 163 | ], 164 | ``` 165 | 166 | #### `config/console.php` 167 | 168 | Having access to the config instance allows for example to reuse parts of your web 169 | configuration in your console config. 170 | 171 | ```php 172 | web(); 176 | return [ 177 | // ... 178 | 'components' => [ 179 | 'db' => $web['components']['db'], 180 | ``` 181 | 182 | #### `web/index.php` 183 | 184 | We've streamlined the process of setting up a `Config` object and loading the 185 | Yii 2 bootstrap file into a single method `Config::boostrap()` which only 186 | receives the application directory as argument. 187 | 188 | ```php 189 | web()])->run(); 195 | ``` 196 | 197 | This makes sure that things are loaded in the right order. If you prefer a more 198 | verbose version, the code above is equivalent to: 199 | 200 | ```php 201 | web()])->run(); 209 | ``` 210 | 211 | #### `yii` 212 | 213 | The same approach is used for the console application: 214 | 215 | ```php 216 | console()]); 222 | exit($application->run()); 223 | ``` 224 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codemix/yii2-configloader", 3 | "description": "Build configuration arrays from config files and env vars.", 4 | "keywords": ["yii2", "config", "environment"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Michael Härtl", 9 | "email": "haertl.mike@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.1.3 || ^8.0", 14 | "yiisoft/yii2": "*", 15 | "vlucas/phpdotenv": "^5.0" 16 | }, 17 | "require-dev": { 18 | "phpunit/phpunit": ">4.0 <8" 19 | }, 20 | "autoload": { 21 | "psr-4": { 22 | "codemix\\yii2confload\\": "src/" 23 | } 24 | }, 25 | "autoload-dev": { 26 | "psr-4": { 27 | "tests\\": "tests" 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Config.php: -------------------------------------------------------------------------------- 1 | web(); 13 | * $config = new Config('/path/to/app')->console(); 14 | * 15 | * // Merge a `config/local.php` or `config/console-local.php` with local overrides 16 | * // This also happens if `ENABLE_LOCALCONF` environment variable is set. 17 | * $config = new Config('/path/to/app')->web([], true); 18 | * $config = new Config('/path/to/app')->console([], true); 19 | * ``` 20 | * 21 | * The class normally is used with this set of files: 22 | * 23 | * - `.env`: Defines environment variables (usually only used for local development) 24 | * - `config/web.php`: Web configuration 25 | * - `config/console.php`: Console configuration 26 | * 27 | * A typical example for an `.env` file: 28 | * 29 | * ``` 30 | * YII_DEBUG=1 31 | * DB_DSN=mysql:host=db;dbname=web 32 | * DB_USER=user 33 | * DB_PASSWORD=secret 34 | * ``` 35 | * 36 | * The `web.php` is loaded in the context of this class, so you can easily access 37 | * these settings there like: 38 | * 39 | * ```php 40 | * return [ 41 | * 'components' => [ 42 | * 'db' => [ 43 | * 'dsn' => self::env('DB_DSN'), 44 | * 'username' => self::env('DB_USER'), 45 | * 'password' => self::env('DB_PASSWORD'), 46 | * ``` 47 | * 48 | * In your `console.php` you can also reuse parts of your web configuration: 49 | * 50 | * ``php 51 | * $web = $this->web(); 52 | * return [ 53 | * 'components' => [ 54 | * 'db' => $web['components']['db'], 55 | * ``` 56 | * 57 | * The initialization of the Yii environment and loading of `.env` file 58 | * is optional and can also be supressed: 59 | * 60 | * ```php 61 | * $config = new Config('/path/to/app', false)->web(); 62 | * ``` 63 | * 64 | * Vice versa the class can also be used to only initialize the Yii environment 65 | * and load a `.env` file: 66 | * 67 | * ```php 68 | * Config::initEnv('/path/to/app'); 69 | * 70 | * // Get setting from environment variable or .env file 71 | * $setting = Config::env('MY_SETTING', 'default'); 72 | * ``` 73 | */ 74 | class Config 75 | { 76 | /** 77 | * @var string the application base directory 78 | */ 79 | public $directory; 80 | 81 | /** 82 | * Initialize the app directory path and the Yii environment. 83 | * 84 | * If an `.env` file is found in the app directory, it's loaded with `Dotenv`. 85 | * If a `YII_DEBUG` or `YII_ENV` environment variable is set, the Yii constants 86 | * are set accordingly. In debug mode the error reporting is also set to `E_ALL`. 87 | * 88 | * @param string $directory the application base directory 89 | * @param bool $initEnv whether to initialize the Yii environment. Default is `true`. 90 | */ 91 | public function __construct($directory, $initEnv = true) 92 | { 93 | $this->directory = $directory; 94 | 95 | if ($initEnv) { 96 | self::initEnv($directory); 97 | } 98 | 99 | } 100 | 101 | /** 102 | * Gets the filename for a config file 103 | * 104 | * @param string $name 105 | * @param bool $required whether the file must exist. Default is `true`. 106 | * @param string $directory the name of the config directory. Default is `config`. 107 | * @return string|null the full path to the config file or `null` if $required 108 | * is set to `false` and the file does not exist 109 | * @throws \Exception 110 | */ 111 | public function getConfigFile($name, $required = true, $directory = 'config') 112 | { 113 | $sep = DIRECTORY_SEPARATOR; 114 | $path = rtrim($this->directory, $sep) . $sep . trim($directory, $sep) . $sep . $name; 115 | if (!file_exists($path)) { 116 | if ($required) { 117 | throw new \Exception("Config file '$path' does not exist"); 118 | } else { 119 | return null; 120 | } 121 | } 122 | return $path; 123 | } 124 | 125 | /** 126 | * Builds the web configuration. 127 | * 128 | * If $local is set to `true` and a `local.php` config file exists, it 129 | * is merged into the web configuration. 130 | * 131 | * Alternatively an environment variable `ENABLE_LOCALCONF` can be set 132 | * to 1. Setting $local to `false` completely disables the local config. 133 | * 134 | * @param array $config additional configuration to merge into the result 135 | * @param bool|null $local whether to check for local configuration overrides. 136 | * The default is `null`, which will check `ENABLE_LOCALCONF` env var. 137 | * @return array the web configuration array 138 | * @throws \Exception 139 | */ 140 | public function web($config = [], $local = null) 141 | { 142 | $files = [$this->getConfigFile('web.php')]; 143 | if ($local === null) { 144 | $local = $this->env('ENABLE_LOCALCONF', false); 145 | } 146 | if ($local) { 147 | $localFile = $this->getConfigFile('local.php', false); 148 | if ($localFile !==null) { 149 | $files[] = $localFile; 150 | } 151 | } 152 | return $this->mergeFiles($files, $config); 153 | } 154 | 155 | /** 156 | * Builds the console configuration. 157 | * 158 | * If $local is set to `true` and a `local-console.php` config file exists, 159 | * it is merged into the console configuration. 160 | * 161 | * Alternatively an environment variable `ENABLE_LOCALCONF` can be set 162 | * to 1. Setting $local to `false` completely disables the local config. 163 | * 164 | * @param array $config additional configuration to merge into the result 165 | * @param bool|null $local whether to check for local configuration overrides. 166 | * The default is `null`, which will check `ENABLE_LOCALCONF` env var. 167 | * @return array the web configuration array 168 | * @throws \Exception 169 | */ 170 | public function console($config = [], $local = null) 171 | { 172 | $files = [$this->getConfigFile('console.php')]; 173 | if ($local === null) { 174 | $local = $this->env('ENABLE_LOCALCONF', false); 175 | } 176 | if ($local) { 177 | $localFile = $this->getConfigFile('local-console.php', false); 178 | if ($localFile !==null) { 179 | $files[] = $localFile; 180 | } 181 | } 182 | return $this->mergeFiles($files, $config); 183 | } 184 | 185 | /** 186 | * Load configuration files and merge them together. 187 | * 188 | * The files are loaded in the context of this class. So you can use `$this` 189 | * and `self` to access instance / class methods. 190 | * 191 | * @param array $files list of configuration files to load and merge. 192 | * Configuration from later files will override earlier values. 193 | * @param array $config additional configuration to merge into the result 194 | * @return array the resulting configuration array 195 | */ 196 | public function mergeFiles($files, $config = []) 197 | { 198 | $configs = array_map(function ($f) { return require($f); }, $files); 199 | $configs[] = $config; 200 | return call_user_func_array('yii\helpers\ArrayHelper::merge', $configs); 201 | } 202 | 203 | /** 204 | * Init the configuration for the given directory and load the Yii bootstrap file. 205 | * 206 | * @param string $directory the application directory 207 | * @param string|null $vendor the composer vendor directory. Default is `null` 208 | * which means, the vendor directory is autodetected. 209 | * @param bool $initEnv whether to initialize the Yii environment. Default is `true`. 210 | * @return static the Config instance for that application directory 211 | */ 212 | public static function bootstrap($directory, $vendor = null, $initEnv = true) 213 | { 214 | $sep = DIRECTORY_SEPARATOR; 215 | if ($vendor === null) { 216 | $vendor = realpath(__DIR__ . $sep . '..' . $sep . '..' . $sep . '..'); 217 | } 218 | $config = new self($directory, $initEnv); 219 | require(rtrim($vendor, $sep) . $sep . 'yiisoft' . $sep . 'yii2' . $sep . 'Yii.php'); 220 | return $config; 221 | } 222 | 223 | /** 224 | * Init the Yii environment from environment variables. 225 | * 226 | * If $directory is passed and contains a `.env` file, that file is loaded 227 | * with `Dotenv` first. 228 | * 229 | * @param string|null $directory the directory to check for an `.env` file 230 | */ 231 | public static function initEnv($directory = null) 232 | { 233 | if ($directory !== null && file_exists($directory . DIRECTORY_SEPARATOR . '.env')) { 234 | $dotenv = \Dotenv\Dotenv::createImmutable($directory); 235 | $dotenv->load(); 236 | } 237 | 238 | // Define main Yii environment variables 239 | $debug = self::env('YII_DEBUG', false); 240 | if ($debug !== false) { 241 | define('YII_DEBUG', (bool)$debug); 242 | if (YII_DEBUG) { 243 | error_reporting(E_ALL); 244 | } 245 | } 246 | $env = self::env('YII_ENV', false); 247 | if ($env !== false) { 248 | define('YII_ENV', $env); 249 | } 250 | } 251 | 252 | /** 253 | * Get either an env var or a default value if the var is not set. 254 | * 255 | * @param string $name the name of the variable to get 256 | * @param mixed $default the default value to return if variable is not set. 257 | * Default is `null`. 258 | * @param bool $required whether the var must be set. $default is ignored in 259 | * this case. Default is `false`. 260 | * @return mixed the content of the environment variable or $default if not set 261 | */ 262 | public static function env($name, $default = null, $required = false) 263 | { 264 | if (array_key_exists($name, $_ENV)) { 265 | return $_ENV[$name]; 266 | } 267 | if (array_key_exists($name, $_SERVER)) { 268 | return $_SERVER[$name]; 269 | } 270 | $value = getenv($name); 271 | if ($value === false && $required) { 272 | throw new \Exception("Environment variable '$name' is not set"); 273 | } 274 | return $value === false ? $default : $value; 275 | } 276 | } 277 | -------------------------------------------------------------------------------- /tests/BootstrapTest.php: -------------------------------------------------------------------------------- 1 | web(); 13 | $expected = [ 14 | 'key1' => 'dotenv1', 15 | 'key2' => [ 16 | 'web1', 17 | 'web2', 18 | ], 19 | 'key3' => 2, 20 | 'key4' => 'web4', 21 | ]; 22 | 23 | $this->assertEquals($expected, $web); 24 | $this->assertTrue(defined('YII_DEBUG')); 25 | $this->assertTrue(defined('YII_ENV')); 26 | $this->assertTrue(defined('YII_ENV_PROD')); 27 | $this->assertEquals(0, YII_DEBUG); 28 | $this->assertEquals('dev', YII_ENV); 29 | $this->assertTrue(class_exists('Yii', false)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/ConsoleConfigTest.php: -------------------------------------------------------------------------------- 1 | console(); 13 | $expected = [ 14 | 'key1' => 'dotenv1', 15 | 'key2' => [ 16 | 'web1', 17 | 'web2', 18 | ], 19 | 'key3' => 'console3', 20 | 'key4' => [ 21 | 'console3', 22 | 'console4', 23 | ], 24 | ]; 25 | $this->assertEquals($expected, $console); 26 | } 27 | 28 | public function testCanMergeCustomConfig() 29 | { 30 | $config = new Config(__DIR__ . '/app'); 31 | $console = $config->console([ 32 | 'key5' => 'test', 33 | ]); 34 | $expected = [ 35 | 'key1' => 'dotenv1', 36 | 'key2' => [ 37 | 'web1', 38 | 'web2', 39 | ], 40 | 'key3' => 'console3', 41 | 'key4' => [ 42 | 'console3', 43 | 'console4', 44 | ], 45 | 'key5' => 'test', 46 | ]; 47 | $this->assertEquals($expected, $console); 48 | } 49 | 50 | public function testCanMergeLocalConfigByEnvVar() 51 | { 52 | $config = new Config(__DIR__ . '/app'); 53 | putenv('ENABLE_LOCALCONF=1'); 54 | $console = $config->console([ 55 | 'key5' => 'test', 56 | ]); 57 | $expected = [ 58 | 'key1' => 'dotenv1', 59 | 'key2' => [ 60 | 'web1', 61 | 'web2', 62 | 'local1', 63 | 'local2', 64 | 'lconsole1', 65 | 'lconsole2', 66 | ], 67 | 'key3' => 'lconsole3', 68 | 'key4' => [ 69 | 'console3', 70 | 'console4', 71 | ], 72 | 'key5' => 'test', 73 | ]; 74 | $this->assertEquals($expected, $console); 75 | } 76 | 77 | public function testCanMergeLocalConfigByArgument() 78 | { 79 | $config = new Config(__DIR__ . '/app'); 80 | $console = $config->console([ 81 | 'key5' => 'test', 82 | ], true); 83 | $expected = [ 84 | 'key1' => 'dotenv1', 85 | 'key2' => [ 86 | 'web1', 87 | 'web2', 88 | 'lconsole1', 89 | 'lconsole2', 90 | ], 91 | 'key3' => 'lconsole3', 92 | 'key4' => [ 93 | 'console3', 94 | 'console4', 95 | ], 96 | 'key5' => 'test', 97 | ]; 98 | $this->assertEquals($expected, $console); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/EnvTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(defined('YII_DEBUG')); 16 | $this->assertTrue(defined('YII_ENV')); 17 | $this->assertTrue(YII_DEBUG); 18 | $this->assertEquals('prod', YII_ENV); 19 | } 20 | 21 | public function testCanInitYiiEnvFromEnvFile() 22 | { 23 | Config::initEnv(__DIR__ . '/app'); 24 | 25 | $this->assertTrue(defined('YII_DEBUG')); 26 | $this->assertTrue(defined('YII_ENV')); 27 | $this->assertEquals(0, YII_DEBUG); 28 | $this->assertEquals('dev', YII_ENV); 29 | } 30 | 31 | public function testCanGetEnvVars() 32 | { 33 | putenv('TEST1=987'); 34 | putenv('TEST2=demo'); 35 | 36 | $this->assertEquals(987, Config::env('TEST1')); 37 | $this->assertEquals('demo', Config::env('TEST2')); 38 | $this->assertEquals('default', Config::env('TEST3', 'default')); 39 | } 40 | 41 | public function testCanRequireEnvVar() 42 | { 43 | try { 44 | $x = Config::env('DOESNOTEXIST', null, true); 45 | $this->fail('No exception thrown for missing env variable'); 46 | } catch (\Exception $e) { 47 | $this->assertEquals("Environment variable 'DOESNOTEXIST' is not set", $e->getMessage()); 48 | } 49 | } 50 | 51 | public function testCanGetEnvVarsFromEnvFile() 52 | { 53 | Config::initEnv(__DIR__ . '/app'); 54 | 55 | $this->assertEquals(0, Config::env('YII_DEBUG')); 56 | $this->assertEquals('dev', Config::env('YII_ENV')); 57 | $this->assertEquals('dotenv1', Config::env('VAR1')); 58 | $this->assertEquals(2, Config::env('VAR2')); 59 | $this->assertEquals('default', Config::env('TEST3', 'default')); 60 | } 61 | 62 | public function testEnvFileDoesNotClearEnvVars() 63 | { 64 | $_ENV['TEST1'] = '654'; 65 | $_ENV['TEST2'] = 'xyz'; 66 | Config::initEnv(__DIR__ . '/app'); 67 | 68 | $this->assertEquals('654', Config::env('TEST1')); 69 | $this->assertEquals('xyz', Config::env('TEST2')); 70 | $this->assertEquals('dotenv1', Config::env('VAR1')); 71 | $this->assertEquals(2, Config::env('VAR2')); 72 | $this->assertEquals('default', Config::env('TEST3', 'default')); 73 | } 74 | 75 | public function testEnvFileDoesNotOverrideEnvVars() 76 | { 77 | $_ENV['VAR1'] = '654'; 78 | $_ENV['VAR2'] = 'xyz'; 79 | Config::initEnv(__DIR__ . '/app'); 80 | 81 | $this->assertEquals(0, Config::env('YII_DEBUG')); 82 | $this->assertEquals('dev', Config::env('YII_ENV')); 83 | $this->assertEquals('654', Config::env('VAR1')); 84 | $this->assertEquals('xyz', Config::env('VAR2')); 85 | } 86 | 87 | public function testInitsYiiEnvByDefault() 88 | { 89 | $config = new Config(__DIR__ . '/app'); 90 | 91 | $this->assertTrue(defined('YII_DEBUG')); 92 | $this->assertTrue(defined('YII_ENV')); 93 | $this->assertEquals(0, YII_DEBUG); 94 | $this->assertEquals('dev', YII_ENV); 95 | } 96 | 97 | public function testCanSuppressYiiEnvInit() 98 | { 99 | $config = new Config(__DIR__ . '/app', false); 100 | 101 | $this->assertFalse(defined('YII_DEBUG')); 102 | $this->assertFalse(defined('YII_ENV')); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tests/WebConfigTest.php: -------------------------------------------------------------------------------- 1 | web(); 13 | $expected = [ 14 | 'key1' => 'dotenv1', 15 | 'key2' => [ 16 | 'web1', 17 | 'web2', 18 | ], 19 | 'key3' => 2, 20 | 'key4' => 'web4', 21 | ]; 22 | 23 | $this->assertEquals($expected, $web); 24 | } 25 | 26 | public function testCanMergeCustomConfig() 27 | { 28 | $config = new Config(__DIR__ . '/app'); 29 | $web = $config->web([ 30 | 'key5' => 'test', 31 | ]); 32 | $expected = [ 33 | 'key1' => 'dotenv1', 34 | 'key2' => [ 35 | 'web1', 36 | 'web2', 37 | ], 38 | 'key3' => 2, 39 | 'key4' => 'web4', 40 | 'key5' => 'test', 41 | ]; 42 | 43 | $this->assertEquals($expected, $web); 44 | } 45 | 46 | public function testCanMergeLocalConfigByEnvVar() 47 | { 48 | $config = new Config(__DIR__ . '/app'); 49 | putenv('ENABLE_LOCALCONF=1'); 50 | $web = $config->web([ 51 | 'key5' => 'test', 52 | ]); 53 | $expected = [ 54 | 'key1' => 'dotenv1', 55 | 'key2' => [ 56 | 'web1', 57 | 'web2', 58 | 'local1', 59 | 'local2', 60 | ], 61 | 'key3' => 'local3', 62 | 'key4' => 'web4', 63 | 'key5' => 'test', 64 | ]; 65 | $this->assertEquals($expected, $web); 66 | } 67 | 68 | public function testCanMergeLocalConfigByArgument() 69 | { 70 | $config = new Config(__DIR__ . '/app'); 71 | $web = $config->web([ 72 | 'key5' => 'test', 73 | ], true); 74 | $expected = [ 75 | 'key1' => 'dotenv1', 76 | 'key2' => [ 77 | 'web1', 78 | 'web2', 79 | 'local1', 80 | 'local2', 81 | ], 82 | 'key3' => 'local3', 83 | 'key4' => 'web4', 84 | 'key5' => 'test', 85 | ]; 86 | $this->assertEquals($expected, $web); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /tests/app/.env: -------------------------------------------------------------------------------- 1 | YII_DEBUG=0 2 | YII_ENV=dev 3 | VAR1='dotenv1' 4 | VAR2=2 5 | -------------------------------------------------------------------------------- /tests/app/config/console.php: -------------------------------------------------------------------------------- 1 | web([ 3 | 'key3' => 'console3', 4 | 'key4' => [ 5 | 'console3', 6 | 'console4', 7 | ], 8 | ]); 9 | -------------------------------------------------------------------------------- /tests/app/config/local-console.php: -------------------------------------------------------------------------------- 1 | 'lconsole3', 4 | 'key2' => [ 5 | 'lconsole1', 6 | 'lconsole2', 7 | ], 8 | ]; 9 | -------------------------------------------------------------------------------- /tests/app/config/local.php: -------------------------------------------------------------------------------- 1 | 'local3', 4 | 'key2' => [ 5 | 'local1', 6 | 'local2', 7 | ], 8 | ]; 9 | -------------------------------------------------------------------------------- /tests/app/config/web.php: -------------------------------------------------------------------------------- 1 | self::env('VAR1'), 4 | 'key2' => [ 5 | 'web1', 6 | 'web2', 7 | ], 8 | 'key3' => self::env('VAR2'), 9 | 'key4' => 'web4', 10 | ]; 11 | -------------------------------------------------------------------------------- /tests/app/otherdir/console.php: -------------------------------------------------------------------------------- 1 | web([ 3 | 'key13' => 'console3', 4 | 'key14' => [ 5 | 'console3', 6 | 'console4', 7 | ], 8 | ]); 9 | -------------------------------------------------------------------------------- /tests/app/otherdir/local-console.php: -------------------------------------------------------------------------------- 1 | 'lconsole3', 4 | 'key12' => [ 5 | 'lconsole1', 6 | 'lconsole2', 7 | ], 8 | ]; 9 | -------------------------------------------------------------------------------- /tests/app/otherdir/local.php: -------------------------------------------------------------------------------- 1 | 'local3', 4 | 'key12' => [ 5 | 'local1', 6 | 'local2', 7 | ], 8 | ]; 9 | -------------------------------------------------------------------------------- /tests/app/otherdir/web.php: -------------------------------------------------------------------------------- 1 | self::env('VAR1'), 4 | 'key12' => [ 5 | 'web1', 6 | 'web2', 7 | ], 8 | 'key13' => self::env('VAR2'), 9 | 'key14' => 'web4', 10 | ]; 11 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 6 5 | $newClass = '\PHPUnit\Framework\TestCase'; 6 | $oldClass = '\PHPUnit_Framework_TestCase'; 7 | if (!class_exists($newClass) && class_exists($oldClass)) { 8 | class_alias($oldClass, $newClass); 9 | } 10 | 11 | --------------------------------------------------------------------------------