diff --git a/service_container.rst b/service_container.rst index ddb81243ccd..a5dff0001c6 100644 --- a/service_container.rst +++ b/service_container.rst @@ -1194,6 +1194,110 @@ 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:: If you do *not* create the alias and are :ref:`loading all services from src/ `,