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