-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZenstruckBackupExtension.php
129 lines (106 loc) · 4.32 KB
/
ZenstruckBackupExtension.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
<?php
namespace Zenstruck\BackupBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Zenstruck\BackupBundle\DependencyInjection\Factory\Factory;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class ZenstruckBackupExtension extends Extension
{
/**
* @noinspection PhpUnhandledExceptionInspection
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$loader->load('destinations.xml');
$loader->load('namers.xml');
$loader->load('processors.xml');
$loader->load('sources.xml');
$abstractProfile = $container->getDefinition('zenstruck_backup.abstract_profile');
$abstractProfile->setFactory([new Reference('zenstruck_backup.profile_builder'), 'create']);
$container->setDefinition('zenstruck_backup.abstract_profile', $abstractProfile);
foreach ($config['sources'] as $name => $source) {
\reset($source);
$configuration
->getSourceFactory(\key($source))
->create($container, $name, \reset($source))
;
}
foreach ($config['namers'] as $name => $namer) {
\reset($namer);
$configuration
->getNamerFactory(\key($namer))
->create($container, $name, \reset($namer))
;
}
foreach ($config['processors'] as $name => $processor) {
\reset($processor);
$configuration
->getProcessorFactory(\key($processor))
->create($container, $name, \reset($processor))
;
}
foreach ($config['destinations'] as $name => $destination) {
\reset($destination);
$configuration
->getDestinationFactory(\key($destination))
->create($container, $name, \reset($destination))
;
}
foreach ($config['profiles'] as $name => $profile) {
$definition = $container->setDefinition(
\sprintf('zenstruck_backup.profile.%s', $name),
new ChildDefinition('zenstruck_backup.abstract_profile')
);
$definition
->replaceArgument(0, $name)
->replaceArgument(1, $profile['scratch_dir'])
->replaceArgument(2, $profile['processor'])
->replaceArgument(3, $profile['namer'])
->replaceArgument(4, $profile['sources'])
->replaceArgument(5, $profile['destinations'])
->addTag('zenstruck_backup.profile')
;
}
}
/**
* @throws \Exception
*/
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
{
$tempContainer = new ContainerBuilder();
$loader = new Loader\XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('factories.xml');
return new Configuration(
$this->getServices('zenstruck_backup.namer_factory', $tempContainer),
$this->getServices('zenstruck_backup.processor_factory', $tempContainer),
$this->getServices('zenstruck_backup.source_factory', $tempContainer),
$this->getServices('zenstruck_backup.destination_factory', $tempContainer)
);
}
/**
* @return Factory[]
*
* @throws \Exception
* @throws \Exception
*/
private function getServices(string $tag, ContainerBuilder $container): array
{
$services = [];
foreach (\array_keys($container->findTaggedServiceIds($tag)) as $id) {
/** @var Factory $factory */
$factory = $container->get($id);
$services[$factory->getName()] = $factory;
}
return $services;
}
}