Skip to content

Replace calls to setDefinition() by register() #7861

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
May 4, 2017
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
7 changes: 2 additions & 5 deletions _includes/service_container/_my_mailer.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@

// app/config/services.php
use AppBundle\Mailer;
use Symfony\Component\DependencyInjection\Definition;

$container->setDefinition('app.mailer', new Definition(
Mailer::class,
array('sendmail')
));
$container->register('app.mailer', Mailer::class)
->addArgument('sendmail');
27 changes: 11 additions & 16 deletions components/event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ answer.
Consider the real-world example where you want to provide a plugin system
for your project. A plugin should be able to add methods, or do something
before or after a method is executed, without interfering with other plugins.
This is not an easy problem to solve with single inheritance, and even if
This is not an easy problem to solve with single inheritance, and even if
multiple inheritance was possible with PHP, it comes with its own drawbacks.

The Symfony EventDispatcher component implements the `Mediator`_ pattern
Expand Down Expand Up @@ -198,7 +198,6 @@ determine which instance is passed.
to tag services as event listeners::

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
Expand All @@ -208,23 +207,19 @@ determine which instance is passed.
$containerBuilder->addCompilerPass(new RegisterListenersPass());

// register the event dispatcher service
$containerBuilder->setDefinition('event_dispatcher', new Definition(
ContainerAwareEventDispatcher::class,
array(new Reference('service_container'))
));
$containerBuilder->register('event_dispatcher', ContainerAwareEventDispatcher::class)
->addArgument(new Reference('service_container'));

// register your event listener service
$listener = new Definition(\AcmeListener::class);
$listener->addTag('kernel.event_listener', array(
'event' => 'acme.foo.action',
'method' => 'onFooAction',
));
$containerBuilder->setDefinition('listener_service_id', $listener);
$containerBuilder->register('listener_service_id', \AcmeListener::class)
->addTag('kernel.event_listener', array(
'event' => 'acme.foo.action',
'method' => 'onFooAction',
));

// register an event subscriber
$subscriber = new Definition(\AcmeSubscriber::class);
$subscriber->addTag('kernel.event_subscriber');
$containerBuilder->setDefinition('subscriber_service_id', $subscriber);
$containerBuilder->register('subscriber_service_id', \AcmeSubscriber::class)
->addTag('kernel.event_subscriber');

By default, the listeners pass assumes that the event dispatcher's service
id is ``event_dispatcher``, that event listeners are tagged with the
Expand Down Expand Up @@ -441,7 +436,7 @@ EventDispatcher Aware Events and Listeners
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The ``EventDispatcher`` always passes the dispatched event, the event's
name and a reference to itself to the listeners. This can lead to some advanced
name and a reference to itself to the listeners. This can lead to some advanced
applications of the ``EventDispatcher`` including dispatching other events inside
listeners, chaining events or even lazy loading listeners into the dispatcher object.

Expand Down
38 changes: 12 additions & 26 deletions console/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,14 @@ First configure a listener for console exception events in the service container

// app/config/services.php
use AppBundle\EventListener\ConsoleExceptionListener;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$definitionConsoleExceptionListener = new Definition(
ConsoleExceptionListener::class,
array(new Reference('logger'))
);
$definitionConsoleExceptionListener->addTag(
'kernel.event_listener',
array('event' => 'console.exception')
);
$container->setDefinition(
'app.listener.command_exception',
$definitionConsoleExceptionListener
);
$container->register('app.listener.command_exception', ConsoleExceptionListener::class)
->addArgument(new Reference('logger'))
->addTag(
'kernel.event_listener',
array('event' => 'console.exception')
);

Then implement the actual listener::

Expand Down Expand Up @@ -211,21 +204,14 @@ First configure a listener for console terminate events in the service container

// app/config/services.php
use AppBundle\EventListener\ErrorLoggerListener;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$definitionErrorLoggerListener = new Definition(
ErrorLoggerListener::class,
array(new Reference('logger'))
);
$definitionErrorLoggerListener->addTag(
'kernel.event_listener',
array('event' => 'console.terminate')
);
$container->setDefinition(
'app.listener.command_error',
$definitionErrorLoggerListener
);
$container->register('app.listener.command_error', ErrorLoggerListener::class)
->addArgument(new Reference('logger'))
->addTag(
'kernel.event_listener',
array('event' => 'console.terminate')
);

Then implement the actual listener::

Expand Down
11 changes: 5 additions & 6 deletions controller/error_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,12 @@ In that case, you might want to override one or both of the ``showAction()`` and
// app/config/services.php
use AppBundle\Controller\CustomExceptionController;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition(CustomExceptionController::class, array(
new Reference('twig'),
'%kernel.debug%'
));
$container->setDefinition('app.exception_controller', $definition);
$container->register('app.exception_controller', CustomExceptionController::class)
->setArguments(array(
new Reference('twig'),
'%kernel.debug%',
));

And then configure ``twig.exception_controller`` using the controller as
services syntax (e.g. ``app.exception_controller:showAction``).
Expand Down
7 changes: 2 additions & 5 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,10 @@ argument:

// app/config/services.php
use AppBundle\Controller\HelloController;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$container->setDefinition('app.hello_controller', new Definition(
HelloController::class,
array(new Reference('templating'))
));
$container->register('app.hello_controller', HelloController::class)
->addArgument(new Reference('templating'));

Rather than fetching the ``templating`` service from the container, you can
inject *only* the exact service(s) that you need directly into the controller.
Expand Down
27 changes: 10 additions & 17 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,10 @@ Then, define a service for this class:

// app/config/services.php
use AppBundle\FileUploader;
use Symfony\Component\DependencyInjection\Definition;

// ...
$container->setDefinition('app.brochure_uploader', new Definition(
FileUploader::class,
array('%brochures_directory%')
));
$container->register('app.brochure_uploader', FileUploader::class)
->addArgument('%brochures_directory%');

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

Expand Down Expand Up @@ -415,21 +412,17 @@ Now, register this class as a Doctrine listener:

// app/config/services.php
use AppBundle\EventListener\BrochureUploaderListener;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

// ...
$definition = new Definition(
BrochureUploaderListener::class,
array(new Reference('brochures_directory'))
);
$definition->addTag('doctrine.event_listener', array(
'event' => 'prePersist',
));
$definition->addTag('doctrine.event_listener', array(
'event' => 'preUpdate',
));
$container->setDefinition('app.doctrine_brochure_listener', $definition);
$container->register('app.doctrine_brochure_listener', BrochureUploaderListener::class)
->addArgument(new Reference('brochures_directory'))
->addTag('doctrine.event_listener', array(
'event' => 'prePersist',
))
->addTag('doctrine.event_listener', array(
'event' => 'prePersist',
));

This listener is now automatically executed when persisting a new Product
entity. This way, you can remove everything related to uploading from the
Expand Down
1 change: 0 additions & 1 deletion doctrine/event_listeners_subscribers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ managers that use this connection.
use AppBundle\EventListener\SearchIndexer;
use AppBundle\EventListener\SearchIndexer2;
use AppBundle\EventListener\SearchIndexerSubscriber;
use Symfony\Component\DependencyInjection\Definition;

$container->loadFromExtension('doctrine', array(
'dbal' => array(
Expand Down
19 changes: 8 additions & 11 deletions doctrine/mongodb_session_storage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ need to change/add some parameters in the main configuration file:
.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;

$container->loadFromExtension('framework', array(
Expand All @@ -89,20 +88,19 @@ need to change/add some parameters in the main configuration file:
),
));

$container->setDefinition('mongo_client', new Definition(
\MongoClient::class,
array(
$container->register('mongo_client', \MongoClient::class)
->setArguments(array(
// if using a username and password
array('mongodb://%mongodb_username%:%mongodb_password%@%mongodb_host%:27017'),
// if not using a username and password
array('mongodb://%mongodb_host%:27017'),
)
));
));

$container->setDefinition('session.handler.mongo', new Definition(
MongoDbSessionHandler::class,
array(new Reference('mongo_client'), '%mongo.session.options%')
));
$container->register('session.handler.mongo', MongoDbSessionHandler::class)
->setArguments(array(
new Reference('mongo_client'),
'%mongo.session.options%',
));

The parameters used above should be defined somewhere in your application, often in your main
parameters configuration:
Expand Down Expand Up @@ -149,7 +147,6 @@ parameters configuration:
.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

$container->setParameter('mongo.session.options', array(
'database' => 'session_db', // your MongoDB database name
Expand Down
23 changes: 12 additions & 11 deletions doctrine/pdo_session_storage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ To use it, you just need to change some parameters in the main configuration fil

// app/config/config.php

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

// ...
$container->loadFromExtension('framework', array(
// ...
Expand All @@ -67,11 +69,11 @@ To use it, you just need to change some parameters in the main configuration fil
),
));

$storageDefinition = new Definition(PdoSessionHandler::class, array(
'mysql:dbname=mydatabase',
array('db_username' => 'myuser', 'db_password' => 'mypassword')
));
$container->setDefinition('session.handler.pdo', $storageDefinition);
$container->register('session.handler.pdo', PdoSessionHandler::class)
->setArguments(array(
'mysql:dbname=mydatabase',
array('db_username' => 'myuser', 'db_password' => 'mypassword'),
));

Configuring the Table and Column Names
--------------------------------------
Expand Down Expand Up @@ -112,15 +114,14 @@ a second array argument to ``PdoSessionHandler``:

// app/config/config.php

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

$storageDefinition = new Definition(PdoSessionHandler::class, array(
'mysql:dbname=mydatabase',
array('db_table' => 'sessions', 'db_username' => 'myuser', 'db_password' => 'mypassword')
));
$container->setDefinition('session.handler.pdo', $storageDefinition);
$container->register('session.handler.pdo', PdoSessionHandler::class)
->setArguments(array(
'mysql:dbname=mydatabase',
array('db_table' => 'sessions', 'db_username' => 'myuser', 'db_password' => 'mypassword'),
));

.. versionadded:: 2.6
The ``db_lifetime_col`` was introduced in Symfony 2.6. Prior to 2.6,
Expand Down
34 changes: 16 additions & 18 deletions event_dispatcher/before_after_filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,13 @@ your listener to be called just before any controller is executed.

// app/config/services.php
use AppBundle\EventListener\TokenListener;
use Symfony\Component\DependencyInjection\Definition;

$listener = new Definition(TokenListener::class, array('%tokens%'));
$listener->addTag('kernel.event_listener', array(
'event' => 'kernel.controller',
'method' => 'onKernelController'
));
$container->setDefinition('app.tokens.action_listener', $listener);
$container->register('app.tokens.action_listener', TokenListener::class)
->addArgument('%tokens%')
->addTag('kernel.event_listener', array(
'event' => 'kernel.controller',
'method' => 'onKernelController',
));

With this configuration, your ``TokenListener`` ``onKernelController()`` method
will be executed on each request. If the controller that is about to be executed
Expand Down Expand Up @@ -271,18 +270,17 @@ event:

// app/config/services.php
use AppBundle\EventListener\TokenListener;
use Symfony\Component\DependencyInjection\Definition;

$listener = new Definition(TokenListener::class, array('%tokens%'));
$listener->addTag('kernel.event_listener', array(
'event' => 'kernel.controller',
'method' => 'onKernelController'
));
$listener->addTag('kernel.event_listener', array(
'event' => 'kernel.response',
'method' => 'onKernelResponse'
));
$container->setDefinition('app.tokens.action_listener', $listener);
$container->register('app.tokens.action_listener', TokenListener::class)
->addArgument('%tokens%')
->addTag('kernel.event_listener', array(
'event' => 'kernel.controller',
'method' => 'onKernelController',
))
->addTag('kernel.event_listener', array(
'event' => 'kernel.response',
'method' => 'onKernelResponse',
));

That's it! The ``TokenListener`` is now notified before every controller is
executed (``onKernelController()``) and after every controller returns a response
Expand Down
11 changes: 3 additions & 8 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,12 @@ the ``genders`` parameter value as the first argument to its to-be-created

// src/AppBundle/Resources/config/services.php
use AppBundle\Form\Type\GenderType;
use Symfony\Component\DependencyInjection\Definition;

$container
->setDefinition('app.form.type.gender', new Definition(
GenderType::class,
array('%genders%')
))
$container->register('app.form.type.gender', GenderType::class)
->addArgument('%genders%')
->addTag('form.type', array(
'alias' => 'app_gender',
))
;
));

.. tip::

Expand Down
Loading