3
3
namespace BeyondCode \HeloLaravel ;
4
4
5
5
use Illuminate \Contracts \Mail \Mailer as MailerContract ;
6
- use Illuminate \Support \Arr ;
7
6
use Illuminate \Support \Facades \Mail ;
8
7
use Illuminate \Support \Facades \View ;
9
8
use Illuminate \Support \ServiceProvider ;
10
- use Illuminate \Support \Str ;
11
9
use Swift_Mailer ;
12
10
13
11
class HeloLaravelServiceProvider extends ServiceProvider
14
12
{
13
+ use CreatesMailers;
14
+
15
15
/**
16
16
* Bootstrap the application services.
17
17
*/
@@ -21,11 +21,8 @@ public function boot()
21
21
return ;
22
22
}
23
23
24
- $ instance = app ()->make (Mailer::class);
25
-
26
- Mail::swap ($ instance );
27
24
28
- app ()-> instance (MailerContract::class, $ instance );
25
+ $ this -> bootMailable ( );
29
26
30
27
if ($ this ->app ->runningInConsole ()) {
31
28
View::addNamespace ('helo ' , __DIR__ . '/../resources/views ' );
@@ -50,7 +47,7 @@ public function register()
50
47
$ this ->mergeConfigFrom (__DIR__ .'/../config/helo.php ' , 'helo ' );
51
48
52
49
$ this ->app ->singleton (Mailer::class, function ($ app ) {
53
- $ version = ( int ) Str:: of ( $ app ->version ())-> explode ( ' . ' )-> first ( );
50
+ $ version = $ this ->version ($ app );
54
51
55
52
if ($ version < 7 ) {
56
53
return $ this ->createLaravel6Mailer ($ app );
@@ -63,114 +60,25 @@ public function register()
63
60
});
64
61
}
65
62
66
- protected function createLaravel6Mailer ( $ app )
63
+ protected function bootMailable ( )
67
64
{
68
- $ config = $ this ->getConfig ();
65
+ if ($ this ->version () < 10 ) {
66
+ $ instance = app ()->make (Mailer::class);
69
67
70
- // Once we have create the mailer instance, we will set a container instance
71
- // on the mailer. This allows us to resolve mailer classes via containers
72
- // for maximum testability on said classes instead of passing Closures.
73
- $ mailer = new Mailer (
74
- $ app ['view ' ], $ app ['swift.mailer ' ], $ app ['events ' ]
75
- );
68
+ Mail::swap ($ instance );
76
69
77
- if ($ app ->bound ('queue ' )) {
78
- $ mailer ->setQueue ($ app ['queue ' ]);
79
- }
80
-
81
- // Next we will set all of the global addresses on this mailer, which allows
82
- // for easy unification of all "from" addresses as well as easy debugging
83
- // of sent messages since they get be sent into a single email address.
84
- foreach (['from ' , 'reply_to ' , 'to ' ] as $ type ) {
85
- $ this ->setGlobalAddress ($ mailer , $ config , $ type );
86
- }
87
-
88
- return $ mailer ;
89
- }
90
-
91
- protected function createLaravel7Mailer ($ app )
92
- {
93
- $ defaultDriver = $ app ['mail.manager ' ]->getDefaultDriver ();
94
- $ config = $ this ->getConfig ($ defaultDriver );
95
-
96
- // Laravel 7 no longer bindes the swift.mailer:
97
- $ swiftMailer = new Swift_Mailer ($ app ['mail.manager ' ]->createTransport ($ config ));
98
-
99
- // Once we have create the mailer instance, we will set a container instance
100
- // on the mailer. This allows us to resolve mailer classes via containers
101
- // for maximum testability on said classes instead of passing Closures.
102
- $ mailer = new Laravel7Mailer (
103
- 'smtp ' , $ app ['view ' ], $ swiftMailer , $ app ['events ' ]
104
- );
105
-
106
- if ($ app ->bound ('queue ' )) {
107
- $ mailer ->setQueue ($ app ['queue ' ]);
108
- }
109
-
110
- // Next we will set all of the global addresses on this mailer, which allows
111
- // for easy unification of all "from" addresses as well as easy debugging
112
- // of sent messages since they get be sent into a single email address.
113
- foreach (['from ' , 'reply_to ' , 'to ' , 'return_path ' ] as $ type ) {
114
- $ this ->setGlobalAddress ($ mailer , $ config , $ type );
115
- }
116
-
117
- return $ mailer ;
118
- }
119
-
120
- protected function createLaravel9Mailer ($ app )
121
- {
122
- $ defaultDriver = $ app ['mail.manager ' ]->getDefaultDriver ();
123
- $ config = $ this ->getConfig ($ defaultDriver );
124
-
125
- // We get Symfony Transport from Laravel 9 mailer
126
- $ symfonyTransport = $ app ['mail.manager ' ]->getSymfonyTransport ();
127
-
128
- // Once we have create the mailer instance, we will set a container instance
129
- // on the mailer. This allows us to resolve mailer classes via containers
130
- // for maximum testability on said classes instead of passing Closures.
131
- $ mailer = new Laravel7Mailer (
132
- 'smtp ' , $ app ['view ' ], $ symfonyTransport , $ app ['events ' ]
133
- );
134
-
135
- if ($ app ->bound ('queue ' )) {
136
- $ mailer ->setQueue ($ app ['queue ' ]);
137
- }
138
-
139
- // Next we will set all of the global addresses on this mailer, which allows
140
- // for easy unification of all "from" addresses as well as easy debugging
141
- // of sent messages since they get be sent into a single email address.
142
- foreach (['from ' , 'reply_to ' , 'to ' , 'return_path ' ] as $ type ) {
143
- $ this ->setGlobalAddress ($ mailer , $ config , $ type );
70
+ $ this ->app ->instance (MailerContract::class, $ instance );
71
+ } else {
72
+ Mail::swap (new MailManager ($ this ->app ));
144
73
}
145
-
146
- return $ mailer ;
147
- }
148
-
149
- protected function getConfig ($ name = 'smtp ' )
150
- {
151
- return $ this ->app ['config ' ]['mail.driver ' ]
152
- ? $ this ->app ['config ' ]['mail ' ]
153
- : $ this ->app ['config ' ]["mail.mailers. {$ name }" ];
154
74
}
155
75
156
- /**
157
- * Set a global address on the mailer by type.
158
- *
159
- * @param \Illuminate\Mail\Mailer $mailer
160
- * @param array $config
161
- * @param string $type
162
- * @return void
163
- */
164
- protected function setGlobalAddress ($ mailer , array $ config , $ type )
76
+ private function version ($ app = null ): int
165
77
{
166
- if (version_compare (app ()->version (), '7.0.0 ' , '< ' )) {
167
- $ address = Arr::get ($ config , $ type );
168
- } else {
169
- $ address = Arr::get ($ config , $ type , $ this ->app ['config ' ]['mail. ' .$ type ]);
78
+ if (! $ app ) {
79
+ $ app = $ this ->app ;
170
80
}
171
81
172
- if (is_array ($ address ) && isset ($ address ['address ' ])) {
173
- $ mailer ->{'always ' . Str::studly ($ type )}($ address ['address ' ], $ address ['name ' ]);
174
- }
82
+ return (int ) collect (explode ('. ' , $ app ->version ()))->first ();
175
83
}
176
84
}
0 commit comments