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