Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit243c0d1

Browse files
committed
create configuration cookbook with ways of handling paramters in Configurator class
1 parent0428c57 commit243c0d1

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

‎cookbook/bundles/extension.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ The second method has several specific advantages:
8989
supported configuration settings for which backward compatibility will
9090
be maintained.
9191

92+
For other usages of the parameter ``%`` syntax see:doc:`Configuration</cookbook/configuration>`.
93+
9294
..index::
9395
single: Bundle; Extension
9496
single: DependencyInjection; Extension

‎cookbook/configuration.rst

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
..index::
2+
single: Configuration
3+
4+
Configuration Usages
5+
====================
6+
7+
Parameters are common in configuration such as ``%kernel.root_dir%``
8+
(which is used for passing a path) or ``%kernel.debug%`` (which is used for
9+
passing a boolean indicating if mode is debug). The syntax wrapping an
10+
alias string with ``%`` conveys that this is a value defined as a parameter
11+
and will be evaluated somewhere down the line and used. Within Symfony
12+
we can have parameters defined in a bundle and used only in that bundle and
13+
also parameters defined in a bundle and used in another. Usually they would
14+
have a namespace like ``demo_bundle.service_doer.local_parameter`` making
15+
explicit where this parameter comes from. If the parameter is global then
16+
it may not have a namespace, e.g. ``%some_global_option_here%``.
17+
18+
We can have parameters being used inside ``parameters.yml``:
19+
20+
..configuration-block::
21+
22+
..code-block::yaml
23+
24+
# app/config/parameters.yml
25+
parameters:
26+
payment_test_mode:%kernel.root_dir%
27+
28+
Inside ``config.yml`` and other configuration files building larger
29+
strings:
30+
31+
..configuration-block::
32+
33+
..code-block::yaml
34+
35+
36+
# app/config/config.yml
37+
my_bundle:
38+
local:
39+
directory:%kernel.root_dir%/../web/media/image
40+
41+
We can also use parameters in service configuration files:
42+
43+
..configuration-block::
44+
45+
..code-block::xml
46+
47+
<parameters>
48+
<parameterid="some_service.class"class="\Acme\DemoBundle\Service\Doer">
49+
</parameters>
50+
51+
<services>
52+
<serviceid="%some_service%"class="%some_service.class%" />
53+
</services>
54+
55+
For more information on how parameters are used in Symfony please see
56+
:ref:`parameters<book-service-container-parameters>`.
57+
58+
Besides these usages above we can use this syntax in routing files and handle
59+
parameters in special cases as discussed below.
60+
61+
If for instance, there is a use case in which we want to use the
62+
``%kernel.debug%`` debug mode parameter to make our bundle adapt its
63+
configuration depending on this. For this case we cannot use
64+
the syntax directly and expect this to work. The configuration handling
65+
will just tread this ``%kernel.debug%`` as a string. Let's consider
66+
this example with ``AcmeDemoBundle``:
67+
68+
..code-block::php
69+
70+
// Inside Configuration class
71+
->booleanNode('logging')->defaultValue('%kernel.debug%')->end()
72+
73+
// Inside the Extension class
74+
$config = $this->processConfiguration($configuration, $configs);
75+
var_dump($config['logging']);
76+
77+
Now let's examine the results to see this closely:
78+
79+
..configuration-block::
80+
81+
..code-block::xml
82+
83+
my_bundle:
84+
logging: true
85+
# true, as expected
86+
87+
my_bundle:
88+
logging: %kernel.debug%
89+
# true/false (depends on 2nd parameter of AppKernel),
90+
# as expected, because %kernel.debug% inside configuration
91+
# gets evaluated before being passed to the extension
92+
93+
my_bundle: ~
94+
# passes the string ``%kernel.debug%``.
95+
# Which is always considered as true.
96+
# Configurator class does not know anything about the default
97+
# string ``%kernel.debug%``.
98+
99+
In order to support this use case ``Configuration`` class has to
100+
be injected with this parameter via the extension as follows:
101+
102+
..code-block::php
103+
104+
<?php
105+
106+
namespace Acme\DemoBundle\DependencyInjection;
107+
108+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
109+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
110+
use Symfony\Component\Config\Definition\ConfigurationInterface;
111+
112+
class Configuration implements ConfigurationInterface
113+
{
114+
private $debug;
115+
116+
/**
117+
* Constructor
118+
*
119+
* @param Boolean $debug Whether to use the debug mode
120+
*/
121+
public function __construct($debug)
122+
{
123+
$this->debug = (Boolean) $debug;
124+
}
125+
126+
/**
127+
* {@inheritDoc}
128+
*/
129+
public function getConfigTreeBuilder()
130+
{
131+
$treeBuilder = new TreeBuilder();
132+
$rootNode = $treeBuilder->root('acme_demo');
133+
134+
$rootNode
135+
// ...
136+
->booleanNode('logging')->defaultValue($this->debug)->end()
137+
// ...
138+
;
139+
140+
return $treeBuilder;
141+
}
142+
}
143+
144+
And set it in the constructor of ``Configuration`` via the Extension class:
145+
146+
..code-block::php
147+
148+
<?php
149+
150+
namespace Acme\DemoBundle\DependencyInjection;
151+
152+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
153+
use Symfony\Component\DependencyInjection\ContainerBuilder;
154+
use Symfony\Component\DependencyInjection\Definition;
155+
use Symfony\Component\Config\FileLocator;
156+
157+
class AcmeDemoExtension extends AbstractDoctrineExtension
158+
{
159+
// ...
160+
161+
/**
162+
* {@inheritDoc}
163+
*/
164+
public function getConfiguration(array $config, ContainerBuilder $container)
165+
{
166+
return new Configuration($container->getParameter('kernel.debug'));
167+
}
168+
}
169+
170+
There are some instances of ``%kernel.debug%`` usage within a ``Configurator``
171+
class in ``TwigBundle`` and ``AsseticBundle``, however this is because they are
172+
setting the parameter value in the container via the Extension class. For
173+
example in ``AsseticBundle`` we have:
174+
175+
..code-block::php
176+
177+
$container->setParameter('assetic.debug', $config['debug']);
178+
179+
The string ``%kernel.debug%`` passed here as an argument handles the
180+
interpreting job to the container which in turn does the evaluation.
181+
Both ways accomplish similar goals. ``AsseticBundle`` will not use
182+
anymore ``%kernel.debug%`` but rather the new ``%assetic.debug%`` parameter.

‎cookbook/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The Cookbook
77
assetic/index
88
bundles/index
99
cache/index
10+
configuration
1011
configuration/index
1112
console/index
1213
controller/index

‎cookbook/routing/service_container_parameters.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,5 @@ path):
120120
However, as the ``%`` characters included in any URL are automatically encoded,
121121
the resulting URL of this example would be ``/score-50%25`` (``%25`` is the
122122
result of encoding the ``%`` character).
123+
124+
For other usages of the parameter ``%`` syntax see:doc:`Configuration</cookbook/configuration>`.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp