|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
6
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
7
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
8
|
|
|
|
|
9
|
|
|
final class Configuration implements ConfigurationInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** {@inheritdoc} */ |
|
12
|
|
|
public function getConfigTreeBuilder() |
|
13
|
|
|
{ |
|
14
|
|
|
$builder = new TreeBuilder(); |
|
15
|
|
|
$root = $builder->root('cruds'); |
|
16
|
|
|
|
|
17
|
|
|
$root |
|
18
|
|
|
->children() |
|
19
|
|
|
->scalarNode('prefix') |
|
20
|
|
|
->defaultNull() |
|
21
|
|
|
->info('Route prefix') |
|
22
|
|
|
->example('/api'); |
|
23
|
|
|
|
|
24
|
|
|
$entities = $root->children()->arrayNode('entities'); |
|
25
|
|
|
$entities->useAttributeAsKey('name'); |
|
26
|
|
|
/** @var ArrayNodeDefinition $entitiesProto */ |
|
27
|
|
|
$entitiesProto = $entities->prototype('array'); |
|
28
|
|
|
|
|
29
|
|
|
$this->configureEntityProto($entitiesProto); |
|
30
|
|
|
|
|
31
|
|
|
return $builder; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function configureEntityProto(ArrayNodeDefinition $parent) |
|
35
|
|
|
{ |
|
36
|
|
|
$parent |
|
37
|
|
|
->children() |
|
38
|
|
|
->scalarNode('class') |
|
39
|
|
|
->isRequired() |
|
40
|
|
|
->info('Doctrine class') |
|
41
|
|
|
->example('MyBundle:MyEntity'); |
|
42
|
|
|
|
|
43
|
|
|
$parent |
|
44
|
|
|
->children() |
|
45
|
|
|
->scalarNode('prefix') |
|
46
|
|
|
->defaultNull() |
|
47
|
|
|
->info('Route prefix') |
|
48
|
|
|
->example('/my-entity'); |
|
49
|
|
|
|
|
50
|
|
|
$actions = $parent |
|
51
|
|
|
->children() |
|
52
|
|
|
->arrayNode('actions'); |
|
53
|
|
|
|
|
54
|
|
|
$actions |
|
55
|
|
|
->info('Action configuration') |
|
56
|
|
|
->example( |
|
57
|
|
|
[ |
|
58
|
|
|
'read' => null, |
|
59
|
|
|
'create' => ['enabled' => false], |
|
60
|
|
|
'delete' => ['enabled' => true, 'path' => '/remove'], |
|
61
|
|
|
] |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this->configureCreateAction($actions); |
|
|
|
|
|
|
65
|
|
|
$this->configureReadAction($actions); |
|
66
|
|
|
$this->configureUpdateAction($actions); |
|
|
|
|
|
|
67
|
|
|
$this->configureDeleteAction($actions); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function configureCreateAction(ArrayNodeDefinition $parent) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function configureReadAction(ArrayNodeDefinition $parent) |
|
75
|
|
|
{ |
|
76
|
|
|
$read = $parent |
|
77
|
|
|
->children() |
|
78
|
|
|
->arrayNode('read'); |
|
79
|
|
|
|
|
80
|
|
|
$find = $read |
|
81
|
|
|
->children() |
|
82
|
|
|
->arrayNode('find'); |
|
83
|
|
|
|
|
84
|
|
|
$get = $read |
|
85
|
|
|
->children() |
|
86
|
|
|
->arrayNode('get'); |
|
87
|
|
|
|
|
88
|
|
|
$this->configureActionNode($find, 'find'); |
|
89
|
|
|
|
|
90
|
|
|
$criteria = $find->children()->variableNode('criteria'); |
|
91
|
|
|
$criteria |
|
92
|
|
|
->defaultValue([]) |
|
93
|
|
|
->beforeNormalization() |
|
94
|
|
|
->ifString() |
|
95
|
|
|
->then(function ($v) { |
|
96
|
|
|
return [$v]; |
|
97
|
|
|
}) |
|
98
|
|
|
->ifNull() |
|
99
|
|
|
->thenEmptyArray(); |
|
100
|
|
|
|
|
101
|
|
|
$criteria->info('List of prioritized criteria modifiers'); |
|
102
|
|
|
$criteria->example( |
|
103
|
|
|
[ |
|
104
|
|
|
'@my.criteria.modifier', |
|
105
|
|
|
] |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$this->configureActionNode($get, 'get'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function configureUpdateAction(ArrayNodeDefinition $parent) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function configureDeleteAction(ArrayNodeDefinition $parent) |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function configureActionNode(ArrayNodeDefinition $parent, $action) |
|
120
|
|
|
{ |
|
121
|
|
|
$parent |
|
122
|
|
|
->children() |
|
123
|
|
|
->scalarNode('path') |
|
124
|
|
|
->example('/' . $action) |
|
125
|
|
|
->info('Action path (prefixed)') |
|
126
|
|
|
->defaultNull(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: