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