Skip to content

Commit 3e303a2

Browse files
author
Krzysztof Chrapka
committed
Initial commit
0 parents  commit 3e303a2

22 files changed

Lines changed: 1000 additions & 0 deletions

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
root = true
3+
4+
[*]
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
vendor
3+
composer.lock
4+
bin

LICENSE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2015 Krzysztof Chrapka
2+
3+
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+
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.

README.md

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
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

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "ojezu/dynamic-parameter-bundle",
3+
"description": "Symfony3.4 bundle that facilitates dynamic parameter loading at start of each request",
4+
"autoload": {
5+
"psr-4": { "OJezu\\DynamicParameterBundle\\": "src/" }
6+
},
7+
"authors": [
8+
{
9+
"name": "Krzysztof Chrapka",
10+
"email": "composer@ojezu.org"
11+
}
12+
],
13+
"require": {
14+
"symfony/dependency-injection": "^3.4",
15+
"symfony/config": "^3.4",
16+
"symfony/http-kernel": "^3.4",
17+
"symfony/expression-language": "^3.3"
18+
},
19+
"require-dev": {
20+
"squizlabs/php_codesniffer": "^2.3"
21+
},
22+
"scripts": {
23+
"post-install-cmd": [
24+
"tool/install-hooks"
25+
],
26+
"test": [
27+
"tool/verify-all"
28+
]
29+
},
30+
"minimum-stability": "dev",
31+
"license": "MIT"
32+
}

doc/examples/app.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use OJezu\DynamicParameterBundle\Kernel\Installation;
4+
use Symfony\Component\HttpFoundation\Request;
5+
6+
/**
7+
* @var Composer\Autoload\ClassLoader
8+
*/
9+
$loader = require __DIR__.'/../app/autoload.php';
10+
include_once __DIR__.'/../var/bootstrap.php.cache';
11+
12+
$request = Request::createFromGlobals();
13+
$requestedInstallation = $request->server->get('SYMFONY_INSTALLATION'); // might be set by http server based on domain
14+
$installation = new Installation($requestedInstallation, Installation::TYPE_PROD);
15+
$kernel = new AppKernel($installation, 'live', false);
16+
17+
$response = $kernel->handle($request);
18+
$response->send();
19+
$kernel->terminate($request, $response);

doc/examples/console.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Symfony\Component\Console\Input\ArgvInput;
5+
use OJezu\DynamicParameterBundle\Kernel\Installation;
6+
use OJezu\DynamicParameterBundle\Kernel\Application;
7+
8+
$loader = require __DIR__.'/../app/autoload.php';
9+
10+
$input = new ArgvInput();
11+
$env = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: 'dev');
12+
$requestedInstallation = $input->getParameterOption(['--installation']);
13+
14+
$installation = new Installation($requestedInstallation, Installation::TYPE_PROD);
15+
$kernel = new AppKernel($installation, $env);
16+
$application = new Application($kernel);
17+
$application->run($input);

phpcs.ruleset.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP Standard">
3+
<description>PHP Standard</description>
4+
<file>.</file>
5+
<exclude-pattern>*/doc/*</exclude-pattern>
6+
<exclude-pattern>*/vendor/*</exclude-pattern>
7+
<exclude-pattern>*/tool/*</exclude-pattern>
8+
<rule ref="PSR2"/>
9+
</ruleset>

0 commit comments

Comments
 (0)