Skip to content

[DependencyInjection] update service_container.rst #17051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <services-wire-specific-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::
Comment on lines 1195 to +1198
Copy link
Member

@nicolas-grekas nicolas-grekas Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want to pass the second, you'll need to :ref:`manually wire the service <services-wire-specific-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::
If you want to pass ``site_update_manager.superadmin``, you'll need either to :ref:`manually wire it<services-wire-specific-service>`
or to create a named autowiring alias.

(named autowiring alias should link to service_container/autowiring.rst, then, the example is not needed)


.. 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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas can you confirm? TBH I never heard/saw such thing.

Should we add it to the docs?

Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's a named autowiring alias, and it's good to tell about it indeed
that being said, we have the service_container/autowiring.rst that already explains what they are.
I think the example should be removed and replaced by a link to that page.


.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- ... -->

<service id="site_update_manager.superadmin" class="App\Service\SiteUpdateManager" autowire="false">
<argument type="service" id="App\Service\MessageGenerator"/>
<argument type="service" id="mailer"/>
<argument>superadmin@example.com</argument>
</service>

<service id="site_update_manager.normal_users" class="App\Service\SiteUpdateManager" autowire="false">
<argument type="service" id="App\Service\MessageGenerator"/>
<argument type="service" id="mailer"/>
<argument>contact@example.com</argument>
</service>

<service id="App\Service\SiteUpdateManager" alias="site_update_manager.superadmin"/>
<service
id="App\Service\SiteUpdateManager $normalUsers"
alias="site_update_manager.normal_users"/>
</services>
</container>

.. 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/ <service-container-services-load-example>`,
Expand Down