Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

Commit 1a70e60

Browse files
committed
add factory
1 parent ad75109 commit 1a70e60

File tree

3 files changed

+207
-69
lines changed

3 files changed

+207
-69
lines changed

DependencyInjection/Configuration.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,35 @@ private function getMailersNode()
8585
->prototype('array')
8686
// BC layer for "delivery_address: null" (the case of a string goes through the XML normalization too)
8787
->beforeNormalization()
88-
->ifTrue(function ($v) {
89-
return is_array($v) && array_key_exists('delivery_address', $v) && null === $v['delivery_address'];
90-
})
91-
->then(function ($v) {
92-
@trigger_error('The swiftmailer.delivery_address configuration key is deprecated since version 2.3.10 and will be removed in 3.0. Use the swiftmailer.delivery_addresses configuration key instead (or remove the empty setting)', E_USER_DEPRECATED);
93-
unset($v['delivery_address']);
88+
->ifTrue(function ($v) {
89+
return is_array($v) && array_key_exists('delivery_address', $v) && null === $v['delivery_address'];
90+
})
91+
->then(function ($v) {
92+
@trigger_error('The swiftmailer.delivery_address configuration key is deprecated since version 2.3.10 and will be removed in 3.0. Use the swiftmailer.delivery_addresses configuration key instead (or remove the empty setting)', E_USER_DEPRECATED);
93+
unset($v['delivery_address']);
9494

95-
if (!isset($v['delivery_addresses'])) {
96-
$v['delivery_addresses'] = array();
97-
}
95+
if (!isset($v['delivery_addresses'])) {
96+
$v['delivery_addresses'] = array();
97+
}
9898

99-
return $v;
100-
})
99+
return $v;
100+
})
101+
->end()
102+
// Deprecate url
103+
->beforeNormalization()
104+
->ifTrue(function ($v) {
105+
return is_array($v) && array_key_exists('url', $v);
106+
})
107+
->then(function ($v) {
108+
@trigger_error('The swiftmailer.url configuration key is deprecated since version 2.5.0 and will be removed in 3.0. Use the swiftmailer.dsn configuration key instead', E_USER_DEPRECATED);
109+
$v['dsn'] = $v['url'];
110+
unset($v['url']);
111+
112+
return $v;
113+
})
101114
->end()
102115
->children()
116+
->scalarNode('dsn')->defaultNull()->end()
103117
->scalarNode('url')->defaultNull()->end()
104118
->scalarNode('transport')->defaultValue('smtp')->end()
105119
->scalarNode('username')->defaultNull()->end()

DependencyInjection/SwiftmailerExtension.php

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\SwiftmailerBundle\DependencyInjection;
1313

14+
use Symfony\Component\DependencyInjection\Definition;
1415
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1516
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -66,63 +67,57 @@ public function load(array $configs, ContainerBuilder $container)
6667

6768
protected function configureMailer($name, array $mailer, ContainerBuilder $container, $isDefaultMailer = false)
6869
{
69-
if (null === $mailer['transport']) {
70-
$transport = 'null';
71-
} elseif ('gmail' === $mailer['transport']) {
72-
$mailer['encryption'] = 'ssl';
73-
$mailer['auth_mode'] = 'login';
74-
$mailer['host'] = 'smtp.gmail.com';
75-
$transport = 'smtp';
70+
$definitionDecorator = $this->createChildDefinition('swiftmailer.transport.eventdispatcher.abstract');
71+
$container
72+
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator)
73+
;
74+
75+
$options = [];
76+
$container->resolveEnvPlaceholders($mailer, null, $options);
77+
78+
if ($options && isset($mailer['dsn'])) {
79+
$transportId = sprintf('swiftmailer.mailer.%s.transport.dynamic', $name);
80+
$definitionDecorator = (new Definition(\Swift_Transport::class))
81+
->setFactory(array(SwiftmailerTransportFactory::class, 'createTransport'))
82+
->setArguments(array(
83+
$mailer,
84+
new Reference('router.request_context'),
85+
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
86+
));
87+
$container->setDefinition(sprintf('swiftmailer.mailer.%s.transport.dynamic', $name), $definitionDecorator);
88+
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), $transportId);
89+
90+
$definitionDecorator = $this->createChildDefinition('swiftmailer.mailer.abstract');
91+
$container
92+
->setDefinition(sprintf('swiftmailer.mailer.%s', $name), $definitionDecorator)
93+
->replaceArgument(0, new Reference(sprintf('swiftmailer.mailer.%s.transport', $name)))
94+
;
95+
96+
$enable = !(isset($mailer['disable_delivery']) && $mailer['disable_delivery']);
97+
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), $enable);
7698
} else {
77-
$transport = $mailer['transport'];
78-
}
99+
$transport = SwiftmailerTransportFactory::resolveOptions($mailer);
79100

80-
if (null !== $mailer['url']) {
81-
$parts = parse_url($mailer['url']);
82-
if (!empty($parts['scheme'])) {
83-
$transport = $parts['scheme'];
84-
}
101+
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name), $transport);
85102

86-
if (!empty($parts['user'])) {
87-
$mailer['username'] = $parts['user'];
88-
}
89-
if (!empty($parts['pass'])) {
90-
$mailer['password'] = $parts['pass'];
91-
}
92-
if (!empty($parts['host'])) {
93-
$mailer['host'] = $parts['host'];
94-
}
95-
if (!empty($parts['port'])) {
96-
$mailer['port'] = $parts['port'];
97-
}
98-
if (!empty($parts['query'])) {
99-
$query = array();
100-
parse_str($parts['query'], $query);
101-
if (!empty($query['encryption'])) {
102-
$mailer['encryption'] = $query['encryption'];
103-
}
104-
if (!empty($query['auth_mode'])) {
105-
$mailer['auth_mode'] = $query['auth_mode'];
106-
}
103+
if (isset($mailer['disable_delivery']) && $mailer['disable_delivery']) {
104+
$transport = 'null';
105+
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), false);
106+
} else {
107+
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), true);
107108
}
108-
}
109-
unset($mailer['url']);
110109

111-
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name), $transport);
110+
if (empty($mailer['port'])) {
111+
$mailer['port'] = 'ssl' === $mailer['encryption'] ? 465 : 25;
112+
}
112113

113-
if (isset($mailer['disable_delivery']) && $mailer['disable_delivery']) {
114-
$transport = 'null';
115-
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), false);
116-
} else {
117-
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), true);
118-
}
114+
$transportId = in_array($transport, array('smtp', 'mail', 'sendmail', 'null'))
115+
? sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport)
116+
: $transport;
119117

120-
if (empty($mailer['port'])) {
121-
$mailer['port'] = 'ssl' === $mailer['encryption'] ? 465 : 25;
118+
$this->configureMailerTransport($name, $mailer, $container, $transport, $isDefaultMailer);
122119
}
123-
124-
$this->configureMailerTransport($name, $mailer, $container, $transport, $isDefaultMailer);
125-
$this->configureMailerSpool($name, $mailer, $container, $transport, $isDefaultMailer);
120+
$this->configureMailerSpool($name, $mailer, $container, $transportId, $isDefaultMailer);
126121
$this->configureMailerSenderAddress($name, $mailer, $container, $isDefaultMailer);
127122
$this->configureMailerAntiFlood($name, $mailer, $container, $isDefaultMailer);
128123
$this->configureMailerDeliveryAddress($name, $mailer, $container, $isDefaultMailer);
@@ -144,11 +139,6 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild
144139
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.%s', $name, $key), $mailer[$key]);
145140
}
146141

147-
$definitionDecorator = $this->createChildDefinition('swiftmailer.transport.eventdispatcher.abstract');
148-
$container
149-
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator)
150-
;
151-
152142
if ('smtp' === $transport) {
153143
$authDecorator = $this->createChildDefinition('swiftmailer.transport.authhandler.abstract');
154144
$container
@@ -278,10 +268,6 @@ protected function configureMailerSpool($name, array $mailer, ContainerBuilder $
278268
))
279269
;
280270

281-
if (in_array($transport, array('smtp', 'mail', 'sendmail', 'null'))) {
282-
// built-in transport
283-
$transport = sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport);
284-
}
285271
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport.real', $name), $transport);
286272
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.spool', $name));
287273
$container->setParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name), true);
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Routing\RequestContext;
15+
16+
/**
17+
* Factory to create a \Swift_Transport object.
18+
*
19+
* @author Romain Gautier <[email protected]>
20+
*/
21+
class SwiftmailerTransportFactory
22+
{
23+
/**
24+
* @param array $options
25+
* @param RequestContext $requestContext
26+
* @param \Swift_Events_EventDispatcher $eventDispatcher
27+
*
28+
* @return \Swift_Transport
29+
*
30+
* @throws \InvalidArgumentException if the scheme is not a built-in Swiftmailer transport
31+
*/
32+
public static function createTransport(array $options, RequestContext $requestContext, \Swift_Events_EventDispatcher $eventDispatcher)
33+
{
34+
$transport = static::resolveOptions($options);
35+
36+
if ('smtp' === $transport) {
37+
$smtpAuthHandler = new \Swift_Transport_Esmtp_AuthHandler([
38+
new \Swift_Transport_Esmtp_Auth_CramMd5Authenticator(),
39+
new \Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
40+
new \Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
41+
]);
42+
$smtpAuthHandler->setUsername($options['user']);
43+
$smtpAuthHandler->setPassword($options['pass']);
44+
$smtpAuthHandler->setAuthMode($options['auth_mode']);
45+
46+
$transport = new \Swift_Transport_EsmtpTransport(
47+
new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
48+
array($smtpAuthHandler),
49+
$eventDispatcher
50+
);
51+
$transport->setHost($options['host']);
52+
$transport->setPort($options['port']);
53+
$transport->setEncryption($options['encryption']);
54+
$transport->setTimeout($options['timeout']);
55+
$transport->setSourceIp($options['source_ip']);
56+
57+
(new SmtpTransportConfigurator(null, $requestContext))->configure($transport);
58+
} elseif ('sendmail' === $transport) {
59+
$transport = new \Swift_Transport_SendmailTransport(
60+
new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
61+
$eventDispatcher
62+
);
63+
64+
(new SmtpTransportConfigurator(null, $requestContext))->configure($transport);
65+
} elseif ('mail' === $transport) {
66+
$transport = new \Swift_Transport_MailTransport(
67+
new \Swift_Transport_SimpleMailInvoker(),
68+
$eventDispatcher
69+
);
70+
} elseif ('null' === $transport) {
71+
$transport = new \Swift_Transport_NullTransport($eventDispatcher);
72+
} else {
73+
throw new \InvalidArgumentException(sprintf('Not a built-in Swiftmailer transport: %s.', $options['dsn']));
74+
}
75+
76+
return $transport;
77+
}
78+
79+
/**
80+
* @param array $options
81+
*
82+
* @return string transport
83+
*/
84+
public static function resolveOptions(array &$options)
85+
{
86+
if (null === $options['transport']) {
87+
$transport = 'null';
88+
} elseif ('gmail' === $options['transport']) {
89+
$options['encryption'] = 'ssl';
90+
$options['auth_mode'] = 'login';
91+
$options['host'] = 'smtp.gmail.com';
92+
$transport = 'smtp';
93+
} else {
94+
$transport = $options['transport'];
95+
}
96+
97+
if (isset($options['dsn'])) {
98+
$parts = parse_url($options['dsn']);
99+
if (!empty($parts['scheme'])) {
100+
$transport = $parts['scheme'];
101+
}
102+
103+
if (!empty($parts['user'])) {
104+
$options['username'] = $parts['user'];
105+
}
106+
if (!empty($parts['pass'])) {
107+
$options['password'] = $parts['pass'];
108+
}
109+
if (!empty($parts['host'])) {
110+
$options['host'] = $parts['host'];
111+
}
112+
if (!empty($parts['port'])) {
113+
$options['port'] = $parts['port'];
114+
}
115+
if (!empty($parts['query'])) {
116+
$query = [];
117+
parse_str($parts['query'], $query);
118+
if (!empty($query['encryption'])) {
119+
$options['encryption'] = $query['encryption'];
120+
}
121+
if (!empty($query['auth_mode'])) {
122+
$options['auth_mode'] = $query['auth_mode'];
123+
}
124+
if (!empty($query['timeout'])) {
125+
$options['timeout'] = $query['timeout'];
126+
}
127+
if (!empty($query['source_ip'])) {
128+
$options['source_ip'] = $query['source_ip'];
129+
}
130+
if (!empty($query['local_domain'])) {
131+
$options['local_domain'] = $query['local_domain'];
132+
}
133+
}
134+
}
135+
136+
return $transport;
137+
}
138+
}

0 commit comments

Comments
 (0)