Skip to content

Commit fe76c97

Browse files
committed
minor symfony#13965 Use "closure-style" PHP configuration format in code examples (ocrampete16)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- Use "closure-style" PHP configuration format in code examples I noticed that we still use the "old" way of defining configuration in PHP in some code examples even though the new format has been available for quite a few years now: https://symfony.com/blog/new-in-symfony-3-4-php-based-configuration-for-services-and-routes Seeing two styles of writing configuration in PHP might be confusing for beginners, so I've updated the examples here to use the new format. There's probably still a number of places where the older format is used - I'll be sure to submit more PRs as I encouter them :) Commits ------- addf17b Use "closure-style" PHP configuration format in code examples
2 parents 170aa97 + addf17b commit fe76c97

File tree

1 file changed

+73
-46
lines changed

1 file changed

+73
-46
lines changed

configuration.rst

+73-46
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ 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');
129-
123+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
124+
125+
return static function (ContainerConfigurator $container) {
126+
$container->import('legacy_config.php');
127+
// ignore_errors (3rd parameter) silently discards errors if the loaded file doesn't exist
128+
$container->import('my_config_file.xml', null, true);
129+
// glob expressions are also supported to load multiple files
130+
$container->import('/etc/myapp/*.yaml');
131+
};
132+
130133
// ...
131134
132135
.. _config-parameter-intro:
@@ -209,24 +212,25 @@ reusable configuration value. By convention, parameters are defined under the
209212
.. code-block:: php
210213
211214
// 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', 'something@example.com');
215-
216-
// boolean parameters
217-
$container->setParameter('app.enable_v2_protocol', true);
218-
219-
// array/collection parameters
220-
$container->setParameter('app.supported_locales', ['en', 'es', 'fr']);
215+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
221216
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
226217
use App\Entity\BlogPost;
227218
228-
$container->setParameter('app.some_constant', GLOBAL_CONSTANT);
229-
$container->setParameter('app.another_constant', BlogPost::MAX_ITEMS);
219+
return static function (ContainerConfigurator $container) {
220+
$container->parameters()
221+
// the parameter name is an arbitrary string (the 'app.' prefix is recommended
222+
// to better differentiate your parameters from Symfony parameters).
223+
->set('app.admin_email', 'something@example.com')
224+
// boolean parameters
225+
->set('app.enable_v2_protocol', true)
226+
// array/collection parameters
227+
->set('app.supported_locales', ['en', 'es', 'fr'])
228+
// binary content parameters (use the PHP escape sequences)
229+
->set('app.some_parameter', 'This is a Bell char: \x07')
230+
// PHP constants as parameter values
231+
->set('app.some_constant', GLOBAL_CONSTANT)
232+
->set('app.another_constant', BlogPost::MAX_ITEMS);
233+
};
230234
231235
// ...
232236
@@ -278,12 +282,17 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
278282
.. code-block:: php
279283
280284
// 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%',
285+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
286+
287+
return static function (ContainerConfigurator $container) {
288+
$container->extension('some_package', [
289+
// any string surrounded by two % is replaced by that parameter value
290+
'email_address' => '%app.admin_email%',
291+
292+
// ...
293+
]);
294+
};
284295
285-
// ...
286-
]);
287296
288297
.. note::
289298

@@ -310,7 +319,12 @@ configuration file using a special syntax: wrap the parameter name in two ``%``
310319
.. code-block:: php
311320
312321
// config/services.php
313-
$container->setParameter('url_pattern', 'http://symfony.com/?foo=%%s&bar=%%d');
322+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
323+
324+
return static function (ContainerConfigurator $container) {
325+
$container->parameters()
326+
->set('url_pattern', 'http://symfony.com/?foo=%%s&bar=%%d');
327+
};
314328
315329
.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc
316330

@@ -478,12 +492,16 @@ This example shows how you could configure the database connection using an env
478492
.. code-block:: php
479493
480494
// 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-
]);
495+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
496+
497+
return static function (ContainerConfigurator $container) {
498+
$container->extension('doctrine', [
499+
'dbal' => [
500+
// by convention the env var names are always uppercase
501+
'url' => '%env(resolve:DATABASE_URL)%',
502+
]
503+
]);
504+
};
487505
488506
.. seealso::
489507

@@ -780,13 +798,18 @@ doesn't work for parameters:
780798
.. code-block:: php
781799
782800
// config/services.php
801+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
802+
783803
use App\Service\MessageGenerator;
784-
use Symfony\Component\DependencyInjection\Reference;
785804
786-
$container->setParameter('app.contents_dir', '...');
805+
return static function (ContainerConfigurator $container) {
806+
$container->parameters()
807+
->set('app.contents_dir', '...');
787808
788-
$container->getDefinition(MessageGenerator::class)
789-
->setArgument('$contentsDir', '%app.contents_dir%');
809+
$container->services()
810+
->get(MessageGenerator::class)
811+
->arg('$contentsDir', '%app.contents_dir%');
812+
};
790813
791814
If you inject the same parameters over and over again, use the
792815
``services._defaults.bind`` option instead. The arguments defined in that option are
@@ -832,18 +855,22 @@ whenever a service/controller defines a ``$projectDir`` argument, use this:
832855
.. code-block:: php
833856
834857
// config/services.php
858+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
859+
835860
use App\Controller\LuckyController;
836861
use Psr\Log\LoggerInterface;
837862
use Symfony\Component\DependencyInjection\Reference;
838863
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-
;
864+
return static function (ContainerConfigurator $container) {
865+
$container->services()
866+
->set(LuckyController::class)
867+
->public()
868+
->args([
869+
// pass this value to any $projectDir argument for any service
870+
// that's created in this file (including controller arguments)
871+
'$projectDir' => '%kernel.project_dir%',
872+
]);
873+
};
847874
848875
.. seealso::
849876

0 commit comments

Comments
 (0)