1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\BackupBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
7
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
8
|
|
|
use Zenstruck\BackupBundle\DependencyInjection\Factory\Factory; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Kevin Bond <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Configuration implements ConfigurationInterface |
14
|
|
|
{ |
15
|
|
|
private $namerFactories; |
16
|
|
|
private $processorFactories; |
17
|
|
|
private $sourceFactories; |
18
|
|
|
private $destinationFactories; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Factory[] $namerFactories |
22
|
|
|
* @param Factory[] $processorFactories |
23
|
|
|
* @param Factory[] $sourceFactories |
24
|
|
|
* @param Factory[] $destinationFactories |
25
|
|
|
*/ |
26
|
5 |
|
public function __construct(array $namerFactories, array $processorFactories, array $sourceFactories, array $destinationFactories) |
27
|
|
|
{ |
28
|
5 |
|
$this->namerFactories = $namerFactories; |
29
|
5 |
|
$this->processorFactories = $processorFactories; |
30
|
5 |
|
$this->sourceFactories = $sourceFactories; |
31
|
5 |
|
$this->destinationFactories = $destinationFactories; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
5 |
|
public function getConfigTreeBuilder() |
35
|
|
|
{ |
36
|
5 |
|
$treeBuilder = new TreeBuilder('zenstruck_backup'); |
37
|
|
|
|
38
|
|
|
// Keep compatibility with symfony/config < 4.2 |
39
|
5 |
|
if (\method_exists($treeBuilder, 'getRootNode')) { |
40
|
|
|
$rootNode = $treeBuilder->getRootNode(); |
41
|
|
|
} else { |
42
|
5 |
|
$rootNode = $treeBuilder->root('zenstruck_backup'); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
$this->addFactories($rootNode, 'namers', 'namer', $this->namerFactories); |
|
|
|
|
46
|
5 |
|
$this->addFactories($rootNode, 'processors', 'processor', $this->processorFactories); |
|
|
|
|
47
|
5 |
|
$this->addFactories($rootNode, 'sources', 'source', $this->sourceFactories); |
|
|
|
|
48
|
5 |
|
$this->addFactories($rootNode, 'destinations', 'destination', $this->destinationFactories); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$rootNode |
51
|
5 |
|
->children() |
52
|
5 |
|
->arrayNode('profiles') |
53
|
5 |
|
->useAttributeAsKey('name') |
54
|
5 |
|
->prototype('array') |
55
|
5 |
|
->children() |
56
|
5 |
|
->scalarNode('scratch_dir')->defaultValue('%kernel.cache_dir%/backup')->end() |
57
|
5 |
|
->arrayNode('sources') |
58
|
5 |
|
->isRequired() |
59
|
5 |
|
->requiresAtLeastOneElement() |
60
|
5 |
|
->prototype('scalar')->end() |
61
|
5 |
|
->beforeNormalization() |
62
|
5 |
|
->ifString() |
63
|
5 |
|
->then(function ($v) { |
64
|
1 |
|
return array($v); |
65
|
5 |
|
}) |
66
|
5 |
|
->end() |
67
|
5 |
|
->end() |
68
|
5 |
|
->scalarNode('namer')->isRequired()->end() |
69
|
5 |
|
->scalarNode('processor')->isRequired()->end() |
70
|
5 |
|
->arrayNode('destinations') |
71
|
5 |
|
->isRequired() |
72
|
5 |
|
->requiresAtLeastOneElement() |
73
|
5 |
|
->prototype('scalar')->end() |
74
|
5 |
|
->beforeNormalization() |
75
|
5 |
|
->ifString() |
76
|
5 |
|
->then(function ($v) { |
77
|
3 |
|
return array($v); |
78
|
5 |
|
}) |
79
|
5 |
|
->end() |
80
|
5 |
|
->end() |
81
|
5 |
|
->end() |
82
|
5 |
|
->end() |
83
|
5 |
|
->end() |
84
|
5 |
|
->end() |
85
|
|
|
; |
86
|
|
|
|
87
|
5 |
|
return $treeBuilder; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $id |
92
|
|
|
* |
93
|
|
|
* @return Factory |
94
|
|
|
*/ |
95
|
1 |
|
public function getSourceFactory($id) |
96
|
|
|
{ |
97
|
1 |
|
return $this->sourceFactories[$id]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $id |
102
|
|
|
* |
103
|
|
|
* @return Factory |
104
|
|
|
*/ |
105
|
1 |
|
public function getNamerFactory($id) |
106
|
|
|
{ |
107
|
1 |
|
return $this->namerFactories[$id]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $id |
112
|
|
|
* |
113
|
|
|
* @return Factory |
114
|
|
|
*/ |
115
|
1 |
|
public function getDestinationFactory($id) |
116
|
|
|
{ |
117
|
1 |
|
return $this->destinationFactories[$id]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $id |
122
|
|
|
* |
123
|
|
|
* @return Factory |
124
|
|
|
*/ |
125
|
1 |
|
public function getProcessorFactory($id) |
126
|
|
|
{ |
127
|
1 |
|
return $this->processorFactories[$id]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param ArrayNodeDefinition $node |
132
|
|
|
* @param string $plural |
133
|
|
|
* @param string $singular |
134
|
|
|
* @param Factory[] $factories |
135
|
|
|
*/ |
136
|
5 |
|
private function addFactories(ArrayNodeDefinition $node, $plural, $singular, array $factories) |
137
|
|
|
{ |
138
|
|
|
$nodeBuilder = $node |
|
|
|
|
139
|
5 |
|
->children() |
140
|
5 |
|
->arrayNode($plural) |
141
|
5 |
|
->useAttributeAsKey('name') |
142
|
5 |
|
->prototype('array') |
143
|
5 |
|
->canBeUnset() |
144
|
5 |
|
->validate() |
145
|
5 |
|
->ifTrue(function ($v) { |
146
|
1 |
|
return count($v) > 1; |
147
|
5 |
|
}) |
148
|
|
|
->thenInvalid(sprintf('Can only have 1 %s per configuration.', $singular)) |
149
|
|
|
->end() |
150
|
|
|
->children() |
151
|
|
|
; |
152
|
|
|
|
153
|
|
|
foreach ($factories as $name => $factory) { |
154
|
|
|
$factoryNode = $nodeBuilder->arrayNode($name); |
155
|
|
|
|
156
|
|
|
$factory->addConfiguration($factoryNode); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.