1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace FH\Bundle\MailerBundle\DependencyInjection; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use FH\Bundle\MailerBundle\Composer\ComposerIdentifiers; |
8
|
|
|
use FH\Bundle\MailerBundle\Composer\EmailComposer; |
9
|
|
|
use FH\Bundle\MailerBundle\Composer\TemplatedEmailComposer; |
10
|
|
|
use FH\Bundle\MailerBundle\Email\MessageOptions; |
11
|
|
|
use Symfony\Component\Config\FileLocator; |
12
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
17
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
18
|
|
|
|
19
|
|
|
final class FHMailerExtension extends ConfigurableExtension |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @throws Exception |
23
|
|
|
*/ |
24
|
2 |
|
public function loadInternal(array $configs, ContainerBuilder $container): void |
25
|
|
|
{ |
26
|
2 |
|
$fileLoader = (new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'))); |
27
|
2 |
|
$fileLoader->load('message_composer.yaml'); |
28
|
2 |
|
$fileLoader->load('transport.yaml'); |
29
|
|
|
|
30
|
2 |
|
foreach ($configs[ComposerIdentifiers::TEMPLATED_EMAIL] as $name => $messageOptions) { |
31
|
1 |
|
$composerId = $this->createComposerId($name, ComposerIdentifiers::TEMPLATED_EMAIL); |
32
|
|
|
|
33
|
1 |
|
$this->registerTemplatedEmailComposer($container, $composerId, $messageOptions); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
foreach ($configs[ComposerIdentifiers::EMAIL] as $name => $messageOptions) { |
37
|
1 |
|
$composerId = $this->createComposerId($name, ComposerIdentifiers::EMAIL); |
38
|
|
|
|
39
|
1 |
|
$this->registerEmailComposer($container, $composerId, $messageOptions); |
40
|
|
|
} |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ContainerBuilder $container |
45
|
|
|
* @param string $composerId |
46
|
|
|
* @param string[] $messageOptions |
47
|
|
|
*/ |
48
|
1 |
|
private function registerTemplatedEmailComposer( |
49
|
|
|
ContainerBuilder $container, |
50
|
|
|
string $composerId, |
51
|
|
|
array $messageOptions |
52
|
|
|
): void { |
53
|
1 |
|
$this->registerComposer($container, $messageOptions, TemplatedEmailComposer::class, $composerId); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ContainerBuilder $container |
58
|
|
|
* @param string $composerId |
59
|
|
|
* @param string[] $messageOptions |
60
|
|
|
*/ |
61
|
1 |
|
private function registerEmailComposer( |
62
|
|
|
ContainerBuilder $container, |
63
|
|
|
string $composerId, |
64
|
|
|
array $messageOptions |
65
|
|
|
): void { |
66
|
1 |
|
$this->registerComposer($container, $messageOptions, EmailComposer::class, $composerId); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ContainerBuilder $container |
71
|
|
|
* @param string[] $messageOptions |
72
|
|
|
* @param string $composerClass |
73
|
|
|
* @param string $composerId |
74
|
|
|
*/ |
75
|
1 |
|
private function registerComposer( |
76
|
|
|
ContainerBuilder $container, |
77
|
|
|
array $messageOptions, |
78
|
|
|
string $composerClass, |
79
|
|
|
string $composerId |
80
|
|
|
): void { |
81
|
1 |
|
$optionsId = "$composerId._message_options"; |
82
|
1 |
|
$container->setDefinition($optionsId, $this->createMessageOptionsDefinition($messageOptions)); |
83
|
|
|
|
84
|
1 |
|
$composerDefinition = new ChildDefinition($composerClass); |
85
|
1 |
|
$composerDefinition->setArgument('$messageOptions', new Reference($optionsId)); |
86
|
|
|
|
87
|
1 |
|
$container->setDefinition($composerId, $composerDefinition); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
private function createMessageOptionsDefinition(array $options): Definition |
91
|
|
|
{ |
92
|
1 |
|
$definition = new Definition(MessageOptions::class, [$options]); |
93
|
1 |
|
$definition->setFactory([MessageOptions::class, 'fromArray']); |
94
|
|
|
|
95
|
1 |
|
return $definition; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
private function createComposerId(string $composerId, string $composer): string |
99
|
|
|
{ |
100
|
1 |
|
return "fh_mailer.composer.$composer.$composerId"; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|