-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Cookbook][Configuration] add configuration cookbook handlig parameters in Configurator class #3420
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
Merged
weaverryan
merged 7 commits into
symfony:2.3
from
cordoval:3219-add-note-about-default-values-as-parameters-in-configuration-component
Mar 18, 2014
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
243c0d1
create configuration cookbook with ways of handling paramters in Conf…
cordoval ec13feb
plug changes according to comments
cordoval c07c655
fix see also block
cordoval 28c144a
plug xml and php version of the configuration
cordoval a6bda5e
address weaverryan comments
cordoval ad27a83
address comments by xabbuh
cordoval 9710bb5
final touches
cordoval 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
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
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
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
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
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
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
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
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,159 @@ | ||
.. index:: | ||
single: Using Parameters within a Dependency Injection Class | ||
|
||
Using Parameters within a Dependency Injection Class | ||
---------------------------------------------------- | ||
|
||
You have seen how to use configuration parameters within | ||
:ref:`Symfony service containers <book-service-container-parameters>`. | ||
There are special cases such as when you want, for instance, to use the | ||
``%kernel.debug%`` parameter to make the services in your bundle enter | ||
debug mode. For this case there is more work to do in order | ||
to make the system understand the parameter value. By default | ||
your parameter ``%kernel.debug%`` will be treated as a | ||
simple string. Consider this example with the AcmeDemoBundle:: | ||
|
||
// Inside Configuration class | ||
$rootNode | ||
->children() | ||
->booleanNode('logging')->defaultValue('%kernel.debug%')->end() | ||
// ... | ||
->end() | ||
; | ||
|
||
// Inside the Extension class | ||
$config = $this->processConfiguration($configuration, $configs); | ||
var_dump($config['logging']); | ||
|
||
Now, examine the results to see this closely: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
my_bundle: | ||
logging: true | ||
# true, as expected | ||
|
||
my_bundle: | ||
logging: %kernel.debug% | ||
# true/false (depends on 2nd parameter of AppKernel), | ||
# as expected, because %kernel.debug% inside configuration | ||
# gets evaluated before being passed to the extension | ||
|
||
my_bundle: ~ | ||
# passes the string "%kernel.debug%". | ||
# Which is always considered as true. | ||
# The Configurator does not know anything about | ||
# "%kernel.debug%" being a parameter. | ||
|
||
.. code-block:: xml | ||
|
||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:my-bundle="http://example.org/schema/dic/my_bundle"> | ||
|
||
<my-bundle:config logging="true" /> | ||
<!-- true, as expected --> | ||
|
||
<my-bundle:config logging="%kernel.debug%" /> | ||
<!-- true/false (depends on 2nd parameter of AppKernel), | ||
as expected, because %kernel.debug% inside configuration | ||
gets evaluated before being passed to the extension --> | ||
|
||
<my-bundle:config /> | ||
<!-- passes the string "%kernel.debug%". | ||
Which is always considered as true. | ||
The Configurator does not know anything about | ||
"%kernel.debug%" being a parameter. --> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
$container->loadFromExtension('my_bundle', array( | ||
'logging' => true, | ||
// true, as expected | ||
) | ||
); | ||
|
||
$container->loadFromExtension('my_bundle', array( | ||
'logging' => "%kernel.debug%", | ||
// true/false (depends on 2nd parameter of AppKernel), | ||
// as expected, because %kernel.debug% inside configuration | ||
// gets evaluated before being passed to the extension | ||
) | ||
); | ||
|
||
$container->loadFromExtension('my_bundle'); | ||
// passes the string "%kernel.debug%". | ||
// Which is always considered as true. | ||
// The Configurator does not know anything about | ||
// "%kernel.debug%" being a parameter. | ||
|
||
In order to support this use case, the ``Configuration`` class has to | ||
be injected with this parameter via the extension as follows:: | ||
|
||
namespace Acme\DemoBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
private $debug; | ||
|
||
public function __construct($debug) | ||
{ | ||
$this->debug = (Boolean) $debug; | ||
} | ||
|
||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('acme_demo'); | ||
|
||
$rootNode | ||
->children() | ||
// ... | ||
->booleanNode('logging')->defaultValue($this->debug)->end() | ||
// ... | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} | ||
|
||
And set it in the constructor of ``Configuration`` via the ``Extension`` class:: | ||
|
||
namespace Acme\DemoBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
class AcmeDemoExtension extends Extension | ||
{ | ||
// ... | ||
|
||
public function getConfiguration(array $config, ContainerBuilder $container) | ||
{ | ||
return new Configuration($container->getParameter('kernel.debug')); | ||
} | ||
} | ||
|
||
.. sidebar:: Setting the Default in the Extension | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to indent the following lines by four spaces to make them part of the sidebar. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
There are some instances of ``%kernel.debug%`` usage within a ``Configurator`` | ||
class in TwigBundle and AsseticBundle, however this is because the default | ||
parameter value is set by the Extension class. For example in AsseticBundle, | ||
you can find:: | ||
|
||
$container->setParameter('assetic.debug', $config['debug']); | ||
|
||
The string ``%kernel.debug%`` passed here as an argument handles the | ||
interpreting job to the container which in turn does the evaluation. | ||
Both ways accomplish similar goals. AsseticBundle will not use | ||
anymore ``%kernel.debug%`` but rather the new ``%assetic.debug%`` parameter. |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
.. index:: | ||
single: Web Server | ||
single: Web Server | ||
|
||
Configuring a web server | ||
======================== | ||
|
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
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.
this is no longer true, is it?