-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependencies.php
143 lines (123 loc) · 5.9 KB
/
dependencies.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
use DI\Container;
use DI\ContainerBuilder;
use Mailer\Application\Auth;
use Mailer\Application\Email\Config;
use Mailer\Application\HttpModels\SendRequest;
use Mailer\Application\Messenger\Handler\SendRequestConsumer;
use Mailer\Application\Validator;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisTransportFactory;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
use Symfony\Component\Messenger\RoutableMessageBus;
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\TransportFactory;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Extra\CssInliner\CssInlinerExtension;
use Twig\Extra\Intl\IntlExtension;
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
Auth\SendAuthMiddleware::class => static function (ContainerInterface $c): Auth\SendAuthMiddleware {
return new Auth\SendAuthMiddleware($c->get(LoggerInterface::class));
},
Config::class => static function (ContainerInterface $c): Config {
return new Config($c->get('settings')['emails']);
},
LoggerInterface::class => static function (ContainerInterface $c) {
$settings = $c->get('settings');
$loggerSettings = $settings['logger'];
$logger = new Logger($loggerSettings['name']);
$processor = new UidProcessor();
$logger->pushProcessor($processor);
$handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']);
$logger->pushHandler($handler);
return $logger;
},
MessageBusInterface::class => static function (ContainerInterface $c): MessageBusInterface {
return new MessageBus([
new SendMessageMiddleware(new SendersLocator(
[SendRequest::class => [TransportInterface::class]],
$c,
)),
new HandleMessageMiddleware(new HandlersLocator(
[SendRequest::class => [$c->get(SendRequestConsumer::class)]],
)),
]);
},
RoutableMessageBus::class => static function (ContainerInterface $c): RoutableMessageBus {
$busContainer = new Container();
$busContainer->set('email', $c->get(MessageBusInterface::class)); // async, currently via SQS or Redis
return new RoutableMessageBus($busContainer);
},
SendRequestConsumer::class => static function (ContainerInterface $c): SendRequestConsumer {
return new SendRequestConsumer(
$c->get('settings')['appEnv'],
$c->get(Config::class),
$c->get(LoggerInterface::class),
$c->get(MailerInterface::class),
$c->get(Twig\Environment::class),
$c->get(Validator\SendRequest::class),
);
},
SerializerInterface::class => static function (ContainerInterface $c): SerializerInterface {
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
return new Serializer($normalizers, $encoders);
},
MailerInterface::class => static function (ContainerInterface $c): MailerInterface {
// Timeout comes from php.ini's `default_socket_timeout` – 8 seconds in our Docker base.
// See https://symfony.com/doc/current/mailer.html#using-a-3rd-party-transport
$transport = (new EsmtpTransportFactory())->create(
Dsn::fromString($c->get('settings')['mailer']['dsn']),
);
return new Symfony\Component\Mailer\Mailer($transport);
},
TransportInterface::class => static function (ContainerInterface $c): TransportInterface {
$transportFactory = new TransportFactory([
new AmazonSqsTransportFactory(),
new RedisTransportFactory(),
]);
return $transportFactory->createTransport(
getenv('MESSENGER_TRANSPORT_DSN'),
[],
new PhpSerializer(),
);
},
Twig\Environment::class => static function (ContainerInterface $c): Twig\Environment {
$twigSettings = $c->get('settings')['twig'];
$loader = new Twig\Loader\FilesystemLoader($twigSettings['templatePath']);
$env = new Twig\Environment($loader, [
'cache' => $twigSettings['cachePath'],
'debug' => $twigSettings['debug'],
]);
$env->addExtension(new CssInlinerExtension());
$env->addExtension(new IntlExtension());
return $env;
},
Validator\SendRequest::class => static function (ContainerInterface $c): Validator\SendRequest {
return new Validator\SendRequest(
$c->get(Config::class),
$c->get(Twig\Environment::class),
$c->get('settings')['twig'],
);
},
]);
};