SwiftmailerExtension.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\SwiftmailerBundle\DependencyInjection;
  11. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  12. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\Config\FileLocator;
  16. use Symfony\Component\Config\Definition\Processor;
  17. /**
  18. * SwiftmailerExtension is an extension for the SwiftMailer library.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class SwiftmailerExtension extends Extension
  23. {
  24. /**
  25. * Loads the Swift Mailer configuration.
  26. *
  27. * Usage example:
  28. *
  29. * <swiftmailer:config transport="gmail">
  30. * <swiftmailer:username>fabien</swift:username>
  31. * <swiftmailer:password>xxxxx</swift:password>
  32. * <swiftmailer:spool path="/path/to/spool/" />
  33. * </swiftmailer:config>
  34. *
  35. * @param array $configs An array of configuration settings
  36. * @param ContainerBuilder $container A ContainerBuilder instance
  37. */
  38. public function load(array $configs, ContainerBuilder $container)
  39. {
  40. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  41. $loader->load('swiftmailer.xml');
  42. $container->setAlias('mailer', 'swiftmailer.mailer');
  43. $processor = new Processor();
  44. $configuration = new Configuration($container->getParameter('kernel.debug'));
  45. $config = $processor->processConfiguration($configuration, $configs);
  46. if (null === $config['transport']) {
  47. $transport = 'null';
  48. } elseif ('gmail' === $config['transport']) {
  49. $config['encryption'] = 'ssl';
  50. $config['auth_mode'] = 'login';
  51. $config['host'] = 'smtp.gmail.com';
  52. $transport = 'smtp';
  53. } else {
  54. $transport = $config['transport'];
  55. }
  56. if ('smtp' === $transport) {
  57. $loader->load('smtp.xml');
  58. }
  59. $container->setParameter('swiftmailer.transport.name', $transport);
  60. $container->setAlias('swiftmailer.transport', 'swiftmailer.transport.'.$transport);
  61. if (false === $config['port']) {
  62. $config['port'] = 'ssl' === $config['encryption'] ? 465 : 25;
  63. }
  64. foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode') as $key) {
  65. $container->setParameter('swiftmailer.transport.'.$transport.'.'.$key, $config[$key]);
  66. }
  67. // spool?
  68. if (isset($config['spool'])) {
  69. $type = $config['spool']['type'];
  70. $loader->load('spool.xml');
  71. $container->setAlias('swiftmailer.transport.real', 'swiftmailer.transport.'.$transport);
  72. $container->setAlias('swiftmailer.transport', 'swiftmailer.transport.spool');
  73. $container->setAlias('swiftmailer.spool', 'swiftmailer.spool.'.$type);
  74. foreach (array('path') as $key) {
  75. $container->setParameter('swiftmailer.spool.'.$type.'.'.$key, $config['spool'][$key]);
  76. }
  77. }
  78. $container->setParameter('swiftmailer.spool.enabled', isset($config['spool']));
  79. if ($config['logging']) {
  80. $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.messagelogger')));
  81. $container->findDefinition('swiftmailer.data_collector')->addTag('data_collector', array('template' => 'SwiftmailerBundle:Collector:swiftmailer', 'id' => 'swiftmailer'));
  82. }
  83. if (isset($config['sender_address']) && $config['sender_address']) {
  84. $container->setParameter('swiftmailer.sender_address', $config['sender_address']);
  85. $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.impersonate')));
  86. } else {
  87. $container->setParameter('swiftmailer.sender_address', null);
  88. }
  89. if (isset($config['delivery_address']) && $config['delivery_address']) {
  90. $container->setParameter('swiftmailer.single_address', $config['delivery_address']);
  91. $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.redirecting')));
  92. } else {
  93. $container->setParameter('swiftmailer.single_address', null);
  94. }
  95. if (isset($config['disable_delivery']) && $config['disable_delivery']) {
  96. $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.blackhole')));
  97. }
  98. }
  99. /**
  100. * Returns the base path for the XSD files.
  101. *
  102. * @return string The XSD base path
  103. */
  104. public function getXsdValidationBasePath()
  105. {
  106. return __DIR__.'/../Resources/config/schema';
  107. }
  108. /**
  109. * Returns the namespace to be used for this extension (XML namespace).
  110. *
  111. * @return string The XML namespace
  112. */
  113. public function getNamespace()
  114. {
  115. return 'http://symfony.com/schema/dic/swiftmailer';
  116. }
  117. }