|
| 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 |
| 11 | +and their structure can be validated using the "Definition" part of the Config |
| 12 | +Component. Configuration values are usually expected to show some kind of |
| 13 | +hierarchy. Also, values should be of a certain type, be restricted in number |
| 14 | +or be one of a given set of values. For example, the following configuration |
| 15 | +(in Yaml) shows a clear hierarchy and some validation rules that should be |
| 16 | +applied to it (like: "the value for ``auto_connect`` must be a boolean value"): |
| 17 | + |
| 18 | +.. code-block:: yaml |
| 19 | +
|
| 20 | + auto_connect: true |
| 21 | + default_connection: mysql |
| 22 | + connections: |
| 23 | + mysql: |
| 24 | + host: localhost |
| 25 | + driver: mysql |
| 26 | + username: user |
| 27 | + password: pass |
| 28 | + sqlite: |
| 29 | + host: localhost |
| 30 | + driver: sqlite |
| 31 | + memory: true |
| 32 | + username: user |
| 33 | + password: pass |
| 34 | +
|
| 35 | +When loading multiple configuration files, it should be possible to merge |
| 36 | +and overwrite some values. Other values should not be merged and stay as |
| 37 | +they are when first encountered. Also, some keys are only available when |
| 38 | +another key has a specific value (in the sample configuration above: the |
| 39 | +``memory`` key only makes sense when the ``driver`` is ``sqlite``). |
| 40 | + |
| 41 | +Define a hierarchy of configuration values using the TreeBuilder |
| 42 | +---------------------------------------------------------------- |
| 43 | + |
| 44 | +All the rules concerning configuration values can be defined using the |
| 45 | +:class:`Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder`. |
| 46 | + |
| 47 | +A :class:`Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder` instance |
| 48 | +should be returned from a custom ``Configuration`` class which implements the |
| 49 | +:class:`Symfony\\Component\\Config\\Definition\\ConfigurationInterface`:: |
| 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. |
| 76 | +This means, using indentation and the fluent notation, it is possible to |
| 77 | +reflect the real structure of the configuration values:: |
| 78 | + |
| 79 | + $rootNode |
| 80 | + ->children() |
| 81 | + ->booleanNode('auto_connect') |
| 82 | + ->defaultTrue() |
| 83 | + ->end() |
| 84 | + ->scalarNode('default_connection') |
| 85 | + ->defaultValue('default') |
| 86 | + ->end() |
| 87 | + ->end() |
| 88 | + ; |
| 89 | + |
| 90 | +The root node itself is an array node, and has children, like the boolean |
| 91 | +node ``auto_connect`` and the scalar node ``default_connection``. In general: |
| 92 | +after defining a node, a call to ``end()`` takes you one step up in the hierarchy. |
| 93 | + |
| 94 | +Array nodes |
| 95 | +~~~~~~~~~~~ |
| 96 | + |
| 97 | +It is possible to add a deeper level to the hierarchy, by adding an array |
| 98 | +node. The array node itself, may have a pre-defined set of variable nodes: |
| 99 | + |
| 100 | +.. code-block:: php |
| 101 | +
|
| 102 | + $rootNode |
| 103 | + ->arrayNode('connection') |
| 104 | + ->scalarNode('driver')->end() |
| 105 | + ->scalarNode('host')->end() |
| 106 | + ->scalarNode('username')->end() |
| 107 | + ->scalarNode('password')->end() |
| 108 | + ->end() |
| 109 | + ; |
| 110 | +
|
| 111 | +Or you may define a prototype for each node inside an array node: |
| 112 | + |
| 113 | +.. code-block:: php |
| 114 | +
|
| 115 | + $rootNode |
| 116 | + ->arrayNode('connections') |
| 117 | + ->prototype('array') |
| 118 | + ->children() |
| 119 | + ->scalarNode('driver')->end() |
| 120 | + ->scalarNode('host')->end() |
| 121 | + ->scalarNode('username')->end() |
| 122 | + ->scalarNode('password')->end() |
| 123 | + ->end() |
| 124 | + ->end() |
| 125 | + ->end() |
| 126 | + ; |
| 127 | +
|
| 128 | +A prototype can be used to add a definition which may be repeated many times |
| 129 | +inside the current node. According to the prototype definition in the example |
| 130 | +above, it is possible to have multiple connection arrays (containing a ``driver``, |
| 131 | +``host``, etc.). |
| 132 | + |
| 133 | +Array node options |
| 134 | +~~~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +Before defining the children of an array node, you can provide options like: |
| 137 | + |
| 138 | +``useAttributeAsKey()`` |
| 139 | + Provide the name of a child node, whose value should be used as the key in the resulting array |
| 140 | +``requiresAtLeastOneElement()`` |
| 141 | + There should be at least one element in the array (works only when ``isRequired()`` is also |
| 142 | + called). |
| 143 | + |
| 144 | +An example of this: |
| 145 | + |
| 146 | +.. code-block:: php |
| 147 | +
|
| 148 | + $rootNode |
| 149 | + ->arrayNode('parameters') |
| 150 | + ->isRequired() |
| 151 | + ->requiresAtLeastOneElement() |
| 152 | + ->prototype('array') |
| 153 | + ->useAttributeAsKey('name') |
| 154 | + ->children() |
| 155 | + ->scalarNode('name')->isRequired()->end() |
| 156 | + ->scalarNode('value')->isRequired()->end() |
| 157 | + ->end() |
| 158 | + ->end() |
| 159 | + ->end() |
| 160 | + ; |
| 161 | +
|
| 162 | +Default and required values |
| 163 | +--------------------------- |
| 164 | + |
| 165 | +For all node types, it is possible to define default values and replacement |
| 166 | +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 |
| 208 | + try to merge an array, 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 |
| 220 | +builder implements a fluent interface for a well-known control structure. |
| 221 | +The builder is used for adding advanced validation rules to node definitions, like:: |
| 222 | + |
| 223 | + $rootNode |
| 224 | + ->arrayNode('connection') |
| 225 | + ->children() |
| 226 | + ->scalarNode('driver') |
| 227 | + ->isRequired() |
| 228 | + ->validate() |
| 229 | + ->ifNotInArray(array('mysql', 'sqlite', 'mssql')) |
| 230 | + ->thenInvalid('Invalid database driver "%s"') |
| 231 | + ->end() |
| 232 | + ->end() |
| 233 | + ->end() |
| 234 | + ->end() |
| 235 | + ; |
| 236 | + |
| 237 | +A validation rule always has an "if" part. You can specify this part in the |
| 238 | +following ways: |
| 239 | + |
| 240 | +- ``ifTrue()`` |
| 241 | +- ``ifString()`` |
| 242 | +- ``ifNull()`` |
| 243 | +- ``ifArray()`` |
| 244 | +- ``ifInArray()`` |
| 245 | +- ``ifNotInArray()`` |
| 246 | +- ``always()`` |
| 247 | + |
| 248 | +A validation rule also requires a "then" part: |
| 249 | + |
| 250 | +- ``then()`` |
| 251 | +- ``thenEmptyArray()`` |
| 252 | +- ``thenInvalid()`` |
| 253 | +- ``thenUnset()`` |
| 254 | + |
| 255 | +Usually, "then" is a closure. Its return value will be used as a new value |
| 256 | +for the node, instead |
| 257 | +of the node's original value. |
| 258 | + |
| 259 | +Processing configuration values |
| 260 | +------------------------------- |
| 261 | + |
| 262 | +The :class:`Symfony\\Component\\Config\\Definition\\Processor` uses the tree |
| 263 | +as it was built using the :class:`Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder` |
| 264 | +to process multiple arrays of configuration values that should be merged. |
| 265 | +If any value is not of the expected type, is mandatory and yet undefined, |
| 266 | +or could not be validated in some other way, an exception will be thrown. |
| 267 | +Otherwise the result is a clean array of configuration values:: |
| 268 | + |
| 269 | + use Symfony\Component\Yaml\Yaml; |
| 270 | + use Symfony\Component\Config\Definition\Processor; |
| 271 | + use Acme\DatabaseConfiguration; |
| 272 | + |
| 273 | + $config1 = Yaml::parse(__DIR__.'/src/Matthias/config/config.yml'); |
| 274 | + $config2 = Yaml::parse(__DIR__.'/src/Matthias/config/config_extra.yml'); |
| 275 | + |
| 276 | + $configs = array($config1, $config2); |
| 277 | + |
| 278 | + $processor = new Processor(); |
| 279 | + $configuration = new DatabaseConfiguration; |
| 280 | + $processedConfiguration = $processor->processConfiguration($configuration, $configs); |
0 commit comments