@@ -22,6 +22,8 @@ which means it's always safe to upgrade across minor versions.
22
22
All of the new features are **optional **: they are not enabled by default, so you
23
23
need to actually change your configuration files to use them.
24
24
25
+ .. _`service-33-default_definition` :
26
+
25
27
The new Default services.yml File
26
28
---------------------------------
27
29
@@ -417,7 +419,7 @@ In general, the new best practice is to use normal constructor dependency inject
417
419
4) Auto-tagging with autoconfigure
418
420
----------------------------------
419
421
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
421
423
``_defaults ``. Thanks to this, the container will auto-tag services registered in
422
424
this file. For example, suppose you want to create an event subscriber. First, you
423
425
create the class::
@@ -474,6 +476,69 @@ Many autoconfigured tags have an optional priority. If you need to specify a pri
474
476
(or any other optional tag attribute), no problem! Just :ref: `manually configure your service <services-manually-wire-args >`
475
477
and add the tag. Your tag will take precedent over the one added by auto-configuration.
476
478
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
+
477
542
What about Performance
478
543
----------------------
479
544
0 commit comments