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