8000 [DI] Added _instanceof example · symfony/symfony-docs@471adf7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 471adf7

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

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

service_container/3.3-di-changes.rst

Lines changed: 51 additions & 1 deletion
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,54 @@ 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+
This can be very useful when many services share some tag that cannot be
480+
inherited from an abstract definition:
481+
482+
.. configuration-block::
483+
484+
.. code-block:: yaml
485+
486+
# app/config/services.yml
487+
services:
488+
# ...
489+
490+
_instanceof:
491+
class: AppBundle\Domain\LoaderInterface
492+
public: true
493+
tags: ['app.domain_loader']
494+
495+
.. code-block:: xml
496+
497+
<!-- app/config/services.xml -->
498+
<?xml version="1.0" encoding="UTF-8" ?>
499+
<container xmlns="http://symfony.com/schema/dic/services"
500+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
501+
xsi:schemaLocation="http://symfony.com/schema/dic/services
502+
http://symfony.com/schema/dic/services/services-1.0.xsd">
503+
504+
<services>
505+
<!-- ... -->
506+
507+
<instanceof id="AppBundle\Domain\LoaderInterface" public="true">
508+
<tag name="app.domain_loader" />
509+
</instanceof>
510+
</services>
511+
</container>
512+
513+
.. code-block:: php
514+
515+
// app/config/services.php
516+
use AppBundle\Domain\LoaderInterface;
517+
518+
$container->registerForAutoconfiguration(LoaderInterface::class)
519+
->addTag('app.domain_loader');
520+
471521
What about Performance
472522
----------------------
473523

0 commit comments

Comments
 (0)
0