11
11
12
12
namespace Symfony \Bundle \SwiftmailerBundle \DependencyInjection ;
13
13
14
+ use Symfony \Component \DependencyInjection \Definition ;
14
15
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
15
16
use Symfony \Component \DependencyInjection \ContainerInterface ;
16
17
use Symfony \Component \DependencyInjection \ChildDefinition ;
@@ -66,63 +67,57 @@ public function load(array $configs, ContainerBuilder $container)
66
67
67
68
protected function configureMailer ($ name , array $ mailer , ContainerBuilder $ container , $ isDefaultMailer = false )
68
69
{
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 );
76
98
} else {
77
- $ transport = $ mailer ['transport ' ];
78
- }
99
+ $ transport = SwiftmailerTransportFactory::resolveOptions ($ mailer );
79
100
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 );
85
102
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 );
107
108
}
108
- }
109
- unset($ mailer ['url ' ]);
110
109
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
+ }
112
113
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 ;
119
117
120
- if (empty ($ mailer ['port ' ])) {
121
- $ mailer ['port ' ] = 'ssl ' === $ mailer ['encryption ' ] ? 465 : 25 ;
118
+ $ this ->configureMailerTransport ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
122
119
}
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 );
126
121
$ this ->configureMailerSenderAddress ($ name , $ mailer , $ container , $ isDefaultMailer );
127
122
$ this ->configureMailerAntiFlood ($ name , $ mailer , $ container , $ isDefaultMailer );
128
123
$ this ->configureMailerDeliveryAddress ($ name , $ mailer , $ container , $ isDefaultMailer );
@@ -144,11 +139,6 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild
144
139
$ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.smtp.%s ' , $ name , $ key ), $ mailer [$ key ]);
145
140
}
146
141
147
- $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.transport.eventdispatcher.abstract ' );
148
- $ container
149
- ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name ), $ definitionDecorator )
150
- ;
151
-
152
142
if ('smtp ' === $ transport ) {
153
143
$ authDecorator = $ this ->createChildDefinition ('swiftmailer.transport.authhandler.abstract ' );
154
144
$ container
@@ -278,10 +268,6 @@ protected function configureMailerSpool($name, array $mailer, ContainerBuilder $
278
268
))
279
269
;
280
270
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
- }
285
271
$ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport.real ' , $ name ), $ transport );
286
272
$ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport ' , $ name ), sprintf ('swiftmailer.mailer.%s.transport.spool ' , $ name ));
287
273
$ container ->setParameter (sprintf ('swiftmailer.mailer.%s.spool.enabled ' , $ name ), true );
0 commit comments