-
-
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
Changes from 1 commit
243c0d1
ec13feb
c07c655
28c144a
a6bda5e
ad27a83
9710bb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
.. index:: | ||
single: Configuration | ||
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. this should be indented by 4 spaces 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. it should also be less generic (and imo, the cookbook should be moved to the "Configuration" section) |
||
|
||
Configuration Usages | ||
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. What about changing this to: Using Parameters in the Configuration |
||
==================== | ||
|
||
Parameters are common in configuration such as ``%kernel.root_dir%`` | ||
(which is used for passing a path) or ``%kernel.debug%`` (which is used for | ||
passing a boolean indicating if mode is debug). The syntax wrapping an | ||
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. Parameters, such as ``kernel.root_dir`` (pointing to a path) or ``kernel.debug``
(whether debug mode is enabled), are common in configuration. |
||
alias string with ``%`` conveys that this is a value defined as a parameter | ||
and will be evaluated somewhere down the line and used. Within Symfony | ||
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. By wrapping the parameter name within 2 ``%`` characters, you indicate
that this value should be replaced by the value of the parameter |
||
we can have parameters defined in a bundle and used only in that bundle and | ||
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. When using Symfony, you can have parameters defined [...] |
||
also parameters defined in a bundle and used in another. Usually they would | ||
have a namespace like ``demo_bundle.service_doer.local_parameter`` making | ||
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. namespace -> name |
||
explicit where this parameter comes from. If the parameter is global then | ||
it may not have a namespace, e.g. ``%some_global_option_here%``. | ||
|
||
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. create a new section "Basic Usage" |
||
We can have parameters being used inside ``parameters.yml``: | ||
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 can use parameters inside the ``parameters.yml`` file: |
||
|
||
.. configuration-block:: | ||
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. no need for the configuration block |
||
|
||
.. code-block:: yaml | ||
|
||
# app/config/parameters.yml | ||
parameters: | ||
payment_test_mode: %kernel.root_dir% | ||
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. as our highlighter uses Yaml 1.0, you should wrap it in double quotes. |
||
|
||
Inside ``config.yml`` and other configuration files building larger | ||
strings: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
|
||
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. remove this empty line |
||
# app/config/config.yml | ||
my_bundle: | ||
local: | ||
directory: %kernel.root_dir%/../web/media/image | ||
|
||
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. XML <?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
my-bundle="http://example.org/schema/dic/my_bundle">
<my-bundle:config>
<my-bundle:local directory="%kernel.root_dir%/../web/media/image" />
</my-bundle:config>
</container> PHP $container->loadFromExtension('my_bundle', array(
'local' => array(
'directory' => '%kernel.root_dir%/../web/media/image',
),
)); |
||
We can also use parameters in service configuration files: | ||
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. a bit useless imo, as this is already explained in the Service Container chapter, where parameters are introduced, and because config files are service config files... But, if you really want to keep this, you should use a form of "you", not "we". And you should add all other formats, doctypes and root elements below |
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: xml | ||
|
||
<parameters> | ||
<parameter id="some_service.class" class="\Acme\DemoBundle\Service\Doer"> | ||
</parameters> | ||
|
||
<services> | ||
<service id="%some_service%" class="%some_service.class%" /> | ||
</services> | ||
|
||
For more information on how parameters are used in Symfony please see | ||
:ref:`parameters <book-service-container-parameters>`. | ||
|
||
Besides these usages above we can use this syntax in routing files and handle | ||
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. we -> you |
||
parameters in special cases as discussed below. | ||
|
||
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. create a new section, called "Using Parameters in your Bundle Configuration" |
||
If for instance, there is a use case in which we want to use the | ||
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. we -> you |
||
``%kernel.debug%`` debug mode parameter to make our bundle adapt its | ||
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. our -> your |
||
configuration depending on this. For this case we cannot use | ||
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. we -> you |
||
the syntax directly and expect this to work. The configuration handling | ||
will just tread this ``%kernel.debug%`` as a string. Let's consider | ||
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. remove "let's" |
||
this example with ``AcmeDemoBundle``: | ||
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. the AcmeDemoBundle (without backticks) |
||
|
||
.. code-block:: php | ||
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. remove this line and the line above and use a double colon |
||
|
||
// Inside Configuration class | ||
->booleanNode('logging')->defaultValue('%kernel.debug%')->end() | ||
|
||
// Inside the Extension class | ||
$config = $this->processConfiguration($configuration, $configs); | ||
var_dump($config['logging']); | ||
|
||
Now let's examine the results to see this closely: | ||
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. again, remove "let's". And add a comma after "Now" |
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: xml | ||
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. it's yaml, not xml. |
||
|
||
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%``. | ||
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. do not use backticks in comments and put it in double quotes, to make it more clear it's a string and not the parameter. |
||
# Which is always considered as true. | ||
# Configurator class does not know anything about the default | ||
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. The Configurator does not know anything about "%kernel.debug%" being a parameter |
||
# string ``%kernel.debug%``. | ||
|
||
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. missing XML and PHP |
||
In order to support this use case ``Configuration`` class has to | ||
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. [...] use case, the |
||
be injected with this parameter via the extension as follows: | ||
|
||
.. code-block:: php | ||
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. use double colon and remove this |
||
|
||
<?php | ||
|
||
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. remove the above 2 lines |
||
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; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Boolean $debug Whether to use the debug mode | ||
*/ | ||
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. remove phpdocs, especially when they doesn't make more sense than the example. |
||
public function __construct($debug) | ||
{ | ||
$this->debug = (Boolean) $debug; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('acme_demo'); | ||
|
||
$rootNode | ||
// ... | ||
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. I think we should at least add $rootNode
->children()
// ...
->booleanNode('logging')->defaultValue($this->debug)->end()
// ...
->end()
; |
||
->booleanNode('logging')->defaultValue($this->debug)->end() | ||
// ... | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} | ||
|
||
And set it in the constructor of ``Configuration`` via the Extension class: | ||
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. Extension -> missing backticks |
||
|
||
.. code-block:: php | ||
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. remove this and use double colon |
||
|
||
<?php | ||
|
||
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. remove the above 2 lines |
||
namespace Acme\DemoBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
class AcmeDemoExtension extends AbstractDoctrineExtension | ||
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. why is there the DoctrineExtension here ? You should extend from the base class in HttpKernel |
||
{ | ||
// ... | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConfiguration(array $config, ContainerBuilder $container) | ||
{ | ||
return new Configuration($container->getParameter('kernel.debug')); | ||
} | ||
} | ||
|
||
There are some instances of ``%kernel.debug%`` usage within a ``Configurator`` | ||
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. maybe put this in a sidebar: .. sidebar:: Setting the Default in the Extension
There are some [...] |
||
class in ``TwigBundle`` and ``AsseticBundle``, however this is because they are | ||
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. remove backticks |
||
setting the parameter value in the container via the Extension class. For | ||
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. [...] is because the default parameter value is set by the Extension class. |
||
example in ``AsseticBundle`` we have: | ||
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. AsseticBundle (without backticks), you can find:: (double colon) |
||
|
||
.. code-block:: php | ||
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. remove this |
||
|
||
$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 | ||
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. remove backticks |
||
anymore ``%kernel.debug%`` but rather the new ``%assetic.debug%`` parameter. |
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.
put the hole
:doc:
role on a new lineThere 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.
and this should be placed in a
.. seealso::
directive