Skip to content

Commit b1d1e36

Browse files
authored
Use "closure-style" PHP configuration format in code examples
1 parent 89382cf commit b1d1e36

File tree

1 file changed

+78
-44
lines changed

1 file changed

+78
-44
lines changed

configuration.rst

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ configuration files, even if they use a different format:
120120
.. code-block:: php
121121
122122
// config/services.php
123-
$loader->import('legacy_config.xml');
124-
// the third optional argument of import() is 'ignore_errors', which
125-
// silently discards errors if the loaded file doesn't exist
126-
$loader->import('my_config_file.yaml', null, true);
127-
// glob expressions are also supported to load multiple files
128-
$loader->import('/etc/myapp/*.yaml');
129123
124+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
125+
126+
return static function (ContainerConfigurator $container) {
127+
$container->import('legacy_config.php');
128+
// ignore_errors (3rd parameter) silently discards errors if the loaded file doesn't exist
129+
$container->import('my_config_file.xml', null, true);
130+
// glob expressions are also supported to load multiple files
131+
$container->import('/etc/myapp/*.yaml');
132+
};
133+
130134
// ...
131135
132136
.. _config-parameter-intro:
@@ -209,24 +213,26 @@ reusable configuration value. By convention, parameters are defined under the
209213
.. code-block:: php
210214
211215
// config/services.php
212-
// the parameter name is an arbitrary string (the 'app.' prefix is recommended
213-
// to better differentiate your parameters from Symfony parameters).
214-
$container->setParameter('app.admin_email', '[email protected]');
215-
216-
// boolean parameters
217-
$container->setParameter('app.enable_v2_protocol', true);
218216
219-
// array/collection parameters
220-
$container->setParameter('app.supported_locales', ['en', 'es', 'fr']);
217+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
221218
222-
// binary content parameters (use the PHP escape sequences)
223-
$container->setParameter('app.some_parameter', 'This is a Bell char: \x07');
224-
225-
// PHP constants as parameter values
226219
use App\Entity\BlogPost;
227220
228-
$container->setParameter('app.some_constant', GLOBAL_CONSTANT);
229-
$container->setParameter('app.another_constant', BlogPost::MAX_ITEMS);
221+
return static function (ContainerConfigurator $container) {
222+
$container->parameters()
223+
// the parameter name is an arbitrary string (the 'app.' prefix is recommended
224+
// to better differentiate your parameters from Symfony parameters).
225+
->set('app.admin_email', '[email protected]')
226+
// boolean parameters
227+
->set('app.enable_v2_protocol', true)
228+
// array/collection parameters
229+
->set('app.supported_locales', ['en', 'es', 'fr'])
230+
// binary content parameters (use the PHP escape sequences)
231+
->set('app.some_parameter', 'This is a Bell char: \x07')
232+
// PHP constants as parameter values
233+
->set('app.some_constant', GLOBAL_CONSTANT)
234+
->set('app.another_constant', BlogPost::MAX_ITEMS);
235+
};
230236
231237
// ...
232238
@@ -278,12 +284,18 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
278284
.. code-block:: php
279285
280286
// config/packages/some_package.php
281-
$container->loadFromExtension('some_package', [
282-
// any string surrounded by two % is replaced by that parameter value
283-
'email_address' => '%app.admin_email%',
284287
285-
// ...
286-
]);
288+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
289+
290+
return static function (ContainerConfigurator $container) {
291+
$container->extension('some_package', [
292+
// any string surrounded by two % is replaced by that parameter value
293+
'email_address' => '%app.admin_email%',
294+
295+
// ...
296+
]);
297+
};
298+
287299
288300
.. note::
289301

@@ -310,7 +322,13 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
310322
.. code-block:: php
311323
312324
// config/services.php
313-
$container->setParameter('url_pattern', 'http://symfony.com/?foo=%%s&bar=%%d');
325+
326+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
327+
328+
return static function (ContainerConfigurator $container) {
329+
$container->parameters()
330+
->set('url_pattern', 'http://symfony.com/?foo=%%s&bar=%%d');
331+
};
314332
315333
.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc
316334

@@ -478,12 +496,17 @@ This example shows how you could configure the database connection using an env
478496
.. code-block:: php
479497
480498
// config/packages/doctrine.php
481-
$container->loadFromExtension('doctrine', [
482-
'dbal' => [
483-
// by convention the env var names are always uppercase
484-
'url' => '%env(resolve:DATABASE_URL)%',
485-
]
486-
]);
499+
500+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
501+
502+
return static function (ContainerConfigurator $container) {
503+
$container->extension('doctrine', [
504+
'dbal' => [
505+
// by convention the env var names are always uppercase
506+
'url' => '%env(resolve:DATABASE_URL)%',
507+
]
508+
]);
509+
};
487510
488511
.. seealso::
489512

@@ -780,13 +803,19 @@ doesn't work for parameters:
780803
.. code-block:: php
781804
782805
// config/services.php
806+
807+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
808+
783809
use App\Service\MessageGenerator;
784-
use Symfony\Component\DependencyInjection\Reference;
785810
786-
$container->setParameter('app.contents_dir', '...');
811+
return static function (ContainerConfigurator $container) {
812+
$container->parameters()
813+
->set('app.contents_dir', '...');
787814
788-
$container->getDefinition(MessageGenerator::class)
789-
->setArgument('$contentsDir', '%app.contents_dir%');
815+
$container->services()
816+
->get(MessageGenerator::class)
817+
->arg('$contentsDir', '%app.contents_dir%');
818+
};
790819
791820
If you inject the same parameters over and over again, use the
792821
``services._defaults.bind`` option instead. The arguments defined in that option are
@@ -832,18 +861,23 @@ whenever a service/controller defines a ``$projectDir`` argument, use this:
832861
.. code-block:: php
833862
834863
// config/services.php
864+
865+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
866+
835867
use App\Controller\LuckyController;
836868
use Psr\Log\LoggerInterface;
837869
use Symfony\Component\DependencyInjection\Reference;
838870
839-
$container->register(LuckyController::class)
840-
->setPublic(true)
841-
->setBindings([
842-
// pass this value to any $projectDir argument for any service
843-
// that's created in this file (including controller arguments)
844-
'$projectDir' => '%kernel.project_dir%',
845-
])
846-
;
871+
return static function (ContainerConfigurator $container) {
872+
$container->services()
873+
->set(LuckyController::class)
874+
->public()
875+
->args([
876+
// pass this value to any $projectDir argument for any service
877+
// that's created in this file (including controller arguments)
878+
'$projectDir' => '%kernel.project_dir%',
879+
]);
880+
};
847881
848882
.. seealso::
849883

0 commit comments

Comments
 (0)