Skip to content

Commit 42e156e

Browse files
committed
New DI configuration syntax (PHP) instead of "legacy"
1 parent 7b3c6a1 commit 42e156e

File tree

5 files changed

+136
-71
lines changed

5 files changed

+136
-71
lines changed

controller/argument_value_resolver.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,17 @@ and adding a priority.
226226
.. code-block:: php
227227
228228
// config/services.php
229+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
230+
229231
use App\ArgumentResolver\UserValueResolver;
230232
231-
$container->autowire(UserValueResolver::class)
232-
->addTag('controller.argument_value_resolver', ['priority' => 50])
233-
;
233+
return static function (ContainerConfigurator $container) {
234+
$services = $configurator->services();
235+
236+
$services->set(UserValueResolver::class)
237+
->tag('controller.argument_value_resolver', ['priority' => 50])
238+
;
239+
};
234240
235241
While adding a priority is optional, it's recommended to add one to make sure
236242
the expected value is injected. The built-in ``RequestAttributeValueResolver``,

controller/upload_file.rst

+9-2
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,17 @@ Then, define a service for this class:
317317
.. code-block:: php
318318
319319
// config/services.php
320+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
321+
320322
use App\Service\FileUploader;
321323
322-
$container->autowire(FileUploader::class)
323-
->setArgument('$targetDirectory', '%brochures_directory%');
324+
return static function (ContainerConfigurator $container) {
325+
$services = $configurator->services();
326+
327+
$services->set(FileUploader::class)
328+
->arg('$targetDirectory', '%brochures_directory%')
329+
;
330+
};
324331
325332
Now you're ready to use this service in the controller::
326333

doctrine/events.rst

+58-34
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,28 @@ with the ``doctrine.event_listener`` tag:
198198
.. code-block:: php
199199
200200
// config/services.php
201+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
202+
201203
use App\EventListener\SearchIndexer;
202204
203-
// listeners are applied by default to all Doctrine connections
204-
$container->autowire(SearchIndexer::class)
205-
->addTag('doctrine.event_listener', [
206-
// this is the only required option for the lifecycle listener tag
207-
'event' => 'postPersist',
205+
return static function (ContainerConfigurator $container) {
206+
$services = $configurator->services();
207+
208+
// listeners are applied by default to all Doctrine connections
209+
$services->set(SearchIndexer::class)
210+
->tag('doctrine.event_listener', [
211+
// this is the only required option for the lifecycle listener tag
212+
'event' => 'postPersist',
208213
209-
// listeners can define their priority in case multiple listeners are associated
210-
// to the same event (default priority = 0; higher numbers = listener is run earlier)
211-
'priority' => 500,
214+
// listeners can define their priority in case multiple listeners are associated
215+
// to the same event (default priority = 0; higher numbers = listener is run earlier)
216+
'priority' => 500,
212217
213-
# you can also restrict listeners to a specific Doctrine connection
214-
'connection' => 'default',
215-
])
216-
;
218+
# you can also restrict listeners to a specific Doctrine connection
219+
'connection' => 'default',
220+
])
221+
;
222+
};
217223
218224
.. tip::
219225

@@ -314,29 +320,35 @@ with the ``doctrine.orm.entity_listener`` tag:
314320
.. code-block:: php
315321
316322
// config/services.php
323+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
324+
317325
use App\Entity\User;
318326
use App\EventListener\UserChangedNotifier;
319327
320-
$container->autowire(UserChangedNotifier::class)
321-
->addTag('doctrine.orm.entity_listener', [
322-
// These are the options required to define the entity listener:
323-
'event' => 'postUpdate',
324-
'entity' => User::class,
328+
return static function (ContainerConfigurator $container) {
329+
$services = $configurator->services();
330+
331+
$services->set(UserChangedNotifier::class)
332+
->tag('doctrine.orm.entity_listener', [
333+
// These are the options required to define the entity listener:
334+
'event' => 'postUpdate',
335+
'entity' => User::class,
325336
326-
// These are other options that you may define if needed:
337+
// These are other options that you may define if needed:
327338
328-
// set the 'lazy' option to TRUE to only instantiate listeners when they are used
329-
// 'lazy' => true,
339+
// set the 'lazy' option to TRUE to only instantiate listeners when they are used
340+
// 'lazy' => true,
330341
331-
// set the 'entity_manager' option if the listener is not associated to the default manager
332-
// 'entity_manager' => 'custom',
342+
// set the 'entity_manager' option if the listener is not associated to the default manager
343+
// 'entity_manager' => 'custom',
333344
334-
// by default, Symfony looks for a method called after the event (e.g. postUpdate())
335-
// if it doesn't exist, it tries to execute the '__invoke()' method, but you can
336-
// configure a custom method name with the 'method' option
337-
// 'method' => 'checkUserChanges',
338-
])
339-
;
345+
// by default, Symfony looks for a method called after the event (e.g. postUpdate())
346+
// if it doesn't exist, it tries to execute the '__invoke()' method, but you can
347+
// configure a custom method name with the 'method' option
348+
// 'method' => 'checkUserChanges',
349+
])
350+
;
351+
};
340352
341353
Doctrine Lifecycle Subscribers
342354
------------------------------
@@ -434,11 +446,17 @@ with the ``doctrine.event_subscriber`` tag:
434446
.. code-block:: php
435447
436448
// config/services.php
449+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
450+
437451
use App\EventListener\DatabaseActivitySubscriber;
438452
439-
$container->autowire(DatabaseActivitySubscriber::class)
440-
->addTag('doctrine.event_subscriber')
441-
;
453+
return static function (ContainerConfigurator $container) {
454+
$services = $configurator->services();
455+
456+
$services->set(DatabaseActivitySubscriber::class)
457+
->tag('doctrine.event_subscriber')
458+
;
459+
};
442460
443461
If you need to associate the subscriber with a specific Doctrine connection, you
444462
can do it in the service configuration:
@@ -473,11 +491,17 @@ can do it in the service configuration:
473491
.. code-block:: php
474492
475493
// config/services.php
494+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
495+
476496
use App\EventListener\DatabaseActivitySubscriber;
477497
478-
$container->autowire(DatabaseActivitySubscriber::class)
479-
->addTag('doctrine.event_subscriber', ['connection' => 'default'])
480-
;
498+
return static function (ContainerConfigurator $container) {
499+
$services = $configurator->services();
500+
501+
$services->set(DatabaseActivitySubscriber::class)
502+
->tag('doctrine.event_subscriber', ['connection' => 'default'])
503+
;
504+
};
481505
482506
.. tip::
483507

routing/custom_route_loader.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,17 @@ Now define a service for the ``ExtraLoader``:
327327
.. code-block:: php
328328
329329
// config/services.php
330+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
331+
330332
use App\Routing\ExtraLoader;
331333
332-
$container->autowire(ExtraLoader::class)
333-
->addTag('routing.loader')
334-
;
334+
return static function (ContainerConfigurator $container) {
335+
$services = $configurator->services();
336+
337+
$services->set(ExtraLoader::class)
338+
->tag('routing.loader')
339+
;
340+
};
335341
336342
Notice the tag ``routing.loader``. All services with this *tag* will be marked
337343
as potential route loaders and added as specialized route loaders to the

session/database.rst

+51-29
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,22 @@ first register a new handler service with your database credentials:
217217
.. code-block:: php
218218
219219
// config/services.php
220+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
221+
220222
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
221223
222-
$storageDefinition = $container->autowire(PdoSessionHandler::class)
223-
->setArguments([
224-
'%env(DATABASE_URL)%',
225-
// you can also use PDO configuration, but requires passing two arguments:
226-
// 'mysql:dbname=mydatabase; host=myhost; port=myport',
227-
// ['db_username' => 'myuser', 'db_password' => 'mypassword'],
228-
])
229-
;
224+
return static function (ContainerConfigurator $container) {
225+
$services = $configurator->services();
226+
227+
$services->set(PdoSessionHandler::class)
228+
->args([
229+
'%env(DATABASE_URL)%',
230+
// you can also use PDO configuration, but requires passing two arguments:
231+
// 'mysql:dbname=mydatabase; host=myhost; port=myport',
232+
// ['db_username' => 'myuser', 'db_password' => 'mypassword'],
233+
])
234+
;
235+
};
230236
231237
Next, use the :ref:`handler_id <config-framework-session-handler-id>`
232238
configuration option to tell Symfony to use this service as the session handler:
@@ -306,15 +312,20 @@ passed to the ``PdoSessionHandler`` service:
306312
.. code-block:: php
307313
308314
// config/services.php
315+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
316+
309317
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
310-
// ...
311318
312-
$container->autowire(PdoSessionHandler::class)
313-
->setArguments([
314-
'%env(DATABASE_URL)%',
315-
['db_table' => 'customer_session', 'db_id_col' => 'guid'],
316-
])
317-
;
319+
return static function (ContainerConfigurator $container) {
320+
$services = $configurator->services();
321+
322+
$services->set(PdoSessionHandler::class)
323+
->args([
324+
'%env(DATABASE_URL)%',
325+
['db_table' => 'customer_session', 'db_id_col' => 'guid'],
326+
])
327+
;
328+
};
318329
319330
These are parameters that you can configure:
320331

@@ -466,13 +477,19 @@ the MongoDB connection as argument:
466477
.. code-block:: php
467478
468479
// config/services.php
469-
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
480+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
470481
471-
$storageDefinition = $container->autowire(MongoDbSessionHandler::class)
472-
->setArguments([
473-
new Reference('doctrine_mongodb.odm.default_connection'),
474-
])
475-
;
482+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
483+
484+
return static function (ContainerConfigurator $container) {
485+
$services = $configurator->services();
486+
487+
$services->set(MongoDbSessionHandler::class)
488+
->args([
489+
service('doctrine_mongodb.odm.default_connection'),
490+
])
491+
;
492+
};
476493
477494
Next, use the :ref:`handler_id <config-framework-session-handler-id>`
478495
configuration option to tell Symfony to use this service as the session handler:
@@ -569,15 +586,20 @@ configure these values with the second argument passed to the
569586
.. code-block:: php
570587
571588
// config/services.php
572-
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
573-
// ...
589+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
574590
575-
$container->autowire(MongoDbSessionHandler::class)
576-
->setArguments([
577-
'...',
578-
['id_field' => '_guid', 'expiry_field' => 'eol'],
579-
])
580-
;
591+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
592+
593+
return static function (ContainerConfigurator $container) {
594+
$services = $configurator->services();
595+
596+
$services->set(MongoDbSessionHandler::class)
597+
->args([
598+
service('doctrine_mongodb.odm.default_connection'),
599+
['id_field' => '_guid', 'expiry_field' => 'eol'],,
600+
])
601+
;
602+
};
581603
582604
These are parameters that you can configure:
583605

0 commit comments

Comments
 (0)