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