-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
#962 [Config] Bootstrapped Config Component Documentation #1184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
cordoval
wants to merge
2
commits into
symfony:master
from
cordoval:feature/configBootstrappedDocumentation
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
.. index:: | ||
single: Config | ||
|
||
The Config Component | ||
==================== | ||
|
||
Problem to solve | ||
---------------- | ||
|
||
Projects are configurable in one way or another. We usually want to abstract | ||
important data be it configuration data, fixtures data, or any other data | ||
an application can use. We could have just leave these information written | ||
within the code, however providing these data in a human or in a developer | ||
or even in a client friendly way could make a project be maintainable. The | ||
Symfony2 framework is a good example on how a framework can rely on configuration | ||
data to be specified in a human readable way making the project very accessible | ||
to be tuned to specific needs, improved or maintained. The data configuration | ||
in the Symfony2 framework is in most part service specificaton data, but | ||
there is other type of data such as security, routing, and asset. There | ||
is no limit to what kind of data type you can work with when using the | ||
Config component. If you can determine certain resources where to store | ||
this information and you design your project so to keep in mind this incoming | ||
configuration data from these sources then Config component can be used. | ||
|
||
Therefore Config provides the infrastructure for loading configurations | ||
from different data sources and optionally monitoring these data sources | ||
for changes. There are additional tools for validating, normalizing and | ||
handling of defaults that can optionally be used to convert from different | ||
formats to arrays. | ||
|
||
Installation | ||
------------ | ||
|
||
You can install the component in many different ways: | ||
|
||
* Use the official Git repository (https://github.com/symfony/Config); | ||
* Install it via PEAR ( `pear.symfony.com/Config`); | ||
* Install it via Composer (`symfony/Config` on Packagist). | ||
|
||
Architecture | ||
------------ | ||
|
||
wip | ||
|
||
Usage | ||
----- | ||
|
||
The main class is the configuration class: | ||
|
||
.. code-block:: php | ||
|
||
namespace Project\Configuration; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('acme_demo'); | ||
|
||
return $rootNode; | ||
} | ||
} | ||
|
||
A boolean node | ||
~~~~~~~~~~~~~~ | ||
|
||
There are many ways to specify validation rules for configuration values, | ||
but first, let’s create a fresh node in the tree for our still invalid value | ||
“enabled”. The value should always be of type “boolean”, so before we return | ||
$rootNode, we add: | ||
|
||
.. code-block:: php | ||
|
||
$rootNode-> | ||
children() | ||
->booleanNode('enabled')->end() | ||
end() | ||
; | ||
|
||
As you can see, the TreeBuilder and the nodes all implement a fluent interface, | ||
so we can write the tree down in a semantic way: the way it looks reflects | ||
the way it is. | ||
|
||
The calls to end() mean that we want to move up again to the parent node | ||
of the current node. | ||
|
||
ScalarNode | ||
~~~~~~~~~~ | ||
|
||
Let’s add a node for scalar values (like strings, numbers, etc.): | ||
|
||
.. code-block:: php | ||
|
||
$rootNode-> | ||
children() | ||
->booleanNode('enabled')->end() | ||
->scalarNode('default_user') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
end() | ||
; | ||
|
||
The new node defines a config value “default_user” which is required and | ||
may not be empty. | ||
|
||
ArrayNode | ||
~~~~~~~~~ | ||
|
||
Now, let’s add an array node, which allows for an array of users to be defined | ||
in the config file: | ||
|
||
.. code-block:: php | ||
|
||
$rootNode-> | ||
children() | ||
->booleanNode('enabled')->end() | ||
->scalarNode('default_user') | ||
->isRequired() | ||
->cannotBeEmpty() | ||
->end() | ||
->arrayNode('users') | ||
->requiresAtLeastOneElement() | ||
->prototype('array') | ||
->children() | ||
->scalarNode('full_name') | ||
->isRequired(true) | ||
->end() | ||
->booleanNode('is_active') | ||
->defaultValue(true) | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
|
||
A few other things are shown here: the config value “user” contains multiple | ||
subnodes, of which the prototype is an array node. The children of these | ||
array nodes themselves will be a scalar node called “full_name” and a boolean | ||
node called “is_active”, of which the default value is true. | ||
An extra requirement is that at least one such user should be defined. | ||
|
||
Before normalization – then what? | ||
The final thing I want to show is how to change the overall structure of | ||
the config values, before they are validated. This would allow you to add | ||
shortcuts, or special ways of handling certain config structures (see the | ||
DoctrineBundle Configuration class for a beautiful example of this: the | ||
only connection defined will be moved to the “connections” section, before | ||
processing). In my example, I remove all the user definitions, when the | ||
value of “enabled” is false or is not set: | ||
|
||
.. code-block:: php | ||
|
||
$rootNode-> | ||
->beforeNormalization() | ||
->ifTrue(function($v) { | ||
// $v contains the raw configuration values | ||
return !isset($v['enabled']) || false === $v['enabled']; | ||
}) | ||
->then(function($v) { | ||
unset($v['users']); | ||
return $v; | ||
}) | ||
->end() | ||
->children() | ||
// ... | ||
->end() | ||
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ The Components | |
:hidden: | ||
|
||
class_loader | ||
config | ||
console | ||
css_selector | ||
dom_crawler | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a second entry that needs to be added (most people miss it) - it's in the
map.inc.rst
file in the components directory.