1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
|
22
|
|
|
class FOSRestExtension extends Extension |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Loads the services based on your application configuration. |
26
|
|
|
* |
27
|
|
|
* @param array $configs |
28
|
|
|
* @param ContainerBuilder $container |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
* @throws \LogicException |
32
|
|
|
*/ |
33
|
47 |
|
public function load(array $configs, ContainerBuilder $container) |
34
|
|
|
{ |
35
|
47 |
|
$config = $this->processConfiguration(new Configuration(), $configs); |
36
|
|
|
|
37
|
42 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
38
|
42 |
|
$loader->load('view.xml'); |
39
|
42 |
|
$loader->load('routing.xml'); |
40
|
42 |
|
$loader->load('request.xml'); |
41
|
42 |
|
$loader->load('serializer.xml'); |
42
|
|
|
|
43
|
42 |
|
$container->getDefinition('fos_rest.routing.loader.controller')->replaceArgument(4, $config['routing_loader']['default_format']); |
44
|
42 |
|
$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(4, $config['routing_loader']['default_format']); |
45
|
42 |
|
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(4, $config['routing_loader']['default_format']); |
46
|
|
|
|
47
|
42 |
|
$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(2, $config['routing_loader']['include_format']); |
48
|
42 |
|
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(2, $config['routing_loader']['include_format']); |
49
|
42 |
|
$container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(3, $config['routing_loader']['include_format']); |
50
|
|
|
|
51
|
|
|
// The validator service alias is only set if validation is enabled for the request body converter |
52
|
42 |
|
$validator = $config['service']['validator']; |
53
|
42 |
|
unset($config['service']['validator']); |
54
|
|
|
|
55
|
42 |
|
foreach ($config['service'] as $key => $service) { |
56
|
42 |
|
if (null !== $service) { |
57
|
42 |
|
$container->setAlias('fos_rest.'.$key, $service); |
58
|
42 |
|
} |
59
|
42 |
|
} |
60
|
|
|
|
61
|
42 |
|
$this->loadForm($config, $loader, $container); |
62
|
42 |
|
$this->loadException($config, $loader, $container); |
63
|
40 |
|
$this->loadBodyConverter($config, $validator, $loader, $container); |
64
|
40 |
|
$this->loadView($config, $loader, $container); |
65
|
|
|
|
66
|
40 |
|
$this->loadBodyListener($config, $loader, $container); |
67
|
40 |
|
$this->loadFormatListener($config, $loader, $container); |
68
|
40 |
|
$this->loadVersioning($config, $loader, $container); |
69
|
40 |
|
$this->loadParamFetcherListener($config, $loader, $container); |
70
|
40 |
|
$this->loadAllowedMethodsListener($config, $loader, $container); |
71
|
40 |
|
$this->loadAccessDeniedListener($config, $loader, $container); |
72
|
40 |
|
$this->loadZoneMatcherListener($config, $loader, $container); |
73
|
|
|
|
74
|
|
|
// Needs RequestBodyParamConverter and View Handler loaded. |
75
|
40 |
|
$this->loadSerializer($config, $container); |
76
|
40 |
|
} |
77
|
|
|
|
78
|
42 |
|
private function loadForm(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
79
|
|
|
{ |
80
|
42 |
|
if (!empty($config['disable_csrf_role'])) { |
81
|
1 |
|
$loader->load('forms.xml'); |
82
|
1 |
|
$container->getDefinition('fos_rest.form.extension.csrf_disable')->replaceArgument(1, $config['disable_csrf_role']); |
83
|
1 |
|
} |
84
|
42 |
|
} |
85
|
|
|
|
86
|
40 |
|
private function loadAccessDeniedListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
87
|
|
|
{ |
88
|
40 |
|
if ($config['access_denied_listener']['enabled'] && !empty($config['access_denied_listener']['formats'])) { |
89
|
|
|
$loader->load('access_denied_listener.xml'); |
90
|
|
|
|
91
|
|
|
$service = $container->getDefinition('fos_rest.access_denied_listener'); |
92
|
|
|
|
93
|
|
|
if (!empty($config['access_denied_listener']['service'])) { |
94
|
|
|
$service->clearTag('kernel.event_listener'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$service->replaceArgument(0, $config['access_denied_listener']['formats']); |
98
|
|
|
$service->replaceArgument(1, $config['unauthorized_challenge']); |
99
|
|
|
} |
100
|
40 |
|
} |
101
|
|
|
|
102
|
40 |
View Code Duplication |
private function loadAllowedMethodsListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
|
|
|
103
|
|
|
{ |
104
|
40 |
|
if ($config['allowed_methods_listener']['enabled']) { |
105
|
1 |
|
if (!empty($config['allowed_methods_listener']['service'])) { |
106
|
|
|
$service = $container->getDefinition('fos_rest.allowed_methods_listener'); |
107
|
|
|
$service->clearTag('kernel.event_listener'); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$loader->load('allowed_methods_listener.xml'); |
111
|
|
|
|
112
|
1 |
|
$container->getDefinition('fos_rest.allowed_methods_loader')->replaceArgument(1, $config['cache_dir']); |
113
|
1 |
|
} |
114
|
40 |
|
} |
115
|
|
|
|
116
|
40 |
|
private function loadBodyListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
117
|
|
|
{ |
118
|
40 |
|
if ($config['body_listener']['enabled']) { |
119
|
39 |
|
$loader->load('body_listener.xml'); |
120
|
|
|
|
121
|
39 |
|
$service = $container->getDefinition('fos_rest.body_listener'); |
122
|
|
|
|
123
|
39 |
|
if (!empty($config['body_listener']['service'])) { |
124
|
|
|
$service->clearTag('kernel.event_listener'); |
125
|
|
|
} |
126
|
|
|
|
127
|
39 |
|
$service->replaceArgument(1, $config['body_listener']['throw_exception_on_unsupported_content_type']); |
128
|
39 |
|
$service->addMethodCall('setDefaultFormat', array($config['body_listener']['default_format'])); |
129
|
|
|
|
130
|
39 |
|
$container->getDefinition('fos_rest.decoder_provider')->replaceArgument(1, $config['body_listener']['decoders']); |
131
|
|
|
|
132
|
39 |
|
$arrayNormalizer = $config['body_listener']['array_normalizer']; |
133
|
|
|
|
134
|
39 |
|
if (null !== $arrayNormalizer['service']) { |
135
|
3 |
|
$bodyListener = $container->getDefinition('fos_rest.body_listener'); |
136
|
3 |
|
$bodyListener->addArgument(new Reference($arrayNormalizer['service'])); |
137
|
3 |
|
$bodyListener->addArgument($arrayNormalizer['forms']); |
138
|
3 |
|
} |
139
|
39 |
|
} |
140
|
40 |
|
} |
141
|
|
|
|
142
|
40 |
|
private function loadFormatListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
143
|
|
|
{ |
144
|
40 |
|
if ($config['format_listener']['enabled'] && !empty($config['format_listener']['rules'])) { |
145
|
5 |
|
$loader->load('format_listener.xml'); |
146
|
|
|
|
147
|
5 |
|
if (!empty($config['format_listener']['service'])) { |
148
|
|
|
$service = $container->getDefinition('fos_rest.format_listener'); |
149
|
|
|
$service->clearTag('kernel.event_listener'); |
150
|
|
|
} |
151
|
|
|
|
152
|
5 |
|
foreach ($config['format_listener']['rules'] as &$rule) { |
153
|
5 |
|
if (!isset($rule['exception_fallback_format'])) { |
154
|
5 |
|
$rule['exception_fallback_format'] = $rule['fallback_format']; |
155
|
5 |
|
} |
156
|
5 |
|
} |
157
|
|
|
|
158
|
5 |
|
$container->setParameter( |
159
|
5 |
|
'fos_rest.format_listener.rules', |
160
|
5 |
|
$config['format_listener']['rules'] |
161
|
5 |
|
); |
162
|
|
|
|
163
|
5 |
View Code Duplication |
if ($config['view']['mime_types']['enabled']) { |
|
|
|
|
164
|
2 |
|
$container->getDefinition('fos_rest.format_negotiator')->replaceArgument(1, $config['view']['mime_types']['formats']); |
165
|
2 |
|
} |
166
|
5 |
|
} |
167
|
40 |
|
} |
168
|
|
|
|
169
|
40 |
|
private function loadVersioning(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
170
|
|
|
{ |
171
|
40 |
|
if (!empty($config['versioning']['enabled'])) { |
172
|
2 |
|
$loader->load('versioning.xml'); |
173
|
|
|
|
174
|
2 |
|
$versionListener = $container->getDefinition('fos_rest.versioning.listener'); |
175
|
2 |
|
$versionListener->replaceArgument(2, $config['versioning']['default_version']); |
176
|
|
|
|
177
|
2 |
|
$resolvers = []; |
178
|
2 |
View Code Duplication |
if ($config['versioning']['resolvers']['query']['enabled']) { |
|
|
|
|
179
|
2 |
|
$resolvers['query'] = $container->getDefinition('fos_rest.versioning.query_parameter_resolver'); |
180
|
2 |
|
$resolvers['query']->replaceArgument(0, $config['versioning']['resolvers']['query']['parameter_name']); |
181
|
2 |
|
} |
182
|
2 |
View Code Duplication |
if ($config['versioning']['resolvers']['custom_header']['enabled']) { |
|
|
|
|
183
|
2 |
|
$resolvers['custom_header'] = $container->getDefinition('fos_rest.versioning.header_resolver'); |
184
|
2 |
|
$resolvers['custom_header']->replaceArgument(0, $config['versioning']['resolvers']['custom_header']['header_name']); |
185
|
2 |
|
} |
186
|
2 |
View Code Duplication |
if ($config['versioning']['resolvers']['media_type']['enabled']) { |
|
|
|
|
187
|
2 |
|
$resolvers['media_type'] = $container->getDefinition('fos_rest.versioning.media_type_resolver'); |
188
|
2 |
|
$resolvers['media_type']->replaceArgument(0, $config['versioning']['resolvers']['media_type']['regex']); |
189
|
2 |
|
} |
190
|
|
|
|
191
|
2 |
|
$chainResolver = $container->getDefinition('fos_rest.versioning.chain_resolver'); |
192
|
2 |
|
foreach ($config['versioning']['guessing_order'] as $resolver) { |
193
|
2 |
|
if (isset($resolvers[$resolver])) { |
194
|
2 |
|
$chainResolver->addMethodCall('addResolver', [$resolvers[$resolver]]); |
195
|
2 |
|
} |
196
|
2 |
|
} |
197
|
2 |
|
} |
198
|
40 |
|
} |
199
|
|
|
|
200
|
40 |
View Code Duplication |
private function loadParamFetcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
|
|
|
201
|
|
|
{ |
202
|
40 |
|
if ($config['param_fetcher_listener']['enabled']) { |
203
|
4 |
|
$loader->load('param_fetcher_listener.xml'); |
204
|
|
|
|
205
|
4 |
|
if (!empty($config['param_fetcher_listener']['service'])) { |
206
|
|
|
$service = $container->getDefinition('fos_rest.param_fetcher_listener'); |
207
|
|
|
$service->clearTag('kernel.event_listener'); |
208
|
|
|
} |
209
|
|
|
|
210
|
4 |
|
if ($config['param_fetcher_listener']['force']) { |
211
|
1 |
|
$container->getDefinition('fos_rest.param_fetcher_listener')->replaceArgument(1, true); |
212
|
1 |
|
} |
213
|
4 |
|
} |
214
|
40 |
|
} |
215
|
|
|
|
216
|
40 |
|
private function loadBodyConverter(array $config, $validator, XmlFileLoader $loader, ContainerBuilder $container) |
217
|
|
|
{ |
218
|
40 |
|
if (empty($config['body_converter'])) { |
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
|
222
|
40 |
|
if (!empty($config['body_converter']['enabled'])) { |
223
|
2 |
|
$loader->load('request_body_param_converter.xml'); |
224
|
|
|
|
225
|
2 |
|
if (!empty($config['body_converter']['validation_errors_argument'])) { |
226
|
2 |
|
$container->getDefinition('fos_rest.converter.request_body')->replaceArgument(4, $config['body_converter']['validation_errors_argument']); |
227
|
2 |
|
} |
228
|
2 |
|
} |
229
|
|
|
|
230
|
40 |
|
if (!empty($config['body_converter']['validate'])) { |
231
|
2 |
|
$container->setAlias('fos_rest.validator', $validator); |
232
|
2 |
|
} |
233
|
40 |
|
} |
234
|
|
|
|
235
|
40 |
|
private function loadView(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
236
|
|
|
{ |
237
|
40 |
|
if (!empty($config['view']['exception_wrapper_handler'])) { |
238
|
|
|
$container->setAlias('fos_rest.view.exception_wrapper_handler', $config['view']['exception_wrapper_handler']); |
239
|
|
|
} |
240
|
|
|
|
241
|
40 |
|
if (!empty($config['view']['jsonp_handler'])) { |
242
|
1 |
|
$handler = new DefinitionDecorator($config['service']['view_handler']); |
243
|
1 |
|
$handler->setPublic(true); |
244
|
|
|
|
245
|
1 |
|
$jsonpHandler = new Reference('fos_rest.view_handler.jsonp'); |
246
|
1 |
|
$handler->addMethodCall('registerHandler', ['jsonp', [$jsonpHandler, 'createResponse']]); |
247
|
1 |
|
$container->setDefinition('fos_rest.view_handler', $handler); |
248
|
|
|
|
249
|
1 |
|
$container->getDefinition('fos_rest.view_handler.jsonp')->replaceArgument(0, $config['view']['jsonp_handler']['callback_param']); |
250
|
|
|
|
251
|
1 |
|
if (empty($config['view']['mime_types']['jsonp'])) { |
252
|
1 |
|
$config['view']['mime_types']['jsonp'] = $config['view']['jsonp_handler']['mime_type']; |
253
|
1 |
|
} |
254
|
1 |
|
} |
255
|
|
|
|
256
|
40 |
|
if ($config['view']['mime_types']['enabled']) { |
257
|
2 |
|
$loader->load('mime_type_listener.xml'); |
258
|
|
|
|
259
|
2 |
|
if (!empty($config['mime_type_listener']['service'])) { |
260
|
|
|
$service = $container->getDefinition('fos_rest.mime_type_listener'); |
261
|
|
|
$service->clearTag('kernel.event_listener'); |
262
|
|
|
} |
263
|
|
|
|
264
|
2 |
|
$container->getDefinition('fos_rest.mime_type_listener')->replaceArgument(0, $config['view']['mime_types']); |
265
|
2 |
|
} |
266
|
|
|
|
267
|
40 |
|
if ($config['view']['view_response_listener']['enabled']) { |
268
|
6 |
|
$loader->load('view_response_listener.xml'); |
269
|
6 |
|
$service = $container->getDefinition('fos_rest.view_response_listener'); |
270
|
|
|
|
271
|
6 |
|
if (!empty($config['view_response_listener']['service'])) { |
272
|
|
|
$service->clearTag('kernel.event_listener'); |
273
|
|
|
} |
274
|
|
|
|
275
|
6 |
|
$service->replaceArgument(1, $config['view']['view_response_listener']['force']); |
276
|
6 |
|
} |
277
|
|
|
|
278
|
40 |
|
$formats = []; |
279
|
40 |
View Code Duplication |
foreach ($config['view']['formats'] as $format => $enabled) { |
|
|
|
|
280
|
40 |
|
if ($enabled) { |
281
|
40 |
|
$formats[$format] = false; |
282
|
40 |
|
} |
283
|
40 |
|
} |
284
|
40 |
View Code Duplication |
foreach ($config['view']['templating_formats'] as $format => $enabled) { |
|
|
|
|
285
|
40 |
|
if ($enabled) { |
286
|
40 |
|
$formats[$format] = true; |
287
|
40 |
|
} |
288
|
40 |
|
} |
289
|
|
|
|
290
|
40 |
|
$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(3, $formats); |
291
|
40 |
|
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(3, $formats); |
292
|
40 |
|
$container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(4, $formats); |
293
|
|
|
|
294
|
40 |
|
foreach ($config['view']['force_redirects'] as $format => $code) { |
295
|
40 |
|
if (true === $code) { |
296
|
40 |
|
$config['view']['force_redirects'][$format] = Response::HTTP_FOUND; |
297
|
40 |
|
} |
298
|
40 |
|
} |
299
|
|
|
|
300
|
40 |
View Code Duplication |
if (!is_numeric($config['view']['failed_validation'])) { |
|
|
|
|
301
|
|
|
$config['view']['failed_validation'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['failed_validation']); |
302
|
|
|
} |
303
|
|
|
|
304
|
40 |
|
$defaultViewHandler = $container->getDefinition('fos_rest.view_handler.default'); |
305
|
40 |
|
$defaultViewHandler->replaceArgument(5, $formats); |
306
|
40 |
|
$defaultViewHandler->replaceArgument(6, $config['view']['failed_validation']); |
307
|
|
|
|
308
|
40 |
View Code Duplication |
if (!is_numeric($config['view']['empty_content'])) { |
|
|
|
|
309
|
|
|
$config['view']['empty_content'] = constant('\Symfony\Component\HttpFoundation\Response::'.$config['view']['empty_content']); |
310
|
|
|
} |
311
|
|
|
|
312
|
40 |
|
$defaultViewHandler->replaceArgument(7, $config['view']['empty_content']); |
313
|
40 |
|
$defaultViewHandler->replaceArgument(8, $config['view']['serialize_null']); |
314
|
40 |
|
$defaultViewHandler->replaceArgument(9, $config['view']['force_redirects']); |
315
|
40 |
|
$defaultViewHandler->replaceArgument(10, $config['view']['default_engine']); |
316
|
40 |
|
} |
317
|
|
|
|
318
|
42 |
|
private function loadException(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
319
|
|
|
{ |
320
|
42 |
|
if ($config['exception']['enabled']) { |
321
|
6 |
|
$loader->load('exception_listener.xml'); |
322
|
|
|
|
323
|
6 |
|
if (!empty($config['exception']['service'])) { |
324
|
|
|
$service = $container->getDefinition('fos_rest.exception_listener'); |
325
|
|
|
$service->clearTag('kernel.event_listener'); |
326
|
|
|
} |
327
|
|
|
|
328
|
6 |
|
if ($config['exception']['exception_controller']) { |
329
|
|
|
$container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, $config['exception']['exception_controller']); |
330
|
6 |
|
} elseif (isset($container->getParameter('kernel.bundles')['TwigBundle'])) { |
331
|
1 |
|
$container->getDefinition('fos_rest.exception_listener')->replaceArgument(0, 'fos_rest.exception.twig_controller:showAction'); |
332
|
1 |
|
} |
333
|
|
|
|
334
|
6 |
View Code Duplication |
if ($config['view']['mime_types']['enabled']) { |
|
|
|
|
335
|
1 |
|
$container->getDefinition('fos_rest.exception_format_negotiator')->replaceArgument(1, $config['view']['mime_types']['formats']); |
336
|
1 |
|
} |
337
|
|
|
|
338
|
6 |
|
$exceptionController = $container->getDefinition('fos_rest.exception.controller'); |
339
|
6 |
|
$exceptionController->replaceArgument(3, $config['exception']['codes']); |
340
|
6 |
|
$exceptionController->replaceArgument(4, $config['exception']['messages']); |
341
|
6 |
|
} |
342
|
|
|
|
343
|
42 |
|
foreach ($config['exception']['codes'] as $exception => $code) { |
344
|
2 |
|
if (!is_numeric($code)) { |
345
|
|
|
$config['exception']['codes'][$exception] = constant("\Symfony\Component\HttpFoundation\Response::$code"); |
346
|
|
|
} |
347
|
|
|
|
348
|
2 |
|
$this->testExceptionExists($exception); |
349
|
41 |
|
} |
350
|
|
|
|
351
|
41 |
|
foreach ($config['exception']['messages'] as $exception => $message) { |
352
|
3 |
|
$this->testExceptionExists($exception); |
353
|
40 |
|
} |
354
|
40 |
|
} |
355
|
|
|
|
356
|
40 |
|
private function loadSerializer(array $config, ContainerBuilder $container) |
357
|
|
|
{ |
358
|
40 |
|
$bodyConverter = $container->hasDefinition('fos_rest.converter.request_body') ? $container->getDefinition('fos_rest.converter.request_body') : null; |
359
|
40 |
|
$viewHandler = $container->getDefinition('fos_rest.view_handler.default'); |
360
|
|
|
|
361
|
40 |
View Code Duplication |
if (!empty($config['serializer']['version'])) { |
|
|
|
|
362
|
1 |
|
if ($bodyConverter) { |
363
|
|
|
$bodyConverter->replaceArgument(2, $config['serializer']['version']); |
364
|
|
|
} |
365
|
1 |
|
$viewHandler->addMethodCall('setExclusionStrategyVersion', array($config['serializer']['version'])); |
366
|
1 |
|
} |
367
|
|
|
|
368
|
40 |
View Code Duplication |
if (!empty($config['serializer']['groups'])) { |
|
|
|
|
369
|
1 |
|
if ($bodyConverter) { |
370
|
|
|
$bodyConverter->replaceArgument(1, $config['serializer']['groups']); |
371
|
|
|
} |
372
|
1 |
|
$viewHandler->addMethodCall('setExclusionStrategyGroups', array($config['serializer']['groups'])); |
373
|
1 |
|
} |
374
|
|
|
|
375
|
40 |
|
$viewHandler->addMethodCall('setSerializeNullStrategy', array($config['serializer']['serialize_null'])); |
376
|
40 |
|
} |
377
|
|
|
|
378
|
40 |
|
private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
379
|
|
|
{ |
380
|
40 |
|
if (!empty($config['zone'])) { |
381
|
1 |
|
$loader->load('zone_matcher_listener.xml'); |
382
|
1 |
|
$zoneMatcherListener = $container->getDefinition('fos_rest.zone_matcher_listener'); |
383
|
|
|
|
384
|
1 |
|
foreach ($config['zone'] as $zone) { |
385
|
1 |
|
$matcher = $this->createZoneRequestMatcher($container, |
386
|
1 |
|
$zone['path'], |
387
|
1 |
|
$zone['host'], |
388
|
1 |
|
$zone['methods'], |
389
|
1 |
|
$zone['ips'] |
390
|
1 |
|
); |
391
|
|
|
|
392
|
1 |
|
$zoneMatcherListener->addMethodCall('addRequestMatcher', array($matcher)); |
393
|
1 |
|
} |
394
|
1 |
|
} |
395
|
40 |
|
} |
396
|
|
|
|
397
|
1 |
|
private function createZoneRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = array(), $ip = null) |
398
|
|
|
{ |
399
|
1 |
|
if ($methods) { |
|
|
|
|
400
|
|
|
$methods = array_map('strtoupper', (array) $methods); |
401
|
|
|
} |
402
|
|
|
|
403
|
1 |
|
$serialized = serialize(array($path, $host, $methods, $ip)); |
404
|
1 |
|
$id = 'fos_rest.zone_request_matcher.'.md5($serialized).sha1($serialized); |
405
|
|
|
|
406
|
|
|
// only add arguments that are necessary |
407
|
1 |
|
$arguments = array($path, $host, $methods, $ip); |
408
|
1 |
|
while (count($arguments) > 0 && !end($arguments)) { |
409
|
1 |
|
array_pop($arguments); |
410
|
1 |
|
} |
411
|
|
|
|
412
|
|
|
$container |
413
|
1 |
|
->setDefinition($id, new DefinitionDecorator('fos_rest.zone_request_matcher')) |
414
|
1 |
|
->setArguments($arguments) |
415
|
|
|
; |
416
|
|
|
|
417
|
1 |
|
return new Reference($id); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Checks if an exception is loadable. |
422
|
|
|
* |
423
|
|
|
* @param string $exception Class to test |
424
|
|
|
* |
425
|
|
|
* @throws \InvalidArgumentException if the class was not found. |
426
|
|
|
*/ |
427
|
5 |
|
private function testExceptionExists($exception) |
428
|
|
|
{ |
429
|
5 |
|
if (!is_subclass_of($exception, \Exception::class) && !is_a($exception, \Exception::class, true)) { |
|
|
|
|
430
|
2 |
|
throw new \InvalidArgumentException("FOSRestBundle exception mapper: Could not load class '$exception' or the class does not extend from '\Exception'. Most probably this is a configuration problem."); |
431
|
|
|
} |
432
|
3 |
|
} |
433
|
|
|
} |
434
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.