Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7d04efa

Browse filesBrowse files
matthiasnobackweaverryan
authored andcommitted
Added documentation for the Config Component
1 parent d665719 commit 7d04efa
Copy full SHA for 7d04efa

File tree

Expand file treeCollapse file tree

7 files changed

+461
-0
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+461
-0
lines changed

‎components/config/caching.rst

Copy file name to clipboard
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. index::
2+
single: Config; Caching based on resources
3+
4+
Caching based on resources
5+
==========================
6+
7+
When all configuration resources are loaded, you may want to process the configuration values and
8+
combine them all in one file. This file acts like a cache. It’s contents don’t have to be
9+
regenerated every time the application runs – only when the configuration resources are modified.
10+
11+
For example, the Symfony Routing component allows you to load all routes, and then dump a URL
12+
matcher or a URL generator based on these routes. In this case, when one of the resources is
13+
modified (and you are working in a development environment), the generated file should be
14+
invalidated and regenerated. This can be accomplished by making use of the
15+
:class:`Symfony\\Component\\Config\\ConfigCache` class.
16+
17+
The example below shows you how to collect resources, then generate some code based on the
18+
resources that were loaded, and write this code to the cache. The cache also receives the
19+
collection of resources that were used for generating the code. By looking at the “last modified”
20+
timestamp of these resources, the cache can tell if it is still fresh or that it’s contents should
21+
be regenerated.
22+
23+
.. code-block:: php
24+
25+
use Symfony\Component\Config\ConfigCache;
26+
27+
// $yamlUserFiles is filled before with an array of 'users.yml' file paths
28+
29+
$resources = array();
30+
31+
foreach ($yamlUserFiles as $yamlUserFile) {
32+
$resources[] = new FileResource($yamlUserFile);
33+
}
34+
35+
$cachePath = __DIR__ . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'appUserMatcher.php';
36+
37+
$userMatcherCache = new ConfigCache($cachePath, true);
38+
// the second constructor argument indicates whether or not we are in debug mode
39+
40+
if (!$userMatcherCache->isFresh()) {
41+
foreach ($resources as $resource) {
42+
$delegatingLoader->load($resource->getResource());
43+
}
44+
45+
// The code for the UserMatcher is generated elsewhere
46+
// $code = ...;
47+
$userMatcherCache->write($code, $resources);
48+
}
49+
50+
// you may want to require the cached code:
51+
require $cachePath;
52+
53+
A ``.meta`` file is created in the same directory as the cache file itself. This ``.meta`` file
54+
contains the serialized resources, for later reference.

‎components/config/definition.rst

Copy file name to clipboard
+284Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
.. index::
2+
single: Config; Define and process configuration values
3+
4+
Define and process configuration values
5+
=======================================
6+
7+
Validate configuration values
8+
-----------------------------
9+
10+
After loading configuration values from all kinds of resources, the values and their structure can
11+
be validated using the "Definition" part of the Config Component. Configuration values are usually
12+
expected to show some kind of hierarchy. Also, values should be of a certain type, be restricted in
13+
number or be one of a given set of values. For example, the following configuration (in Yaml) shows
14+
a clear hierarchy and some validation rules that should be applied to it (like: "the value for
15+
'auto_connect' must be a boolean value"):
16+
17+
.. code-block:: yaml
18+
19+
auto_connect: true
20+
default_connection: mysql
21+
connections:
22+
mysql:
23+
host: localhost
24+
driver: mysql
25+
username: user
26+
password: pass
27+
sqlite:
28+
host: localhost
29+
driver: sqlite
30+
memory: true
31+
username: user
32+
password: pass
33+
34+
When loading multiple configuration files, it should be possible to merge and overwrite some
35+
values. Other values should not be merged and stay as they are when first encountered. Also,
36+
some keys are only available, when another key has a specific value (in the sample configuration
37+
above: the "memory" key only makes sense when the "driver" is "sqlite").
38+
39+
Define a hierarchy of configuration values using the TreeBuilder
40+
----------------------------------------------------------------
41+
42+
All the rules concerning configuration values can be defined using the
43+
:class:`Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder`.
44+
45+
A :class:`Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder` instance should be returned
46+
from a custom ``Configuration`` class which implements the
47+
:class:`Symfony\\Component\\Config\\Definition\\ConfigurationInterface`:
48+
49+
.. code-block:: php
50+
51+
namespace Acme\DatabaseConfiguration;
52+
53+
use Symfony\Component\Config\Definition\ConfigurationInterface;
54+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
55+
56+
class DatabaseConfiguration implements ConfigurationInterface
57+
{
58+
public function getConfigTreeBuilder()
59+
{
60+
$treeBuilder = new TreeBuilder();
61+
$rootNode = $treeBuilder->root('database');
62+
63+
// add node definitions to the root of the tree
64+
65+
return $treeBuilder;
66+
}
67+
}
68+
69+
Add node definitions to the tree
70+
--------------------------------
71+
72+
Variable nodes
73+
~~~~~~~~~~~~~~
74+
75+
A tree contains node definitions which can be layed out in a semantic way. This means, using
76+
indentation and the fluent notation, it is possible to reflect the real structure of the
77+
configuration values:
78+
79+
.. code-block:: php
80+
81+
$rootNode
82+
->children()
83+
->booleanNode('auto_connect')
84+
->defaultTrue()
85+
->end()
86+
->scalarNode('default_connection')
87+
->defaultValue('default')
88+
->end()
89+
->end()
90+
;
91+
92+
The root node itself is an array node, and has children, like the boolean node "auto_connect"
93+
and the scalar node "default_connection". In general: after defining a node, a call to ``end()``
94+
takes you one step up in the hierarchy.
95+
96+
Array nodes
97+
~~~~~~~~~~~
98+
99+
It is possible to add a deeper level to the hierarchy, by adding an array node. The array node
100+
itself, may have a pre-defined set of variable nodes:
101+
102+
.. code-block:: php
103+
104+
$rootNode
105+
->arrayNode('connection')
106+
->scalarNode('driver')->end()
107+
->scalarNode('host')->end()
108+
->scalarNode('username')->end()
109+
->scalarNode('password')->end()
110+
->end()
111+
;
112+
113+
Or you may define a prototype for each node inside an array node:
114+
115+
.. code-block:: php
116+
117+
$rootNode
118+
->arrayNode('connections')
119+
->prototype('array)
120+
->children()
121+
->scalarNode('driver')->end()
122+
->scalarNode('host')->end()
123+
->scalarNode('username')->end()
124+
->scalarNode('password')->end()
125+
->end()
126+
->end()
127+
->end()
128+
;
129+
130+
A prototype can be used to add a definition which may be repeated many times inside the current
131+
node. According to the prototype definition in the example above, it is possible to have multiple
132+
connection arrays (containing a "driver", "host", etc.).
133+
134+
Array node options
135+
~~~~~~~~~~~~~~~~~~
136+
137+
Before defining the children of an array node, you can provide options like:
138+
139+
``useAttributeAsKey()``
140+
Provide the name of a childnode, whose value should be used as the key in the resulting array
141+
``requiresAtLeastOneElement()``
142+
There should be at least one element in the array (works only when ``isRequired()`` is also
143+
called).
144+
145+
An example of this:
146+
147+
.. code-block:: php
148+
149+
$rootNode
150+
->arrayNode('parameters')
151+
->isRequired()
152+
->requiresAtLeastOneElement()
153+
->prototype('array')
154+
->useAttributeAsKey('name')
155+
->children()
156+
->scalarNode('name')->isRequired()->end()
157+
->scalarNode('value')->isRequired()->end()
158+
->end()
159+
->end()
160+
->end()
161+
;
162+
163+
Default and required values
164+
---------------------------
165+
166+
For all node types, it is possible to define default values and replacement values in case a node
167+
has a certain value:
168+
169+
``defaultValue()``
170+
Set a default value
171+
``isRequired()``
172+
Must be defined (but may be empty)
173+
``cannotBeEmpty()``
174+
may not contain an empty value
175+
``default*()``
176+
(``Null``, ``True``, ``False``), shortcut for ``defaultValue()``
177+
``treat*Like()``
178+
(``Null``, ``True``, ``False``), provide a replacement value in case the value is *.
179+
180+
.. code-block:: php
181+
182+
$rootNode
183+
->arrayNode('connection')
184+
->children()
185+
->scalarNode('driver')
186+
->isRequired()
187+
->cannotBeEmpty()
188+
->end()
189+
->scalarNode('host')
190+
->defaultValue('localhost')
191+
->end()
192+
->scalarNode('username')->end()
193+
->scalarNode('password')->end()
194+
->booleanNode('memory')
195+
->defaultFalse()
196+
->end()
197+
->end()
198+
->end()
199+
;
200+
201+
Merging options
202+
---------------
203+
204+
Extra options concerning the merge process may be provided. For arrays:
205+
206+
``performNoDeepMerging()``
207+
When the value is also defined in a second configuration array, don’t try to merge an array,
208+
but overwrite it entirely
209+
210+
For all nodes:
211+
212+
``cannotBeOverwritten()``
213+
don’t let other configuration arrays overwrite an existing value for this node
214+
215+
Validation rules
216+
----------------
217+
218+
More advanced validation rules can be provided using the
219+
:class:`Symfony\\Component\\Config\\Definition\\Builder\\ExprBuilder`. This builder implements a
220+
fluent interface for a well-known control structure. The builder is used for adding advanced
221+
validation rules to node definitions, like:
222+
223+
.. code-block:: php
224+
225+
$rootNode
226+
->arrayNode('connection')
227+
->children()
228+
->scalarNode('driver')
229+
->isRequired()
230+
->validate()
231+
->ifNotInArray(array('mysql', 'sqlite', 'mssql'))
232+
->thenInvalid('Invalid database driver "%s"')
233+
->end()
234+
->end()
235+
->end()
236+
->end()
237+
;
238+
239+
A validation rule always has an "if" part. You can specify this part in the following ways:
240+
241+
- ``ifTrue()``
242+
- ``ifString()``
243+
- ``ifNull()``
244+
- ``ifArray()``
245+
- ``ifInArray()``
246+
- ``ifNotInArray()``
247+
248+
A validation rule also requires a "then" part:
249+
250+
- ``then()``
251+
- ``thenEmptyArray()``
252+
- ``thenInvalid()``
253+
- ``thenUnset()``
254+
255+
The only exception is of course:
256+
257+
- ``always()``
258+
259+
Usually, “then” is a closure. It’s return value will be used as a new value for the node, instead
260+
of the node’s original value.
261+
262+
Processing configuration values
263+
-------------------------------
264+
265+
The :class:`Symfony\\Component\\Config\\Definition\\Processor` uses the tree as it was built
266+
using the :class:`Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder` to process
267+
multiple arrays of configuration values that should be merged. If any value is not of the
268+
expected type, is mandatory and yet undefined, or could not be validated in some other way,
269+
an exception will be thrown. Otherwise the result is a clean array of configuration values.
270+
271+
.. code-block:: php
272+
273+
use Symfony\Component\Yaml\Yaml;
274+
use Symfony\Component\Config\Definition\Processor;
275+
use Acme\DatabaseConfiguration;
276+
277+
$config1 = Yaml::parse(__DIR__.'/src/Matthias/config/config.yml');
278+
$config2 = Yaml::parse(__DIR__.'/src/Matthias/config/config_extra.yml');
279+
280+
$configs = array($config1, $config2);
281+
282+
$processor = new Processor;
283+
$configuration = new DatabaseConfiguration;
284+
$processedConfiguration = $processor->processConfiguration($configuration, $configs);

‎components/config/index.rst

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Config
2+
======
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
introduction
8+
resources
9+
caching
10+
definition

‎components/config/introduction.rst

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.. index::
2+
single: Config
3+
4+
The Config Component
5+
====================
6+
7+
Introduction
8+
------------
9+
10+
The Config Component provides several classes to help you find, load, combine, autofill and
11+
validate configuration values of any kind, whatever their source may be (Yaml, XML, INI files, or
12+
for instance a database).
13+
14+
Installation
15+
------------
16+
17+
You can install the component in many different ways:
18+
19+
* Use the official Git repository (https://github.com/symfony/Config);
20+
* Install it via PEAR ( `pear.symfony.com/Config`);
21+
* Install it via Composer (`symfony/config` on Packagist).

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.