1 | <?php |
||
2 | |||
3 | namespace Spiechu\SymfonyCommonsBundle\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 | use Symfony\Component\HttpFoundation\Request; |
||
9 | |||
10 | class Configuration implements ConfigurationInterface |
||
11 | { |
||
12 | /** |
||
13 | * {@inheritdoc} |
||
14 | * |
||
15 | * @throws \RuntimeException |
||
16 | * @throws \InvalidArgumentException |
||
17 | */ |
||
18 | 21 | public function getConfigTreeBuilder(): TreeBuilder |
|
19 | { |
||
20 | 21 | $treeBuilder = new TreeBuilder(); |
|
21 | 21 | $rootNode = $treeBuilder->root('spiechu_symfony_commons'); |
|
22 | |||
23 | 21 | $this->addGetMethodOverride($rootNode); |
|
24 | 21 | $this->addResponseSchemaValidation($rootNode); |
|
25 | 21 | $this->addApiVersionSupport($rootNode); |
|
26 | |||
27 | 21 | return $treeBuilder; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param ArrayNodeDefinition $rootNode |
||
32 | * |
||
33 | * @throws \InvalidArgumentException |
||
34 | * @throws \RuntimeException |
||
35 | */ |
||
36 | 21 | protected function addGetMethodOverride(ArrayNodeDefinition $rootNode): void |
|
37 | { |
||
38 | 21 | $overridableHttpMethods = $this->getOverridableHttpMethods(); |
|
39 | $defaultOverridedHttpMethods = [ |
||
40 | 21 | Request::METHOD_DELETE, |
|
41 | 21 | Request::METHOD_POST, |
|
42 | 21 | Request::METHOD_PUT, |
|
43 | ]; |
||
44 | |||
45 | $rootNode |
||
46 | 21 | ->children() |
|
47 | 21 | ->arrayNode('get_method_override') |
|
48 | 21 | ->info('Default options for GET method override feature') |
|
49 | 21 | ->addDefaultsIfNotSet() |
|
50 | 21 | ->canBeEnabled() |
|
51 | 21 | ->children() |
|
52 | 21 | ->scalarNode('listener_service_id') |
|
53 | 21 | ->cannotBeEmpty() |
|
54 | 21 | ->defaultValue('spiechu_symfony_commons.event_listener.get_method_override_listener') |
|
55 | 21 | ->end() |
|
56 | 21 | ->/* @scrutinizer ignore-call */scalarNode('query_param_name') |
|
57 | 21 | ->cannotBeEmpty() |
|
58 | 21 | ->defaultValue('_method') |
|
59 | 21 | ->validate() |
|
60 | ->ifTrue(function ($methodName): bool { |
||
61 | 1 | return !\is_string($methodName); |
|
62 | 21 | }) |
|
63 | 21 | ->thenInvalid('Not a string provided') |
|
64 | 21 | ->end() |
|
65 | 21 | ->end() |
|
66 | 21 | ->arrayNode('allow_methods_override') |
|
67 | 21 | ->beforeNormalization() |
|
68 | 21 | ->ifString()->castToArray() |
|
69 | 21 | ->end() |
|
70 | 21 | ->defaultValue($defaultOverridedHttpMethods) |
|
71 | 21 | ->prototype('scalar') |
|
72 | 21 | ->validate() |
|
73 | 21 | ->ifNotInArray($overridableHttpMethods) |
|
74 | 21 | ->thenInvalid(sprintf( |
|
75 | 21 | 'Invalid methods to override provided, known are: "%s"', |
|
76 | 21 | implode(', ', $overridableHttpMethods) |
|
77 | )) |
||
78 | 21 | ->end() |
|
79 | 21 | ->end() |
|
80 | 21 | ->beforeNormalization() |
|
81 | 21 | ->ifArray() |
|
82 | ->then(function (array $methods): array { |
||
83 | 4 | return array_unique(array_map('strtoupper', $methods)); |
|
84 | 21 | }) |
|
85 | 21 | ->end() |
|
86 | 21 | ->end() |
|
87 | 21 | ->end() |
|
88 | 21 | ->end() |
|
89 | 21 | ->end() |
|
90 | 21 | ->end(); |
|
91 | 21 | } |
|
92 | |||
93 | /** |
||
94 | * @param ArrayNodeDefinition $rootNode |
||
95 | */ |
||
96 | 21 | protected function addResponseSchemaValidation(ArrayNodeDefinition $rootNode): void |
|
97 | { |
||
98 | $rootNode |
||
99 | 21 | ->children() |
|
100 | 21 | ->arrayNode('response_schema_validation') |
|
101 | 21 | ->info('Default options for response schema validation feature') |
|
102 | 21 | ->addDefaultsIfNotSet() |
|
103 | 21 | ->canBeEnabled() |
|
104 | 21 | ->children() |
|
105 | 21 | ->booleanNode('throw_exception_when_format_not_found')->defaultTrue()->end() |
|
106 | 21 | ->scalarNode('failed_schema_check_listener_service_id') |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
107 | 21 | ->defaultValue('spiechu_symfony_commons.event_listener.failed_schema_check_listener') |
|
108 | 21 | ->end() |
|
109 | 21 | ->booleanNode('disable_json_check_schema_subscriber')->defaultFalse()->end() |
|
110 | 21 | ->booleanNode('disable_xml_check_schema_subscriber')->defaultFalse()->end() |
|
111 | 21 | ->end() |
|
112 | 21 | ->end() |
|
113 | 21 | ->end() |
|
114 | 21 | ->end(); |
|
115 | 21 | } |
|
116 | |||
117 | /** |
||
118 | * @param ArrayNodeDefinition $rootNode |
||
119 | * |
||
120 | * @throws \InvalidArgumentException |
||
121 | * @throws \RuntimeException |
||
122 | */ |
||
123 | 21 | protected function addApiVersionSupport(ArrayNodeDefinition $rootNode): void |
|
124 | { |
||
125 | $versionNormalizer = static function ($version): string { |
||
126 | 4 | if (\is_string($version)) { |
|
127 | return $version; |
||
128 | } |
||
129 | |||
130 | 4 | if (!is_numeric($version)) { |
|
131 | throw new \InvalidArgumentException(sprintf('"%s" is not numeric', $version)); |
||
132 | } |
||
133 | |||
134 | 4 | return number_format($version, 1, '.', ''); |
|
135 | 21 | }; |
|
136 | |||
137 | $rootNode |
||
138 | 21 | ->children() |
|
139 | 21 | ->arrayNode('api_versioning') |
|
140 | 21 | ->info('Default options for API versioning feature') |
|
141 | 21 | ->addDefaultsIfNotSet() |
|
142 | 21 | ->canBeEnabled() |
|
143 | 21 | ->children() |
|
144 | 21 | ->booleanNode('versioned_view_listener')->defaultFalse()->end() |
|
145 | 21 | ->/* @scrutinizer ignore-call */arrayNode('features') |
|
146 | 21 | ->useAttributeAsKey('name') |
|
147 | 21 | ->prototype('array') |
|
148 | 21 | ->children() |
|
149 | 21 | ->scalarNode('since') |
|
150 | 21 | ->defaultNull() |
|
151 | 21 | ->beforeNormalization() |
|
152 | 21 | ->always($versionNormalizer) |
|
153 | 21 | ->end() |
|
154 | 21 | ->end() |
|
155 | 21 | ->scalarNode('until') |
|
156 | 21 | ->defaultNull() |
|
157 | 21 | ->beforeNormalization() |
|
158 | 21 | ->always($versionNormalizer) |
|
159 | 21 | ->end() |
|
160 | 21 | ->end() |
|
161 | 21 | ->end() |
|
162 | 21 | ->beforeNormalization() |
|
163 | 21 | ->always(function ($prototypeValue): array { |
|
164 | 4 | if (empty($prototypeValue)) { |
|
165 | throw new \InvalidArgumentException('No version constraints provided'); |
||
166 | } |
||
167 | |||
168 | 4 | return $prototypeValue; |
|
169 | 21 | }) |
|
170 | 21 | ->end() |
|
171 | 21 | ->end() |
|
172 | 21 | ->end() |
|
173 | 21 | ->end() |
|
174 | 21 | ->end() |
|
175 | 21 | ->end() |
|
176 | 21 | ->end(); |
|
177 | 21 | } |
|
178 | |||
179 | /** |
||
180 | * @return string[] |
||
181 | */ |
||
182 | 21 | protected function getOverridableHttpMethods(): array |
|
183 | { |
||
184 | return [ |
||
185 | 21 | Request::METHOD_CONNECT, |
|
186 | 21 | Request::METHOD_DELETE, |
|
187 | 21 | Request::METHOD_HEAD, |
|
188 | 21 | Request::METHOD_OPTIONS, |
|
189 | 21 | Request::METHOD_PATCH, |
|
190 | 21 | Request::METHOD_POST, |
|
191 | 21 | Request::METHOD_PURGE, |
|
192 | 21 | Request::METHOD_TRACE, |
|
193 | 21 | Request::METHOD_PUT, |
|
194 | ]; |
||
195 | } |
||
196 | } |
||
197 |