SwiftmailerExtension.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\Extension\Extension;
  4. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. use Symfony\Component\DependencyInjection\Reference;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * SwiftMailerExtension is an extension for the SwiftMailer library.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.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 $config An array of configuration settings
  34. * @param ContainerBuilder $container A ContainerBuilder instance
  35. */
  36. public function configLoad($config, ContainerBuilder $container)
  37. {
  38. if (!$container->hasDefinition('swiftmailer.mailer')) {
  39. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  40. $loader->load('swiftmailer.xml');
  41. $container->setAlias('mailer', 'swiftmailer.mailer');
  42. }
  43. $r = new \ReflectionClass('Swift_Message');
  44. $container->setParameter('swiftmailer.base_dir', dirname(dirname(dirname($r->getFilename()))));
  45. $transport = $container->getParameter('swiftmailer.transport.name');
  46. if (array_key_exists('transport', $config)) {
  47. if (null === $config['transport']) {
  48. $transport = 'null';
  49. } elseif ('gmail' === $config['transport']) {
  50. $config['encryption'] = 'ssl';
  51. $config['auth_mode'] = 'login';
  52. $config['host'] = 'smtp.gmail.com';
  53. $transport = 'smtp';
  54. } else {
  55. $transport = $config['transport'];
  56. }
  57. $container->setParameter('swiftmailer.transport.name', $transport);
  58. }
  59. $container->setAlias('swiftmailer.transport', 'swiftmailer.transport.'.$transport);
  60. if (isset($config['encryption']) && 'ssl' === $config['encryption'] && !isset($config['port'])) {
  61. $config['port'] = 465;
  62. }
  63. foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode') as $key) {
  64. if (isset($config[$key])) {
  65. $container->setParameter('swiftmailer.transport.'.$transport.'.'.$key, $config[$key]);
  66. }
  67. }
  68. // spool?
  69. if (isset($config['spool'])) {
  70. $type = isset($config['spool']['type']) ? $config['spool']['type'] : 'file';
  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. if (isset($config['spool'][$key])) {
  76. $container->setParameter('swiftmailer.spool.'.$type.'.'.$key, $config['spool'][$key]);
  77. }
  78. }
  79. }
  80. if (array_key_exists('delivery-address', $config)) {
  81. $config['delivery-address'] = $config['delivery_address'];
  82. }
  83. if (isset($config['delivery_address'])) {
  84. $container->setParameter('swiftmailer.single_address', $config['delivery_address']);
  85. $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.redirecting')));
  86. }
  87. if (array_key_exists('disable-delivery', $config)) {
  88. $config['disable_delivery'] = $config['disable-delivery'];
  89. }
  90. if (isset($config['disable_delivery']) && $config['disable_delivery']) {
  91. $container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.blackhole')));
  92. }
  93. }
  94. /**
  95. * Returns the base path for the XSD files.
  96. *
  97. * @return string The XSD base path
  98. */
  99. public function getXsdValidationBasePath()
  100. {
  101. return __DIR__.'/../Resources/config/schema';
  102. }
  103. /**
  104. * Returns the namespace to be used for this extension (XML namespace).
  105. *
  106. * @return string The XML namespace
  107. */
  108. public function getNamespace()
  109. {
  110. return 'http://www.symfony-project.org/schema/dic/swiftmailer';
  111. }
  112. /**
  113. * Returns the recommended alias to use in XML.
  114. *
  115. * This alias is also the mandatory prefix to use when using YAML.
  116. *
  117. * @return string The alias
  118. */
  119. public function getAlias()
  120. {
  121. return 'swiftmailer';
  122. }
  123. }