14
14
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
15
15
use Symfony \Component \DependencyInjection \ContainerInterface ;
16
16
use Symfony \Component \DependencyInjection \ChildDefinition ;
17
+ use Symfony \Component \DependencyInjection \Definition ;
17
18
use Symfony \Component \DependencyInjection \DefinitionDecorator ;
18
19
use Symfony \Component \DependencyInjection \Reference ;
19
20
use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
@@ -66,68 +67,60 @@ 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 ' ;
76
- } else {
77
- $ transport = $ mailer ['transport ' ];
78
- }
79
-
80
- if (null !== $ mailer ['url ' ]) {
81
- $ parts = parse_url ($ mailer ['url ' ]);
82
- if (!empty ($ parts ['scheme ' ])) {
83
- $ transport = $ parts ['scheme ' ];
84
- }
70
+ $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.transport.eventdispatcher.abstract ' );
71
+ $ container
72
+ ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name ), $ definitionDecorator )
73
+ ;
85
74
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
- }
75
+ if (method_exists ($ container , 'resolveEnvPlaceholders ' )) {
76
+ $ options = array ();
77
+ $ envVariablesAllowed = array ('transport ' , 'url ' , 'username ' , 'password ' , 'host ' , 'port ' , 'timeout ' , 'source_ip ' , 'local_domain ' , 'encryption ' , 'auth_mode ' );
78
+ foreach ($ envVariablesAllowed as $ key ) {
79
+ $ container ->resolveEnvPlaceholders ($ mailer [$ key ], null , $ usedEnvs );
80
+ $ options [$ key ] = $ mailer [$ key ];
107
81
}
82
+ } else {
83
+ $ usedEnvs = false ;
108
84
}
109
- unset($ mailer ['url ' ]);
110
-
111
- $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.name ' , $ name ), $ transport );
85
+ if ($ usedEnvs ) {
86
+ $ transportId = sprintf ('swiftmailer.mailer.%s.transport.dynamic ' , $ name );
87
+ $ definitionDecorator = new Definition ('\Swift_Transport ' );
88
+ $ definitionDecorator ->setFactory (array ('Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerTransportFactory ' , 'createTransport ' ));
89
+ $ definitionDecorator ->setArguments (array (
90
+ $ options ,
91
+ new Reference ('router.request_context ' ),
92
+ new Reference (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name )),
93
+ ));
94
+ $ container ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.dynamic ' , $ name ), $ definitionDecorator );
95
+ $ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport ' , $ name ), $ transportId );
96
+
97
+ $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.mailer.abstract ' );
98
+ $ container
99
+ ->setDefinition (sprintf ('swiftmailer.mailer.%s ' , $ name ), $ definitionDecorator )
100
+ ->replaceArgument (0 , new Reference (sprintf ('swiftmailer.mailer.%s.transport ' , $ name )))
101
+ ;
112
102
113
- if (isset ($ mailer ['disable_delivery ' ]) && $ mailer ['disable_delivery ' ]) {
114
- $ transport = 'null ' ;
115
- $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.delivery.enabled ' , $ name ), false );
103
+ $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.name ' , $ name ), 'dynamic ' );
116
104
} else {
117
- $ container -> setParameter ( sprintf ( ' swiftmailer. mailer.%s.delivery.enabled ' , $ name ), true );
118
- }
105
+ $ mailer = SwiftmailerTransportFactory:: resolveOptions ( $ mailer );
106
+ $ transport = $ mailer [ ' transport ' ];
119
107
120
- if (empty ($ mailer ['port ' ])) {
121
- $ mailer ['port ' ] = 'ssl ' === $ mailer ['encryption ' ] ? 465 : 25 ;
122
- }
108
+ $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.name ' , $ name ), $ transport );
123
109
124
- $ this ->configureMailerTransport ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
125
- $ this ->configureMailerSpool ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
110
+ $ transportId = in_array ($ transport , array ('smtp ' , 'sendmail ' , 'null ' ))
111
+ ? sprintf ('swiftmailer.mailer.%s.transport.%s ' , $ name , $ transport )
112
+ : $ transport ;
113
+
114
+ $ this ->configureMailerTransport ($ name , $ mailer , $ container , $ transport , $ isDefaultMailer );
115
+ }
116
+ $ this ->configureMailerSpool ($ name , $ mailer , $ container , $ transportId , $ isDefaultMailer );
126
117
$ this ->configureMailerSenderAddress ($ name , $ mailer , $ container , $ isDefaultMailer );
127
118
$ this ->configureMailerAntiFlood ($ name , $ mailer , $ container , $ isDefaultMailer );
128
119
$ this ->configureMailerDeliveryAddress ($ name , $ mailer , $ container , $ isDefaultMailer );
129
120
$ this ->configureMailerLogging ($ name , $ mailer , $ container , $ isDefaultMailer );
130
121
122
+ $ container ->setParameter (sprintf ('swiftmailer.mailer.%s.delivery.enabled ' , $ name ), empty ($ mailer ['disable_delivery ' ]));
123
+
131
124
// alias
132
125
if ($ isDefaultMailer ) {
133
126
$ container ->setAlias ('swiftmailer.mailer ' , sprintf ('swiftmailer.mailer.%s ' , $ name ));
@@ -144,11 +137,6 @@ protected function configureMailerTransport($name, array $mailer, ContainerBuild
144
137
$ container ->setParameter (sprintf ('swiftmailer.mailer.%s.transport.smtp.%s ' , $ name , $ key ), $ mailer [$ key ]);
145
138
}
146
139
147
- $ definitionDecorator = $ this ->createChildDefinition ('swiftmailer.transport.eventdispatcher.abstract ' );
148
- $ container
149
- ->setDefinition (sprintf ('swiftmailer.mailer.%s.transport.eventdispatcher ' , $ name ), $ definitionDecorator )
150
- ;
151
-
152
140
if ('smtp ' === $ transport ) {
153
141
$ authDecorator = $ this ->createChildDefinition ('swiftmailer.transport.authhandler.abstract ' );
154
142
$ container
@@ -278,10 +266,6 @@ protected function configureMailerSpool($name, array $mailer, ContainerBuilder $
278
266
))
279
267
;
280
268
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
269
$ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport.real ' , $ name ), $ transport );
286
270
$ container ->setAlias (sprintf ('swiftmailer.mailer.%s.transport ' , $ name ), sprintf ('swiftmailer.mailer.%s.transport.spool ' , $ name ));
287
271
$ container ->setParameter (sprintf ('swiftmailer.mailer.%s.spool.enabled ' , $ name ), true );
0 commit comments