|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SfCod\EmailEngineBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use SfCod\EmailEngineBundle\Mailer\Mailer; |
|
7
|
|
|
use SfCod\EmailEngineBundle\Mailer\TemplateManager; |
|
8
|
|
|
use SfCod\EmailEngineBundle\Template\ParametersAwareInterface; |
|
9
|
|
|
use SfCod\EmailEngineBundle\Template\Params\ParameterResolver; |
|
10
|
|
|
use SfCod\EmailEngineBundle\Template\Params\ParameterResolverInterface; |
|
11
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class EmailEngineExtension |
|
20
|
|
|
* |
|
21
|
|
|
* @author Virchenko Maksim <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @package SfCod\EmailEngineBundle\DependencyInjection |
|
24
|
|
|
*/ |
|
25
|
|
|
class EmailEngineExtension extends Extension |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Loads a specific configuration. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $configs |
|
31
|
|
|
* @param ContainerBuilder $container |
|
32
|
|
|
*/ |
|
33
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
34
|
|
|
{ |
|
35
|
|
|
$configuration = new EmailEngineConfiguration(); |
|
36
|
|
|
|
|
37
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
38
|
|
|
|
|
39
|
|
|
$this->createSenders($config, $container); |
|
40
|
|
|
$this->createTemplates($config, $container); |
|
41
|
|
|
$this->createResolver($config, $container); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get extension alias |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getAlias() |
|
50
|
|
|
{ |
|
51
|
|
|
return 'sfcod_email_engine'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create parameter resolver |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $config |
|
58
|
|
|
* @param ContainerBuilder $container |
|
59
|
|
|
*/ |
|
60
|
|
|
private function createResolver(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$resolver = new Definition(ParameterResolverInterface::class); |
|
63
|
|
|
$resolver->setPublic(true); |
|
64
|
|
|
$resolver->setClass(ParameterResolver::class); |
|
65
|
|
|
$resolver->setArguments([ |
|
66
|
|
|
new Reference(ContainerInterface::class), |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
$container->setDefinition(ParameterResolverInterface::class, $resolver); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create senders |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
* @param ContainerBuilder $container |
|
77
|
|
|
*/ |
|
78
|
|
|
private function createSenders(array $config, ContainerBuilder $container) |
|
79
|
|
|
{ |
|
80
|
|
|
$configuration = []; |
|
81
|
|
|
$mainSender = $this->getSender($config['main_sender'], $config); |
|
82
|
|
|
|
|
83
|
|
|
if (isset($mainSender['chain'])) { |
|
84
|
|
|
foreach ($mainSender['chain']['senders'] as $sender) { |
|
85
|
|
|
$configuration[$sender] = $this->getSender($sender, $config); |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
$configuration[$config['main_sender']] = $mainSender; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$senders = []; |
|
92
|
|
|
$repositories = []; |
|
93
|
|
|
foreach ($configuration as $senderName => $senderConfig) { |
|
94
|
|
|
if (false === isset($senderConfig['sender'], $senderConfig['repository']) || |
|
95
|
|
|
false === isset($senderConfig['sender']['class'], $senderConfig['repository']['class'])) { |
|
96
|
|
|
throw new InvalidConfigurationException(sprintf('"sender" and "repository" must be defined in "%s" sender.', $senderName)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$sender = new Definition($senderConfig['sender']['class']); |
|
100
|
|
|
$sender |
|
101
|
|
|
->setPublic(true) |
|
102
|
|
|
->setAutoconfigured(true) |
|
103
|
|
|
->setAutowired(true); |
|
104
|
|
|
|
|
105
|
|
|
$repository = new Definition($senderConfig['repository']['class']); |
|
106
|
|
|
$repository |
|
107
|
|
|
->setPublic(true) |
|
108
|
|
|
->setAutoconfigured(true) |
|
109
|
|
|
->setAutowired(true); |
|
110
|
|
|
|
|
111
|
|
|
$container->addDefinitions([ |
|
112
|
|
|
$senderConfig['repository']['class'] => $repository, |
|
113
|
|
|
$senderConfig['sender']['class'] => $sender, |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
// Prepare senders and repositories for injecting into mailer |
|
117
|
|
|
$senders[$senderConfig['sender']['class']] = $sender; |
|
118
|
|
|
$repositories[$senderConfig['repository']['class']] = $repository; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$mailer = new Definition(Mailer::class); |
|
122
|
|
|
$mailer |
|
123
|
|
|
->setPublic(true) |
|
124
|
|
|
->setArguments([ |
|
125
|
|
|
new Reference(ParameterResolverInterface::class), |
|
126
|
|
|
new Reference(LoggerInterface::class), |
|
127
|
|
|
]) |
|
128
|
|
|
->addMethodCall('setConfiguration', [$configuration]) |
|
129
|
|
|
->addMethodCall('setSenders', [$senders]) |
|
130
|
|
|
->addMethodCall('setRepositories', [$repositories]); |
|
131
|
|
|
|
|
132
|
|
|
$container->setDefinition(Mailer::class, $mailer); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get sender from senders config |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $sender |
|
139
|
|
|
* @param array $config |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
private function getSender(string $sender, array $config): array |
|
144
|
|
|
{ |
|
145
|
|
|
if (false === isset($config['senders'][$sender])) { |
|
146
|
|
|
throw new InvalidConfigurationException(sprintf('Main sender "%s" does not exist in senders "%s".', $sender, json_encode(array_keys($config['senders'])))); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $config['senders'][$sender]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Create templates |
|
154
|
|
|
* |
|
155
|
|
|
* @param array $config |
|
156
|
|
|
* @param ContainerBuilder $container |
|
157
|
|
|
*/ |
|
158
|
|
|
private function createTemplates(array $config, ContainerBuilder $container) |
|
159
|
|
|
{ |
|
160
|
|
|
foreach ($config['templates'] as $template) { |
|
161
|
|
|
if (in_array(ParametersAwareInterface::class, class_implements($template))) { |
|
162
|
|
|
/** @var ParametersAwareInterface $template */ |
|
163
|
|
|
foreach ($template::listParameters() as $parameter) { |
|
164
|
|
|
$definition = new Definition($parameter); |
|
|
|
|
|
|
165
|
|
|
$definition |
|
166
|
|
|
->setPublic(true) |
|
167
|
|
|
->setAutowired(true) |
|
168
|
|
|
->setAutoconfigured(true) |
|
169
|
|
|
->addTag(sprintf('%s.parameter', $template)); |
|
170
|
|
|
|
|
171
|
|
|
$container->setDefinition($parameter, $definition); |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$templateManager = new Definition(TemplateManager::class); |
|
177
|
|
|
$templateManager |
|
178
|
|
|
->setPublic(true) |
|
179
|
|
|
->addArgument($config['templates']); |
|
180
|
|
|
|
|
181
|
|
|
$container->setDefinition(TemplateManager::class, $templateManager); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.