Skip to content

Commit b1e658e

Browse files
committed
[DI] Added _instanceof example
1 parent cff9918 commit b1e658e

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

service_container/3.3-di-changes.rst

Lines changed: 66 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

@@ -417,7 +419,7 @@ In general, the new best practice is to use normal constructor dependency inject
417419
4) Auto-tagging with autoconfigure
418420
----------------------------------
419421

420-
The last big change is the ``autoconfigure`` key, which is set to ``true`` under
422+
The fourth big change is the ``autoconfigure`` key, which is set to ``true`` under
421423
``_defaults``. Thanks to this, the container will auto-tag services registered in
422424
this file. For example, suppose you want to create an event subscriber. First, you
423425
create the class::
@@ -474,6 +476,69 @@ Many autoconfigured tags have an optional priority. If you need to specify a pri
474476
(or any other optional tag attribute), no problem! Just :ref:`manually configure your service <services-manually-wire-args>`
475477
and add the tag. Your tag will take precedent over the one added by auto-configuration.
476478

479+
5) Auto-configure with _instanceof
480+
----------------------------------
481+
482+
And the final big change is ``_instanceof``, it acts as a default definition
483+
template (see `service-33-default_definition`_), but only for service whose
484+
class matches a defined one.
485+
This can be very useful when many services shares some tag that cannot be
486+
inherited from abstract definition:
487+
488+
.. configuration-block::
489+
490+
.. code-block:: yaml
491+
492+
# app/config/services.yml
493+
services:
494+
# ...
495+
496+
_instanceof:
497+
class: AppBundle\Domain\LoaderInterface
498+
public: true
499+
tags: ['app.domain_loader']
500+
501+
.. code-block:: xml
502+
503+
<!-- app/config/services.xml -->
504+
<?xml version="1.0" encoding="UTF-8" ?>
505+
<container xmlns="http://symfony.com/schema/dic/services"
506+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
507+
xsi:schemaLocation="http://symfony.com/schema/dic/services
508+
http://symfony.com/schema/dic/services/services-1.0.xsd">
509+
510+
<services>
511+
<!-- ... -->
512+
513+
<instanceof class="AppBundle\Domain\LoaderInterface" public="true">
514+
<tag name="app.domain_loader" />
515+
</prototype>
516+
</services>
517+
</container>
518+
519+
.. code-block:: php
520+
521+
// app/config/services.php
522+
use Symfony\Component\DependencyInjection\Definition;
523+
524+
$domainLoaderDefinition = new Definition();
525+
526+
$domainLoaderDefinition->addTag('app.domain_loader');
527+
528+
// To use as default template
529+
$definition = new Definition();
530+
531+
$definition
532+
->setAutowired(true)
533+
->setAutoconfigured(true)
534+
->setPublic(false)
535+
->setInstanceofConditionals(
536+
'AppBundle\Domain\LoaderInterface' => $domainLoaderDefinition,
537+
)
538+
;
539+
540+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/{Entity,Repository}');
541+
477542
What about Performance
478543
----------------------
479544

0 commit comments

Comments
 (0)