Skip to content

New DI configuration syntax (PHP) instead of "legacy" #14279

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
merged 1 commit into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,17 @@ and adding a priority.
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\ArgumentResolver\UserValueResolver;

$container->autowire(UserValueResolver::class)
->addTag('controller.argument_value_resolver', ['priority' => 50])
;
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(UserValueResolver::class)
->tag('controller.argument_value_resolver', ['priority' => 50])
;
};

While adding a priority is optional, it's recommended to add one to make sure
the expected value is injected. The built-in ``RequestAttributeValueResolver``,
Expand Down
11 changes: 9 additions & 2 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,17 @@ Then, define a service for this class:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\Service\FileUploader;

$container->autowire(FileUploader::class)
->setArgument('$targetDirectory', '%brochures_directory%');
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(FileUploader::class)
->arg('$targetDirectory', '%brochures_directory%')
;
};

Now you're ready to use this service in the controller::

Expand Down
92 changes: 58 additions & 34 deletions doctrine/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,28 @@ with the ``doctrine.event_listener`` tag:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\EventListener\SearchIndexer;

// listeners are applied by default to all Doctrine connections
$container->autowire(SearchIndexer::class)
->addTag('doctrine.event_listener', [
// this is the only required option for the lifecycle listener tag
'event' => 'postPersist',
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

// listeners are applied by default to all Doctrine connections
$services->set(SearchIndexer::class)
->tag('doctrine.event_listener', [
// this is the only required option for the lifecycle listener tag
'event' => 'postPersist',

// listeners can define their priority in case multiple listeners are associated
// to the same event (default priority = 0; higher numbers = listener is run earlier)
'priority' => 500,
// listeners can define their priority in case multiple listeners are associated
// to the same event (default priority = 0; higher numbers = listener is run earlier)
'priority' => 500,

# you can also restrict listeners to a specific Doctrine connection
'connection' => 'default',
])
;
# you can also restrict listeners to a specific Doctrine connection
'connection' => 'default',
])
;
};

.. tip::

Expand Down Expand Up @@ -314,29 +320,35 @@ with the ``doctrine.orm.entity_listener`` tag:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\Entity\User;
use App\EventListener\UserChangedNotifier;

$container->autowire(UserChangedNotifier::class)
->addTag('doctrine.orm.entity_listener', [
// These are the options required to define the entity listener:
'event' => 'postUpdate',
'entity' => User::class,
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(UserChangedNotifier::class)
->tag('doctrine.orm.entity_listener', [
// These are the options required to define the entity listener:
'event' => 'postUpdate',
'entity' => User::class,

// These are other options that you may define if needed:
// These are other options that you may define if needed:

// set the 'lazy' option to TRUE to only instantiate listeners when they are used
// 'lazy' => true,
// set the 'lazy' option to TRUE to only instantiate listeners when they are used
// 'lazy' => true,

// set the 'entity_manager' option if the listener is not associated to the default manager
// 'entity_manager' => 'custom',
// set the 'entity_manager' option if the listener is not associated to the default manager
// 'entity_manager' => 'custom',

// by default, Symfony looks for a method called after the event (e.g. postUpdate())
// if it doesn't exist, it tries to execute the '__invoke()' method, but you can
// configure a custom method name with the 'method' option
// 'method' => 'checkUserChanges',
])
;
// by default, Symfony looks for a method called after the event (e.g. postUpdate())
// if it doesn't exist, it tries to execute the '__invoke()' method, but you can
// configure a custom method name with the 'method' option
// 'method' => 'checkUserChanges',
])
;
};

Doctrine Lifecycle Subscribers
------------------------------
Expand Down Expand Up @@ -434,11 +446,17 @@ with the ``doctrine.event_subscriber`` tag:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\EventListener\DatabaseActivitySubscriber;

$container->autowire(DatabaseActivitySubscriber::class)
->addTag('doctrine.event_subscriber')
;
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(DatabaseActivitySubscriber::class)
->tag('doctrine.event_subscriber')
;
};

If you need to associate the subscriber with a specific Doctrine connection, you
can do it in the service configuration:
Expand Down Expand Up @@ -473,11 +491,17 @@ can do it in the service configuration:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\EventListener\DatabaseActivitySubscriber;

$container->autowire(DatabaseActivitySubscriber::class)
->addTag('doctrine.event_subscriber', ['connection' => 'default'])
;
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(DatabaseActivitySubscriber::class)
->tag('doctrine.event_subscriber', ['connection' => 'default'])
;
};

.. tip::

Expand Down
12 changes: 9 additions & 3 deletions routing/custom_route_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,17 @@ Now define a service for the ``ExtraLoader``:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\Routing\ExtraLoader;

$container->autowire(ExtraLoader::class)
->addTag('routing.loader')
;
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(ExtraLoader::class)
->tag('routing.loader')
;
};

Notice the tag ``routing.loader``. All services with this *tag* will be marked
as potential route loaders and added as specialized route loaders to the
Expand Down
80 changes: 51 additions & 29 deletions session/database.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,22 @@ first register a new handler service with your database credentials:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;

$storageDefinition = $container->autowire(PdoSessionHandler::class)
->setArguments([
'%env(DATABASE_URL)%',
// you can also use PDO configuration, but requires passing two arguments:
// 'mysql:dbname=mydatabase; host=myhost; port=myport',
// ['db_username' => 'myuser', 'db_password' => 'mypassword'],
])
;
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(PdoSessionHandler::class)
->args([
'%env(DATABASE_URL)%',
// you can also use PDO configuration, but requires passing two arguments:
// 'mysql:dbname=mydatabase; host=myhost; port=myport',
// ['db_username' => 'myuser', 'db_password' => 'mypassword'],
])
;
};

Next, use the :ref:`handler_id <config-framework-session-handler-id>`
configuration option to tell Symfony to use this service as the session handler:
Expand Down Expand Up @@ -306,15 +312,20 @@ passed to the ``PdoSessionHandler`` service:
.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
// ...

$container->autowire(PdoSessionHandler::class)
->setArguments([
'%env(DATABASE_URL)%',
['db_table' => 'customer_session', 'db_id_col' => 'guid'],
])
;
return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(PdoSessionHandler::class)
->args([
'%env(DATABASE_URL)%',
['db_table' => 'customer_session', 'db_id_col' => 'guid'],
])
;
};

These are parameters that you can configure:

Expand Down Expand Up @@ -466,13 +477,19 @@ the MongoDB connection as argument:
.. code-block:: php

// config/services.php
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

$storageDefinition = $container->autowire(MongoDbSessionHandler::class)
->setArguments([
new Reference('doctrine_mongodb.odm.default_connection'),
])
;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;

return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(MongoDbSessionHandler::class)
->args([
service('doctrine_mongodb.odm.default_connection'),
])
;
};

Next, use the :ref:`handler_id <config-framework-session-handler-id>`
configuration option to tell Symfony to use this service as the session handler:
Expand Down Expand Up @@ -569,15 +586,20 @@ configure these values with the second argument passed to the
.. code-block:: php

// config/services.php
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;
// ...
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

$container->autowire(MongoDbSessionHandler::class)
->setArguments([
'...',
['id_field' => '_guid', 'expiry_field' => 'eol'],
])
;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;

return static function (ContainerConfigurator $container) {
$services = $configurator->services();

$services->set(MongoDbSessionHandler::class)
->args([
service('doctrine_mongodb.odm.default_connection'),
['id_field' => '_guid', 'expiry_field' => 'eol'],,
])
;
};

These are parameters that you can configure:

Expand Down