|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Swift_Transport; |
|
8
|
|
|
use Swift_Plugins_LoggerPlugin; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Yiisoft\Aliases\Aliases; |
|
13
|
|
|
use Yiisoft\Di\Container; |
|
14
|
|
|
use Yiisoft\Di\Support\ServiceProvider; |
|
15
|
|
|
use Yiisoft\Mailer\Composer; |
|
16
|
|
|
use Yiisoft\Mailer\FileMailer; |
|
17
|
|
|
use Yiisoft\Mailer\MailerInterface; |
|
18
|
|
|
use Yiisoft\Mailer\MessageFactory; |
|
19
|
|
|
use Yiisoft\Mailer\SwiftMailer\Logger; |
|
20
|
|
|
use Yiisoft\Mailer\SwiftMailer\Mailer; |
|
21
|
|
|
use Yiisoft\Mailer\SwiftMailer\Message; |
|
22
|
|
|
use Yiisoft\View\WebView; |
|
23
|
|
|
|
|
24
|
|
|
final class MailerInterfaceProvider extends ServiceProvider |
|
25
|
|
|
{ |
|
26
|
|
|
private string $composerPath; |
|
27
|
|
|
private bool $writeToFiles; |
|
28
|
|
|
private string $writeToFilesPath; |
|
29
|
|
|
|
|
30
|
8 |
|
public function __construct( |
|
31
|
|
|
string $composerPath = '@runtime/mail', |
|
32
|
|
|
bool $writeToFiles = false, |
|
33
|
|
|
string $writeToFilesPath = '@runtime/mail' |
|
34
|
|
|
) { |
|
35
|
8 |
|
$this->composerPath = $composerPath; |
|
36
|
8 |
|
$this->writeToFiles = $writeToFiles; |
|
37
|
8 |
|
$this->writeToFilesPath = $writeToFilesPath; |
|
38
|
8 |
|
} |
|
39
|
|
|
|
|
40
|
8 |
|
public function register(Container $container): void |
|
41
|
|
|
{ |
|
42
|
|
|
$container->set(MailerInterface::class, function (ContainerInterface $container) { |
|
43
|
6 |
|
$aliases = $container->get(Aliases::class); |
|
44
|
6 |
|
$logger = $container->get(LoggerInterface::class); |
|
45
|
6 |
|
$eventDispatcher = $container->get(EventDispatcherInterface::class); |
|
46
|
6 |
|
$view = $container->get(WebView::class); |
|
47
|
|
|
|
|
48
|
6 |
|
$messageFactory = new MessageFactory(Message::class); |
|
49
|
|
|
|
|
50
|
6 |
|
$composer = new Composer($view, $aliases->get($this->composerPath)); |
|
51
|
|
|
|
|
52
|
6 |
|
if ($this->writeToFiles) { |
|
53
|
6 |
|
return new FileMailer( |
|
54
|
|
|
$messageFactory, |
|
55
|
|
|
$composer, |
|
56
|
|
|
$eventDispatcher, |
|
57
|
|
|
$logger, |
|
58
|
6 |
|
$aliases->get($this->writeToFilesPath) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$transport = $container->get(Swift_Transport::class); |
|
63
|
|
|
$mailer = new Mailer($messageFactory, $composer, $eventDispatcher, $logger, $transport); |
|
64
|
|
|
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin(new Logger($logger))); |
|
65
|
|
|
|
|
66
|
|
|
return $mailer; |
|
67
|
8 |
|
}); |
|
68
|
8 |
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|