From 16e0cc74c06ead965e99c5e3a304496054f4ef65 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 28 Aug 2017 10:04:12 -0400 Subject: [PATCH 1/2] [DI] add docs for new namespace option --- service_container.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/service_container.rst b/service_container.rst index 006c59d7645..12ae3b1704b 100644 --- a/service_container.rst +++ b/service_container.rst @@ -923,6 +923,31 @@ them will not cause the container to be rebuilt. means that all classes are "available to be *used* as services" without needing to be manually configured. +.. sidebar:: The ``namespace`` Option + + .. versionadded:: 3.4 + The ``namespace`` option was added in Symfony 3.4. + + When using the ``resource`` option in YAML, the namespace prefix can only be used once + per file when defining it as the ``id``. In order to have multiple definitions in the + same file with the same namespace prefix, you can use the ``namespace`` option. When + this option is used, the ``id`` can be anything as long as it is unique. For example, + you can define your services like this: + + .. code-block:: yaml + + # app/config/services.yml + services: + command_handlers: + namespace: App\Domain\ + resource: ../../src/Domain/*/CommandHandler + tags: [command_handler] + + event_subscribers: + namespace: App\Domain\ + resource: ../../src/Domain/*/EventSubscriber + tags: [event_subscriber] + .. _services-explicitly-configure-wire-services: Explicitly Configuring Services and Arguments From f395dadde5b39c7eca2e18271316f35578266380 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 9 Feb 2018 16:34:22 +0100 Subject: [PATCH 2/2] Reworded and turned the sidebar into a section --- service_container.rst | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/service_container.rst b/service_container.rst index 12ae3b1704b..7c39997464c 100644 --- a/service_container.rst +++ b/service_container.rst @@ -923,16 +923,26 @@ them will not cause the container to be rebuilt. means that all classes are "available to be *used* as services" without needing to be manually configured. -.. sidebar:: The ``namespace`` Option +Multiple Service Definitions Using the Same Namespace +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - .. versionadded:: 3.4 - The ``namespace`` option was added in Symfony 3.4. +.. versionadded:: 3.4 + The ``namespace`` option in the YAML configuration was introduced in Symfony 3.4. - When using the ``resource`` option in YAML, the namespace prefix can only be used once - per file when defining it as the ``id``. In order to have multiple definitions in the - same file with the same namespace prefix, you can use the ``namespace`` option. When - this option is used, the ``id`` can be anything as long as it is unique. For example, - you can define your services like this: +If you define services using the YAML config format, the PHP namespace is used +as the key of each configuration, so you can't define different service configs +for classes under the same namespace: + +.. code-block:: yaml + + # app/config/services.yml + services: + App\Domain\: + resource: '../../src/Domain/*' + # ... + +In order to have multiple definitions, add the ``namespace`` option and use any +unique string as the key of each service config: .. code-block:: yaml @@ -940,12 +950,12 @@ them will not cause the container to be rebuilt. services: command_handlers: namespace: App\Domain\ - resource: ../../src/Domain/*/CommandHandler + resource: '../../src/Domain/*/CommandHandler' tags: [command_handler] event_subscribers: namespace: App\Domain\ - resource: ../../src/Domain/*/EventSubscriber + resource: '../../src/Domain/*/EventSubscriber' tags: [event_subscriber] .. _services-explicitly-configure-wire-services: