Skip to content

Commit f2b2791

Browse files
committed
minor #9410 Better explain how to inject mailers different than the default one (javiereguiluz)
This PR was merged into the 4.0 branch. Discussion ---------- Better explain how to inject mailers different than the default one As reported in symfony/symfony#26444, this explanation could be updated to the latest Symfony services practices. Commits ------- dc287f4 Better explain how to inject mailers different than the default one
2 parents 2bee844 + dc287f4 commit f2b2791

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

reference/configuration/swiftmailer.rst

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ key (the default mailer is identified by the ``default_mailer`` option):
344344
),
345345
));
346346
347-
Each mailer is registered as a service::
347+
Each mailer is registered automatically as a service with these IDs::
348348

349349
// ...
350350

@@ -362,3 +362,70 @@ Each mailer is registered as a service::
362362
When configuring multiple mailers, options must be placed under the
363363
appropriate mailer key of the configuration instead of directly under the
364364
``swiftmailer`` key.
365+
366+
When using :ref:`autowiring <services-autowire>` only the default mailer is
367+
injected when type-hinting some argument with the ``\Swift_Mailer`` class. If
368+
you need to inject a different mailer in some service, use any of these
369+
alternatives based on the :ref:`service binding <services-binding>` feature:
370+
371+
.. configuration-block::
372+
373+
.. code-block:: yaml
374+
375+
# config/services.yaml
376+
services:
377+
_defaults:
378+
bind:
379+
# this injects the second mailer when type-hinting constructor arguments with \Swift_Mailer
380+
\Swift_Mailer: '@swiftmailer.mailer.second_mailer'
381+
# this injects the second mailer when a service constructor argument is called $specialMailer
382+
$specialMailer: '@swiftmailer.mailer.second_mailer'
383+
384+
App\Some\Service:
385+
# this injects the second mailer only for this argument of this service
386+
$differentMailer: '@swiftmailer.mailer.second_mailer'
387+
388+
# ...
389+
390+
.. code-block:: xml
391+
392+
<!-- config/services.xml -->
393+
<?xml version="1.0" encoding="UTF-8" ?>
394+
<container xmlns="http://symfony.com/schema/dic/services"
395+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
396+
xsi:schemaLocation="http://symfony.com/schema/dic/services
397+
http://symfony.com/schema/dic/services/services-1.0.xsd">
398+
399+
<services>
400+
<defaults autowire="true" autoconfigure="true" public="false">
401+
<!-- this injects the second mailer when type-hinting constructor arguments with \Swift_Mailer -->
402+
<bind key="\Swift_Mailer">@swiftmailer.mailer.second_mailer</bind>
403+
<!-- this injects the second mailer when a service constructor argument is called $specialMailer -->
404+
<bind key="$specialMailer">@swiftmailer.mailer.second_mailer</bind>
405+
</defaults>
406+
407+
<service id="App\Some\Service">
408+
<!-- this injects the second mailer only for this argument of this service -->
409+
<argument key="$differentMailer">@swiftmailer.mailer.second_mailer</argument>
410+
</service>
411+
412+
<!-- ... -->
413+
</services>
414+
</container>
415+
416+
.. code-block:: php
417+
418+
// config/services.php
419+
use App\Some\Service;
420+
use Symfony\Component\DependencyInjection\Reference;
421+
use Psr\Log\LoggerInterface;
422+
423+
$container->register(Service::class)
424+
->setPublic(true)
425+
->setBindings(array(
426+
// this injects the second mailer when this service type-hints constructor arguments with \Swift_Mailer
427+
\Swift_Mailer => '@swiftmailer.mailer.second_mailer',
428+
// this injects the second mailer when this service has a constructor argument called $specialMailer
429+
'$specialMailer' => '@swiftmailer.mailer.second_mailer',
430+
))
431+
;

0 commit comments

Comments
 (0)