Skip to content

[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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cookbook/bundles/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ The second method has several specific advantages:
supported configuration settings for which backward compatibility will
be maintained.

For other usages of the parameter ``%`` syntax see :doc:`Configuration </cookbook/configuration>`.
Copy link
Member

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 line

Copy link
Member

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


.. index::
single: Bundle; Extension
single: DependencyInjection; Extension
Expand Down
182 changes: 182 additions & 0 deletions cookbook/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
.. index::
single: Configuration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be indented by 4 spaces

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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%``.

Copy link
Member

Choose a reason for hiding this comment

The 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``:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use parameters inside the ``parameters.yml`` file:


.. configuration-block::
Copy link
Member

Choose a reason for hiding this comment

The 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%
Copy link
Member

Choose a reason for hiding this comment

The 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


Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we -> you

parameters in special cases as discussed below.

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our -> your

configuration depending on this. For this case we cannot use
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove "let's"

this example with ``AcmeDemoBundle``:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the AcmeDemoBundle (without backticks)


.. code-block:: php
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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%``.
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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%``.

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] use case, the Configuration class has [...]

be injected with this parameter via the extension as follows:

.. code-block:: php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use double colon and remove this


<?php

Copy link
Member

Choose a reason for hiding this comment

The 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
*/
Copy link
Member

Choose a reason for hiding this comment

The 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
// ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should at least add ->children(), so that this code actually works:

$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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extension -> missing backticks


.. code-block:: php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this and use double colon


<?php

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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``
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsseticBundle (without backticks), you can find:: (double colon)


.. code-block:: php
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove backticks

anymore ``%kernel.debug%`` but rather the new ``%assetic.debug%`` parameter.
1 change: 1 addition & 0 deletions cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The Cookbook
assetic/index
bundles/index
cache/index
configuration
configuration/index
console/index
controller/index
Expand Down
2 changes: 2 additions & 0 deletions cookbook/routing/service_container_parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,5 @@ path):
However, as the ``%`` characters included in any URL are automatically encoded,
the resulting URL of this example would be ``/score-50%25`` (``%25`` is the
result of encoding the ``%`` character).

For other usages of the parameter ``%`` syntax see :doc:`Configuration </cookbook/configuration>`.