From 8b7373b4459b36d874e1ed2e9a187230b21d7d1d Mon Sep 17 00:00:00 2001 From: mohamed gasmi Date: Sat, 23 Jul 2022 17:35:21 +0200 Subject: [PATCH 1/2] add alternative solution for multiple implamentations --- service_container.rst | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/service_container.rst b/service_container.rst index ddb81243ccd..63809798496 100644 --- a/service_container.rst +++ b/service_container.rst @@ -1193,6 +1193,109 @@ In this case, *two* services are registered: ``site_update_manager.superadmin`` and ``site_update_manager.normal_users``. Thanks to the alias, if you type-hint ``SiteUpdateManager`` the first (``site_update_manager.superadmin``) will be passed. If you want to pass the second, you'll need to :ref:`manually wire the service `. +Another solution is to add another id ``site_update_manager.superadmin`` followed by +a variable name matching the one you use when doing the injection:: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + # ... + + # this is the service's id + site_update_manager.superadmin: + class: App\Service\SiteUpdateManager + autowire: false + arguments: + - '@App\Service\MessageGenerator' + - '@mailer' + - 'superadmin@example.com' + + site_update_manager.normal_users: + class: App\Service\SiteUpdateManager + autowire: false + arguments: + - '@App\Service\MessageGenerator' + - '@mailer' + - 'contact@example.com' + + # Create an alias, so that - by default - if you type-hint SiteUpdateManager, + # the site_update_manager.superadmin will be used + App\Service\SiteUpdateManager: '@site_update_manager.superadmin' + + # If you type-hint SiteUpdateManager + # for a ``$normalUsers`` argument is detected. + App\Service\SiteUpdateManager $normalUsers: '@site_update_manager.normal_users' + + .. code-block:: xml + + + + + + + + + + + + superadmin@example.com + + + + + + contact@example.com + + + + + + + + .. code-block:: php + + // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; + + use App\Service\MessageGenerator; + use App\Service\SiteUpdateManager; + + return function(ContainerConfigurator $configurator) { + // ... + + // site_update_manager.superadmin is the service's id + $services->set('site_update_manager.superadmin', SiteUpdateManager::class) + ->autowire(false) + ->args([ + ref(MessageGenerator::class), + ref('mailer'), + 'superadmin@example.com', + ]); + + $services->set('site_update_manager.normal_users', SiteUpdateManager::class) + ->autowire(false) + ->args([ + ref(MessageGenerator::class), + ref('mailer'), + 'contact@example.com', + ]); + + // Create an alias, so that - by default - if you type-hint SiteUpdateManager, + // the site_update_manager.superadmin will be used + $services->alias(SiteUpdateManager::class, 'site_update_manager.superadmin'); + + // If you type-hint SiteUpdateManager + // for a ``$normalUsers`` argument is detected. + $services->alias(SiteUpdateManager::class.' $normalUsers', 'site_update_manager.normal_users'); + }; .. caution:: From 0ff32bdc02d601725da7b04a2ee433a1e8c14992 Mon Sep 17 00:00:00 2001 From: mohamed gasmi Date: Sat, 23 Jul 2022 17:45:32 +0200 Subject: [PATCH 2/2] added break line --- service_container.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/service_container.rst b/service_container.rst index 63809798496..a5dff0001c6 100644 --- a/service_container.rst +++ b/service_container.rst @@ -1193,6 +1193,7 @@ In this case, *two* services are registered: ``site_update_manager.superadmin`` and ``site_update_manager.normal_users``. Thanks to the alias, if you type-hint ``SiteUpdateManager`` the first (``site_update_manager.superadmin``) will be passed. If you want to pass the second, you'll need to :ref:`manually wire the service `. + Another solution is to add another id ``site_update_manager.superadmin`` followed by a variable name matching the one you use when doing the injection::