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