AddSwiftMailerTransportPass.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * Sets the transport for Swiftmailer handlers depending on the existing
  16. * container definitions.
  17. *
  18. * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  19. */
  20. class AddSwiftMailerTransportPass implements CompilerPassInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function process(ContainerBuilder $container)
  26. {
  27. $handlers = $container->getParameter('monolog.swift_mailer.handlers');
  28. foreach ($handlers as $id) {
  29. $definition = $container->getDefinition($id);
  30. $mailerId = (string) $definition->getArgument(0);
  31. // Try to fetch the transport for a non-default mailer first, then go with the default swiftmailer
  32. $possibleServices = [
  33. $mailerId.'.transport.real',
  34. $mailerId.'.transport',
  35. 'swiftmailer.transport.real',
  36. 'swiftmailer.transport',
  37. ];
  38. foreach ($possibleServices as $serviceId) {
  39. if ($container->hasAlias($serviceId) || $container->hasDefinition($serviceId)) {
  40. $definition->addMethodCall(
  41. 'setTransport',
  42. [new Reference($serviceId)]
  43. );
  44. break;
  45. }
  46. }
  47. }
  48. }
  49. }