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