Skip to content

Commit 691c547

Browse files
HeahDudeGrégoire Paris
authored and
Grégoire Paris
committed
[DI] Added _instanceof example
Closes #8239
1 parent ec36b1a commit 691c547

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

service_container/3.3-di-changes.rst

+56-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ which means it's always safe to upgrade across minor versions.
2222
All of the new features are **optional**: they are not enabled by default, so you
2323
need to actually change your configuration files to use them.
2424

25+
.. _`service-33-default_definition`:
26+
2527
The new Default services.yml File
2628
---------------------------------
2729

@@ -411,7 +413,7 @@ In general, the new best practice is to use normal constructor dependency inject
411413
4) Auto-tagging with autoconfigure
412414
----------------------------------
413415

414-
The last big change is the ``autoconfigure`` key, which is set to ``true`` under
416+
The fourth big change is the ``autoconfigure`` key, which is set to ``true`` under
415417
``_defaults``. Thanks to this, the container will auto-tag services registered in
416418
this file. For example, suppose you want to create an event subscriber. First, you
417419
create the class::
@@ -468,6 +470,59 @@ Many autoconfigured tags have an optional priority. If you need to specify a pri
468470
(or any other optional tag attribute), no problem! Just :ref:`manually configure your service <services-manually-wire-args>`
469471
and add the tag. Your tag will take precedence over the one added by auto-configuration.
470472

473+
5) Auto-configure with _instanceof
474+
----------------------------------
475+
476+
And the final big change is ``_instanceof``. It acts as a default definition
477+
template (see `service-33-default_definition`_), but only for services whose
478+
class matches a defined one.
479+
480+
This can be very useful when many services share some tag that cannot be
481+
inherited from an abstract definition:
482+
483+
.. configuration-block::
484+
485+
.. code-block:: yaml
486+
487+
# app/config/services.yml
488+
services:
489+
# ...
490+
491+
_instanceof:
492+
class: AppBundle\Domain\LoaderInterface
493+
public: true
494+
tags: ['app.domain_loader']
495+
496+
.. code-block:: xml
497+
498+
<!-- app/config/services.xml -->
499+
<?xml version="1.0" encoding="UTF-8" ?>
500+
<container xmlns="http://symfony.com/schema/dic/services"
501+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
502+
xsi:schemaLocation="http://symfony.com/schema/dic/services
503+
http://symfony.com/schema/dic/services/services-1.0.xsd">
504+
505+
<services>
506+
<!-- ... -->
507+
508+
<instanceof id="AppBundle\Domain\LoaderInterface" public="true">
509+
<tag name="app.domain_loader" />
510+
</instanceof>
511+
</services>
512+
</container>
513+
514+
.. code-block:: php
515+
516+
// app/config/services.php
517+
use AppBundle\Domain\LoaderInterface;
518+
519+
// ...
520+
521+
/* This method returns a child definition to define the default
522+
configuration of the given class or interface */
523+
$container->registerForAutoconfiguration(LoaderInterface::class)
524+
->addTag('app.domain_loader');
525+
471526
What about Performance
472527
----------------------
473528

0 commit comments

Comments
 (0)