From 0e993e5138aa8a3fa373d34d7400c6bdf9fa5176 Mon Sep 17 00:00:00 2001 From: HeahDude Date: Sun, 30 Jul 2017 00:22:57 +0200 Subject: [PATCH 1/2] [DI] Added _instanceof example --- service_container/3.3-di-changes.rst | 67 +++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/service_container/3.3-di-changes.rst b/service_container/3.3-di-changes.rst index 90b64ea3cbd..c7d20187e68 100644 --- a/service_container/3.3-di-changes.rst +++ b/service_container/3.3-di-changes.rst @@ -22,6 +22,8 @@ which means it's always safe to upgrade across minor versions. All of the new features are **optional**: they are not enabled by default, so you need to actually change your configuration files to use them. +.. _`service-33-default_definition`: + The new Default services.yml File --------------------------------- @@ -417,7 +419,7 @@ In general, the new best practice is to use normal constructor dependency inject 4) Auto-tagging with autoconfigure ---------------------------------- -The last big change is the ``autoconfigure`` key, which is set to ``true`` under +The fourth big change is the ``autoconfigure`` key, which is set to ``true`` under ``_defaults``. Thanks to this, the container will auto-tag services registered in this file. For example, suppose you want to create an event subscriber. First, you create the class:: @@ -474,6 +476,69 @@ Many autoconfigured tags have an optional priority. If you need to specify a pri (or any other optional tag attribute), no problem! Just :ref:`manually configure your service ` and add the tag. Your tag will take precedence over the one added by auto-configuration. +5) Auto-configure with _instanceof +---------------------------------- + +And the final big change is ``_instanceof``, it acts as a default definition +template (see `service-33-default_definition`_), but only for service whose +class matches a defined one. +This can be very useful when many services shares some tag that cannot be +inherited from abstract definition: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/services.yml + services: + # ... + + _instanceof: + class: AppBundle\Domain\LoaderInterface + public: true + tags: ['app.domain_loader'] + + .. code-block:: xml + + + + + + + + + + + + + + + .. code-block:: php + + // app/config/services.php + use Symfony\Component\DependencyInjection\Definition; + + $domainLoaderDefinition = new Definition(); + + $domainLoaderDefinition->addTag('app.domain_loader'); + + // To use as default template + $definition = new Definition(); + + $definition + ->setAutowired(true) + ->setAutoconfigured(true) + ->setPublic(false) + ->setInstanceofConditionals( + 'AppBundle\Domain\LoaderInterface' => $domainLoaderDefinition, + ) + ; + + $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/{Entity,Repository}'); + What about Performance ---------------------- From 519d7f6cb8902c4c68b10e8105233a33a3723a2a Mon Sep 17 00:00:00 2001 From: HeahDude Date: Sat, 5 Aug 2017 09:41:23 +0200 Subject: [PATCH 2/2] fixup comment --- service_container/3.3-di-changes.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/service_container/3.3-di-changes.rst b/service_container/3.3-di-changes.rst index c7d20187e68..c5c5b348435 100644 --- a/service_container/3.3-di-changes.rst +++ b/service_container/3.3-di-changes.rst @@ -479,11 +479,11 @@ and add the tag. Your tag will take precedence over the one added by auto-config 5) Auto-configure with _instanceof ---------------------------------- -And the final big change is ``_instanceof``, it acts as a default definition -template (see `service-33-default_definition`_), but only for service whose +And the final big change is ``_instanceof``. It acts as a default definition +template (see `service-33-default_definition`_), but only for services whose class matches a defined one. -This can be very useful when many services shares some tag that cannot be -inherited from abstract definition: +This can be very useful when many services share some tag that cannot be +inherited from an abstract definition: .. configuration-block::