|
| 1 | +Yii2 Configloader |
| 2 | +================= |
| 3 | + |
| 4 | +[](https://packagist.org/packages/codemix/yii2-configloader) |
| 5 | +[](https://packagist.org/packages/codemix/yii2-configloader) |
| 6 | +[](https://packagist.org/packages/codemix/yii2-configloader) |
| 7 | + |
| 8 | +Build configuration arrays from config files and environment variables. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +You can use this extension to solve some or all of the following tasks: |
| 13 | + |
| 14 | + * Build Yii2 configuration arrays for web and console application |
| 15 | + * Initialize Yii environment (`YII_DEBUG`, `YII_ENV`) from environment variables |
| 16 | + * Load environment variables from a `.env` file |
| 17 | + * Get config options from environment variables |
| 18 | + * Load local configuration overrides |
| 19 | + * Streamline config initialization and Yii 2 bootstrapping |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Install the package with [composer](http://getcomposer.org): |
| 24 | + |
| 25 | + composer require codemix/yii2-configloader |
| 26 | + |
| 27 | + |
| 28 | +## Description |
| 29 | + |
| 30 | +We mainly use this extension to configure our dockerized yii2 applications. |
| 31 | +It's [good practice](https://12factor.net/) to build your docker applications in such a way, |
| 32 | +that the runtime configuration in productive mode happens solely via environment variables. |
| 33 | + |
| 34 | +But during local development we can loosen these strict requirement a little as we |
| 35 | +sometimes have to add debug options or the like that should not be part of the main |
| 36 | +configuration. Here the extension helps to override settings with local configuration |
| 37 | +files that live outside of version control. |
| 38 | + |
| 39 | +You have several options how to use this extension: |
| 40 | + |
| 41 | + * Use only the Yii environment initialization |
| 42 | + * Use only the configuration loader |
| 43 | + * Use both |
| 44 | + |
| 45 | +We first show how to use the first two options "standalone" and then a third, |
| 46 | +combined way that includes all features. |
| 47 | + |
| 48 | + |
| 49 | +### Initializing Yii environment |
| 50 | + |
| 51 | +This will set the `YII_DEBUG` and `YII_ENV` variables according to the respective |
| 52 | +environment variables if those are set. It can also load them from a `.env` file. |
| 53 | + |
| 54 | +In debug mode `error_reporting()` will also be set to `E_ALL`. |
| 55 | + |
| 56 | +```php |
| 57 | +<?php |
| 58 | +use codemix\yii2confload\Config; |
| 59 | + |
| 60 | +Config::initEnv('/path/to/app'); |
| 61 | +$setting = Config::env('MY_SETTING', 'default'); |
| 62 | +``` |
| 63 | + |
| 64 | +If you leave away the application path, no `.env` file will be loaded. |
| 65 | + |
| 66 | + |
| 67 | +### Loading configuration |
| 68 | + |
| 69 | +The extension requires the following naming scheme for your configuration files: |
| 70 | + |
| 71 | + * `config/web.php` - Web configuration |
| 72 | + * `config/console.php` - Console configuration |
| 73 | + * `config/local.php` - Local overrides for the web configuration (optional) |
| 74 | + * `config/local-console.php` - Local overrides for the console configuration (optional) |
| 75 | + |
| 76 | +If you only want to load configuration from files but whithout initializing the Yii |
| 77 | +environments as shown above, you'd create a `Config` instance and pass the application |
| 78 | +base directory and, as second argument, `false` to the constructor: |
| 79 | + |
| 80 | +```php |
| 81 | +<?php |
| 82 | +use codemix\yii2confload\Config; |
| 83 | +$config = new Config('/path/to/app', false); |
| 84 | +// Reads configuration from config/web.php |
| 85 | +$webConfig = $config->web(); |
| 86 | +``` |
| 87 | + |
| 88 | +#### Local configuration |
| 89 | + |
| 90 | +By default local configuration files `local.php` and `local-console.php` are not |
| 91 | +loaded. To activate this feature you can either set the `ENABLE_LOCALCONF` environment |
| 92 | +variable (either in your server environment or in `.env`): |
| 93 | + |
| 94 | +``` |
| 95 | +ENABLE_LOCALCONF=1 |
| 96 | +``` |
| 97 | + |
| 98 | +Now any call to `web()` or `console()` will return the merged result of `config/web.php` + |
| 99 | +`config/console.php` or `config/console.php` + `config/local-console.php` respectively. |
| 100 | + |
| 101 | + |
| 102 | +Alternatively you can explicitely ask for local configuration: |
| 103 | + |
| 104 | +```php |
| 105 | +<?php |
| 106 | +use codemix\yii2confload\Config; |
| 107 | +$config = new Config('/path/to/app', false); |
| 108 | +// Merges configuration from config/web.php and config/local.php if present |
| 109 | +$webConfig = $config->web([], true); |
| 110 | +// Merges configuration from config/console.php and config/local-console.php if present |
| 111 | +$consoleConfig = $config->console([], true); |
| 112 | +``` |
| 113 | + |
| 114 | +#### Merging custom configuration |
| 115 | + |
| 116 | +You can also inject some other configuration when you fetch the web or console config: |
| 117 | + |
| 118 | +```php |
| 119 | +<?php |
| 120 | +use codemix\yii2confload\Config; |
| 121 | +$config = new Config('/path/to/app', false); |
| 122 | +$webConfig = $config->web(['id' => 'test'], true); |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | +### Initialize Yii environment and load configuration |
| 127 | + |
| 128 | +Let's finally show a full example that demonstrates how to use all the mentioned |
| 129 | +features in one go. A typical setup will use the following files: |
| 130 | + |
| 131 | +#### `.env` |
| 132 | + |
| 133 | +Here we define the Yii environment and DB credentials. You'd add more config options |
| 134 | +in the same manner: |
| 135 | + |
| 136 | +``` |
| 137 | +YII_DEBUG=1 |
| 138 | +YII_ENV=dev |
| 139 | +
|
| 140 | +DB_DSN=mysql:host=db.example.com;dbname=web |
| 141 | +DB_USER=user |
| 142 | +DB_PASSWORD='**secret**' |
| 143 | +``` |
| 144 | + |
| 145 | +#### `config/web.php` |
| 146 | + |
| 147 | +This file is later included in the scope of `codemix\yii2confload\Config`, so you |
| 148 | +can easily access instance and class methods: |
| 149 | + |
| 150 | +```php |
| 151 | +<?php |
| 152 | +/* @var codemix\yii2confload\Config $this */ |
| 153 | +return [ |
| 154 | + 'components' => [ |
| 155 | + 'db' => [ |
| 156 | + 'dsn' => self::env('DB_DSN', 'mysql:host=db;dbname=web'), |
| 157 | + 'username' => self::env('DB_USER', 'web'), |
| 158 | + 'password' => self::env('DB_PASSWORD', 'web'), |
| 159 | + ], |
| 160 | +``` |
| 161 | + |
| 162 | +#### `config/console.php` |
| 163 | + |
| 164 | +Having access to the config instance allows for example to reuse parts of your web |
| 165 | +configuration in your console config. |
| 166 | + |
| 167 | +```php |
| 168 | +<?php |
| 169 | +/* @var codemix\yii2confload\Config $this */ |
| 170 | + |
| 171 | +$web = $this->web(); |
| 172 | +return [ |
| 173 | + // ... |
| 174 | + 'components' => [ |
| 175 | + 'db' => $web['components']['db'], |
| 176 | +``` |
| 177 | + |
| 178 | +#### `web/index.php` |
| 179 | + |
| 180 | +We've streamlined the process of setting up a `Config` object and loading the |
| 181 | +Yii 2 bootstrap file into a single method `Config::boostrap()` which only |
| 182 | +receives the application directory as argument. |
| 183 | + |
| 184 | +```php |
| 185 | +<?php |
| 186 | +use codemix\yii2confload\Config; |
| 187 | + |
| 188 | +require(__DIR__ . '/../vendor/autoload.php'); |
| 189 | +$config = Config::bootstrap(__DIR__ . '/..'); |
| 190 | +Yii::createObject('yii\web\Application', [$config->web()])->run(); |
| 191 | +``` |
| 192 | + |
| 193 | +This makes sure that things are loaded in the right order. If you prefer a more |
| 194 | +verbose version, the code above is equivalent to: |
| 195 | + |
| 196 | +```php |
| 197 | +<?php |
| 198 | +use codemix\yii2confload\Config; |
| 199 | + |
| 200 | +require(__DIR__ . '/../vendor/autoload.php'); |
| 201 | +$config = new Config(__DIR__ . '/..'); |
| 202 | +require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); |
| 203 | + |
| 204 | +Yii::createObject('yii\web\Application', [$config->web()])->run(); |
| 205 | +``` |
| 206 | + |
| 207 | +#### `yii` |
| 208 | + |
| 209 | +The same approach is used for the console application: |
| 210 | + |
| 211 | +```php |
| 212 | +<?php |
| 213 | +use codemix\yii2confload\Config; |
| 214 | + |
| 215 | +require(__DIR__ . '/vendor/autoload.php'); |
| 216 | +$config = Config::bootstrap(__DIR__); |
| 217 | +$application = Yii::createObject('yii\console\Application', [$config->console()]); |
| 218 | +exit($application->run()); |
| 219 | +``` |
0 commit comments