SwiftmailerExtension.php 5.8KB

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