1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Cruds\Factory\ReflectionConstructorFactory; |
6
|
|
|
use ScayTrase\Api\Cruds\ObjectFactoryInterface; |
7
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
9
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
10
|
|
|
|
11
|
|
|
final class Configuration implements ConfigurationInterface |
12
|
|
|
{ |
13
|
|
|
/** {@inheritdoc} */ |
14
|
|
|
public function getConfigTreeBuilder() |
15
|
|
|
{ |
16
|
|
|
$builder = new TreeBuilder(); |
17
|
|
|
$root = $builder->root('cruds'); |
18
|
|
|
|
19
|
|
|
$root |
20
|
|
|
->children() |
21
|
|
|
->scalarNode('prefix') |
22
|
|
|
->defaultNull() |
23
|
|
|
->info('Route prefix') |
24
|
|
|
->example('/api'); |
25
|
|
|
|
26
|
|
|
$entities = $root->children()->arrayNode('entities'); |
27
|
|
|
$entities->useAttributeAsKey('name'); |
28
|
|
|
$entities->normalizeKeys(false); |
29
|
|
|
/** @var ArrayNodeDefinition $entitiesProto */ |
30
|
|
|
$entitiesProto = $entities->prototype('array'); |
31
|
|
|
|
32
|
|
|
$this->configureEntityProto($entitiesProto); |
33
|
|
|
|
34
|
|
|
return $builder; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function configureEntityProto(ArrayNodeDefinition $parent) |
38
|
|
|
{ |
39
|
|
|
$parent |
40
|
|
|
->children() |
41
|
|
|
->scalarNode('class') |
42
|
|
|
->isRequired() |
43
|
|
|
->info('Doctrine class') |
44
|
|
|
->example('MyBundle:MyEntity'); |
45
|
|
|
|
46
|
|
|
$parent |
47
|
|
|
->children() |
48
|
|
|
->scalarNode('prefix') |
49
|
|
|
->defaultNull() |
50
|
|
|
->info('Route prefix. Defaults to entity key if not set') |
51
|
|
|
->example('/my-entity'); |
52
|
|
|
|
53
|
|
|
$parent |
54
|
|
|
->children() |
55
|
|
|
->scalarNode('repository') |
56
|
|
|
->defaultNull() |
57
|
|
|
->info('Entity repository. service reference, default to factory-acquired doctrine repository') |
58
|
|
|
->example('@my_entity.repository'); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
$actions = $parent |
62
|
|
|
->children() |
63
|
|
|
->arrayNode('actions'); |
64
|
|
|
|
65
|
|
|
$actions |
66
|
|
|
->beforeNormalization() |
67
|
|
|
->ifArray() |
68
|
|
|
->then( |
69
|
|
|
function (array $v) { |
70
|
|
|
if (array_keys($v) !== range(0, count($v) - 1)) { |
71
|
|
|
return $v; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$result = []; |
75
|
|
|
foreach ($v as $key) { |
76
|
|
|
$result[$key] = ['enabled' => true]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
) |
82
|
|
|
->end() |
83
|
|
|
->info('Action configuration') |
84
|
|
|
->example( |
85
|
|
|
[ |
86
|
|
|
'create' => ['enabled' => false], |
87
|
|
|
'read' => null, |
88
|
|
|
'update' => null, |
89
|
|
|
'delete' => ['enabled' => true, 'path' => '/remove'], |
90
|
|
|
'search' => null, |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$this->configureCreateAction($actions); |
95
|
|
|
$this->configureReadAction($actions); |
96
|
|
|
$this->configureUpdateAction($actions); |
97
|
|
|
$this->configureDeleteAction($actions); |
98
|
|
|
$this->configureSearchAction($actions); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function configureCreateAction(ArrayNodeDefinition $parent) |
102
|
|
|
{ |
103
|
|
|
$create = $parent |
104
|
|
|
->children() |
105
|
|
|
->arrayNode('create'); |
106
|
|
|
|
107
|
|
|
$create |
108
|
|
|
->children() |
109
|
|
|
->variableNode('factory') |
110
|
|
|
->defaultNull() |
111
|
|
|
->example('@my_entity.factory') |
112
|
|
|
->info( |
113
|
|
|
'Service ID implementing '.PHP_EOL. |
114
|
|
|
ObjectFactoryInterface::class.PHP_EOL. |
115
|
|
|
'Defaults to '.ReflectionConstructorFactory::class |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$create |
119
|
|
|
->children() |
120
|
|
|
->variableNode('processor') |
121
|
|
|
->defaultNull() |
122
|
|
|
->example('@my_entity.factory') |
123
|
|
|
->info( |
124
|
|
|
'Service ID implementing '.PHP_EOL. |
125
|
|
|
ObjectFactoryInterface::class.PHP_EOL. |
126
|
|
|
'Defaults to '.ReflectionConstructorFactory::class |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->configureActionNode($create, 'create'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function configureSearchAction(ArrayNodeDefinition $parent) |
133
|
|
|
{ |
134
|
|
|
$search = $parent |
135
|
|
|
->children() |
136
|
|
|
->arrayNode('search'); |
137
|
|
|
$this->configureActionNode($search, 'search'); |
138
|
|
|
|
139
|
|
|
$criteria = $search->children()->variableNode('criteria'); |
140
|
|
|
$criteria |
141
|
|
|
->defaultValue([]) |
142
|
|
|
->beforeNormalization() |
143
|
|
|
->ifString() |
144
|
|
|
->then( |
145
|
|
|
function ($v) { |
146
|
|
|
return [$v]; |
147
|
|
|
} |
148
|
|
|
) |
149
|
|
|
->ifNull() |
150
|
|
|
->thenEmptyArray(); |
151
|
|
|
|
152
|
|
|
$criteria->info('List of prioritized criteria modifiers'); |
153
|
|
|
$criteria->example( |
154
|
|
|
[ |
155
|
|
|
'@my.criteria.modifier', |
156
|
|
|
] |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
private function configureReadAction(ArrayNodeDefinition $parent) |
162
|
|
|
{ |
163
|
|
|
$read = $parent |
164
|
|
|
->children() |
165
|
|
|
->arrayNode('read'); |
166
|
|
|
$this->configureActionNode($read, 'read'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function configureUpdateAction(ArrayNodeDefinition $parent) |
170
|
|
|
{ |
171
|
|
|
$update = $parent |
172
|
|
|
->children() |
173
|
|
|
->arrayNode('update'); |
174
|
|
|
|
175
|
|
|
$update |
176
|
|
|
->children() |
177
|
|
|
->variableNode('processor') |
178
|
|
|
->defaultNull() |
179
|
|
|
->example('@my_entity.factory') |
180
|
|
|
->info( |
181
|
|
|
'Service ID implementing '.PHP_EOL. |
182
|
|
|
ObjectFactoryInterface::class.PHP_EOL. |
183
|
|
|
'Defaults to '.ReflectionConstructorFactory::class |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
$this->configureActionNode($update, 'update'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private function configureDeleteAction(ArrayNodeDefinition $parent) |
190
|
|
|
{ |
191
|
|
|
$delete = $parent |
192
|
|
|
->children() |
193
|
|
|
->arrayNode('delete'); |
194
|
|
|
$this->configureActionNode($delete, 'delete'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function configureActionNode(ArrayNodeDefinition $parent, $action) |
198
|
|
|
{ |
199
|
|
|
$parent->canBeEnabled(); |
200
|
|
|
|
201
|
|
|
$parent |
202
|
|
|
->children() |
203
|
|
|
->scalarNode('path') |
204
|
|
|
->example('/'.$action) |
205
|
|
|
->info('Action path (prefixed)') |
206
|
|
|
->defaultValue('/'.$action); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|