|
| 1 | +# OJezu/DynamicParameterBundle |
| 2 | + |
| 3 | + |
| 4 | +This bundle enables having multiple configurations in one Symfony application. It does so, by providing two independent features: |
| 5 | + |
| 6 | + * Installation-aware kernel, and installation-dependent parameters |
| 7 | + * Advanced parameter provider, that can read parameters from any source and supports paths. |
| 8 | + |
| 9 | + |
| 10 | +Combination of this features allow for example, implementation of multi-tenant application, that is configured to use completely different resources (data base connection, filesystem adapters, etc.) for each tenant. Moreover, configuration management can be off-loaded to a database server, JSON file, zookeeper instance... |
| 11 | + |
| 12 | +### Requirements |
| 13 | + |
| 14 | + |
| 15 | +This bundle requires Symfony 3.4, as it depends on advanced environment variable processing. |
| 16 | + |
| 17 | +### Usage |
| 18 | + |
| 19 | +**Note:** Processors from this bundle *do not* read actual environment variables. |
| 20 | + |
| 21 | +#### Multi-installation |
| 22 | + |
| 23 | +Provide kernel with information about installation (see configuration below), and then in your configuration you can use those parameters. |
| 24 | + |
| 25 | +```yaml |
| 26 | +#app/config/config.yml |
| 27 | + |
| 28 | +ojezu_dynamic_parameter: |
| 29 | + multi_installation: true |
| 30 | + |
| 31 | +file_storage: |
| 32 | + bucket: "/myapp/installation/%env(ojezu_installation:name)%/" |
| 33 | +``` |
| 34 | +
|
| 35 | +It's similar to plain environment variables, but gives more control, as it's developer who decides what and from where will find its way into `Installation` object. All `Installation` public properties can be accessed, and you can swap it for extended one, with properties you need. |
| 36 | + |
| 37 | +#### Advanced parameter provider |
| 38 | + |
| 39 | +After configuring advanced parameter provider (see below), you are able to map parameters to abstract configuration paths used to obtain parameter values from any source, as long as there is a provider for that source. Providers are very simple services, that just have to implement `ParameterProviderInterface`. JSON file provider is already provided by this bundle, more powerful than Symfony built-in "json:" env variable processor. |
| 40 | + |
| 41 | +```yaml |
| 42 | +#app/config/config.yml |
| 43 | +
|
| 44 | +ojezu_dynamic_parameter: |
| 45 | + advanced_parameters: |
| 46 | + json_provider: |
| 47 | + file_path: '%kernel.root_dir%/config/config.json' |
| 48 | + processor: |
| 49 | + parameter_map: |
| 50 | + database_host: { path: ['database', 'host'] } |
| 51 | +
|
| 52 | +doctrine: |
| 53 | + dbal: |
| 54 | + driver: pdo_mysql |
| 55 | + server_version: 5.7 |
| 56 | + host: "%env(ojezu_param:database_host)%" |
| 57 | +``` |
| 58 | + |
| 59 | +This configuration will find database.host value in JSON config file, and provide it to DBAL configuration. |
| 60 | + |
| 61 | +#### Using them together |
| 62 | + |
| 63 | +While both features offer more than what's built in Symfony 3.4, using them together allows for easy management of multiple configurations supported by same Symfony application. |
| 64 | + |
| 65 | +```yaml |
| 66 | +#app/config/config.yml |
| 67 | +
|
| 68 | +ojezu_dynamic_parameter: |
| 69 | + multi_installation: true |
| 70 | + advanced_parameters: |
| 71 | + json_provider: |
| 72 | + file_path: '%kernel.root_dir%/config/config.json' |
| 73 | + processor: |
| 74 | + parameter_map: |
| 75 | + database_name: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'name'] } |
| 76 | +
|
| 77 | +doctrine: |
| 78 | + dbal: |
| 79 | + driver: pdo_mysql |
| 80 | + server_version: 5.7 |
| 81 | + host: "mysql.example.com" |
| 82 | + dbname: "%env(ojezu_param:database_name)%" |
| 83 | +``` |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "installation": { |
| 88 | + "application1": { |
| 89 | + "database": { |
| 90 | + "name": "app1_database", |
| 91 | + } |
| 92 | + }, |
| 93 | + "application2": { |
| 94 | + "database": { |
| 95 | + "name": "app2_database", |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### Configuration |
| 103 | + |
| 104 | +#### Multi-installation |
| 105 | + |
| 106 | + |
| 107 | +In order to be able to use multi-installation support: |
| 108 | + |
| 109 | +1. Enable it in configuration: |
| 110 | + |
| 111 | + ```yaml |
| 112 | + #app/config/config.yml |
| 113 | +
|
| 114 | + ojezu_dynamic_parameter: |
| 115 | + multi_installation: true |
| 116 | + ``` |
| 117 | + |
| 118 | +2. Change your AppKernel to extend `\OJezu\DynamicParameterBundle\Kernel\Kernel` |
| 119 | + |
| 120 | + ```php |
| 121 | + <?php |
| 122 | +
|
| 123 | + use \OJezu\DynamicParameterBundle\Kernel\Kernel; |
| 124 | +
|
| 125 | + class AppKernel extends Kernel |
| 126 | + { |
| 127 | + (...) |
| 128 | + } |
| 129 | + ``` |
| 130 | + |
| 131 | +3. Make sure that in all places where kernel is created in your application, it is provided with `Installation` instance. Kernel is usually created by `web/*.php` files, but remember to modify your `bin/console` too. |
| 132 | + |
| 133 | + ```php |
| 134 | + <?php |
| 135 | + (...) |
| 136 | + $installation = new Installation($requestedInstallation, Installation::TYPE_PROD); |
| 137 | + $kernel = new AppKernel($installation, $env, $debug); |
| 138 | + ``` |
| 139 | + |
| 140 | +4. In `bin/console` be sure to also swap `Application` with one provided by this bundle, if you want to specify installation via CLI option - otherwise parsing of argv may introduce problems. |
| 141 | + |
| 142 | +Complete examples can be found in `doc/examples` directory |
| 143 | + |
| 144 | + |
| 145 | +#### Advanced parameter provider |
| 146 | + |
| 147 | +You must provide mapping for supported parameters. It is required due to limitations in `%env(processor:variable)%` syntax, and to allow paths that can be easily adapted to any configuration storage. |
| 148 | + |
| 149 | +```yaml |
| 150 | +ojezu_dynamic_parameter: |
| 151 | + advanced_parameters: |
| 152 | + json_provider: |
| 153 | + file_path: '%kernel.root_dir%/config/config.json' |
| 154 | + processor: |
| 155 | + parameter_map: |
| 156 | + database_host: { path: ['database', 'host'] } |
| 157 | + database_name: { path: ['database', 'name'] } |
| 158 | + database_user: { path: ['database', 'user'] } |
| 159 | +``` |
| 160 | + |
| 161 | +Those parameters can later be used in all places in your application configuration, no matter support from configured bundle: |
| 162 | + |
| 163 | +```yaml |
| 164 | +doctrine: |
| 165 | + dbal: |
| 166 | + driver: pdo_mysql |
| 167 | + server_version: 5.7 |
| 168 | + host: "%env(ojezu_param:database_host)%" |
| 169 | + dbname: "%env(ojezu_param:database_name)%" |
| 170 | + user: "%env(ojezu_param:database_user)%" |
| 171 | +``` |
| 172 | + |
| 173 | +##### Using other parameters |
| 174 | + |
| 175 | +In paths other parameters can be used, including `ojezu_installation` parameters. |
| 176 | + |
| 177 | +```yaml |
| 178 | +ojezu_dynamic_parameter: |
| 179 | + multi_installation: true |
| 180 | + advanced_parameters: |
| 181 | + json_provider: |
| 182 | + file_path: '%kernel.root_dir%/config/config.json' |
| 183 | + processor: |
| 184 | + parameter_map: |
| 185 | + database_host: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'host'] } |
| 186 | + database_name: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'name'] } |
| 187 | + database_user: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'user'] } |
| 188 | +``` |
| 189 | + |
| 190 | +##### Changing providers |
| 191 | + |
| 192 | +You can swap out json_provider for any other service implementing `OJezu\DynamicParameterBundle\Service\ParameterProviderInterface` interface, by removing json_provider section, and adding provider configuration. |
| 193 | + |
| 194 | +```yaml |
| 195 | +ojezu_dynamic_parameter: |
| 196 | + multi_installation: true |
| 197 | + advanced_parameters: |
| 198 | + provider: |
| 199 | + service: 'MyAppBundle\Services\RedisParameterProvider' # this is service! not class. |
| 200 | + processor: |
| 201 | + parameter_map: |
| 202 | + database_host: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'host'] } |
| 203 | + database_name: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'name'] } |
| 204 | + database_user: { path: ['installation', '%env(ojezu_installation:name)%', 'database', 'user'] } |
| 205 | +``` |
| 206 | + |
| 207 | +Keep in mind that your provider is a service - it can have its arguments injected, it can be tagged etc. As long as there is no cycle it will work like any other service (no use trying to inject `ojezu_param`s there! |
| 208 | + |
| 209 | + |
| 210 | +##### Defaults? |
| 211 | + |
| 212 | +Yes. |
| 213 | + |
| 214 | +```yaml |
| 215 | +ojezu_dynamic_parameter: |
| 216 | + advanced_parameters: |
| 217 | + json_provider: |
| 218 | + file_path: '%kernel.root_dir%/config/config.json' |
| 219 | + processor: |
| 220 | + parameter_map: |
| 221 | + database_host: |
| 222 | + path: ['database', 'host'] |
| 223 | + default: 'localhost' |
| 224 | +``` |
| 225 | + |
| 226 | +Keep in mind that your provider is a service - it can have its arguments injected, it can be tagged etc. As long as there is no cycle it will work like any other service (no use trying to inject `ojezu_param`s there! |
| 227 | + |
| 228 | +##### No config mode |
| 229 | + |
| 230 | +In some instances there is no configuration to be loaded - e.g. when warming cache. For those instances there is no config mode, in which provider won't be used, and all variables will be resolved to null, unless given explicit value for use in those scenarios. *Defaults won't be used.* |
| 231 | + |
| 232 | +Enable it by using `load_configuration` option in processor section: |
| 233 | + |
| 234 | +```yaml |
| 235 | +ojezu_dynamic_parameter: |
| 236 | + multi_installation: true |
| 237 | + advanced_parameters: |
| 238 | + json_provider: |
| 239 | + file_path: '%kernel.root_dir%/config/config.json' |
| 240 | + processor: |
| 241 | + load_configuration: '%env(bool:ojezu_installation:name)% |
| 242 | + parameter_map: |
| 243 | + database_host: {path: ['installation', '%env(ojezu_installation:name)%', 'database', 'host']} |
| 244 | + log_channel: {path: ['log', '%env(ojezu_installation:name)%'], default: 'default', no_config_value: 'default'} |
| 245 | + bucket_name: {path: ['buckets', '%env(ojezu_installation:name)%'], no_config_value: '%env(LOCAL_BUCKET)%'} |
| 246 | +``` |
| 247 | + |
| 248 | +### Extending this bundle |
| 249 | + |
| 250 | +Points for expansion are |
| 251 | + |
| 252 | + * `Installation` value object for ojezu_installation |
| 253 | + * Parameter providers |
| 254 | + |
| 255 | +If you need more options from `Installation`, extend that class with additional public properties or methods, and use your extended class in its place. |
| 256 | + |
| 257 | +New providers can be written by extending `OJezu\DynamicParameterBundle\Service\ParameterProviderInterface` and configured as described in Configuration part of this ReadMe |
| 258 | + |
| 259 | +### Testing |
| 260 | + |
| 261 | +would be nice. |
| 262 | + |
| 263 | + |
| 264 | +License |
| 265 | +=== |
| 266 | +MIT |
0 commit comments