|
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 FOS\RestBundle\EventListener\ResponseStatusCodeListener; |
|
15
|
|
|
use FOS\RestBundle\Inflector\DoctrineInflector; |
|
16
|
|
|
use FOS\RestBundle\View\ViewHandler; |
|
17
|
|
|
use Symfony\Component\Config\FileLocator; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
26
|
|
|
use Symfony\Component\Form\AbstractType; |
|
27
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
29
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
30
|
|
|
use Symfony\Component\HttpKernel\EventListener\ErrorListener; |
|
31
|
|
|
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as LegacyHttpKernelExceptionListener; |
|
32
|
|
|
use Symfony\Component\Validator\Constraint; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @internal since 2.8 |
|
36
|
|
|
*/ |
|
37
|
|
|
class FOSRestExtension extends Extension |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
23 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
43
|
|
|
{ |
|
44
|
23 |
|
return new Configuration($container->getParameter('kernel.debug')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
68 |
|
public function load(array $configs, ContainerBuilder $container) |
|
48
|
|
|
{ |
|
49
|
68 |
|
$configuration = new Configuration($container->getParameter('kernel.debug')); |
|
50
|
68 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
51
|
|
|
|
|
52
|
63 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
53
|
63 |
|
$loader->load('view.xml'); |
|
54
|
63 |
|
$loader->load('request.xml'); |
|
55
|
63 |
|
$loader->load('serializer.xml'); |
|
56
|
|
|
|
|
57
|
63 |
|
$container->register('fos_rest.inflector.doctrine', DoctrineInflector::class) |
|
58
|
63 |
|
->setDeprecated(true, 'The %service_id% service is deprecated since FOSRestBundle 2.8.') |
|
59
|
63 |
|
->setPublic(false); |
|
60
|
|
|
|
|
61
|
63 |
|
if ($config['routing_loader']['enabled']) { |
|
62
|
10 |
|
$loader->load('routing.xml'); |
|
63
|
|
|
|
|
64
|
10 |
|
$restRouteLoader = $container->getDefinition('fos_rest.routing.loader.controller'); |
|
65
|
10 |
|
$restRouteLoader->addArgument(new Reference('controller_name_converter', ContainerInterface::NULL_ON_INVALID_REFERENCE)); |
|
66
|
10 |
|
$restRouteLoader->addArgument(new Reference('fos_rest.routing.loader.reader.controller')); |
|
67
|
10 |
|
$restRouteLoader->addArgument($config['routing_loader']['default_format']); |
|
68
|
|
|
|
|
69
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(4, $config['routing_loader']['default_format']); |
|
70
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(4, $config['routing_loader']['default_format']); |
|
71
|
|
|
|
|
72
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(2, $config['routing_loader']['include_format']); |
|
73
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(2, $config['routing_loader']['include_format']); |
|
74
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(3, $config['routing_loader']['include_format']); |
|
75
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(5, $config['routing_loader']['prefix_methods']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
63 |
|
foreach ($config['service'] as $key => $service) { |
|
79
|
63 |
|
if ('validator' === $service && empty($config['body_converter']['validate'])) { |
|
80
|
60 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
63 |
|
if (null !== $service) { |
|
84
|
63 |
|
if ('view_handler' === $key) { |
|
85
|
63 |
|
$container->setAlias('fos_rest.'.$key, new Alias($service, true)); |
|
86
|
63 |
|
} elseif('templating' === $key) { |
|
87
|
6 |
|
$alias = new Alias($service); |
|
88
|
|
|
|
|
89
|
6 |
|
if (method_exists($alias, 'setDeprecated')) { |
|
90
|
6 |
|
$alias->setDeprecated(true, 'The "%alias_id%" service alias is deprecated since FOSRestBundle 2.8.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
6 |
|
$container->setAlias('fos_rest.'.$key, $alias); |
|
94
|
|
|
} else { |
|
95
|
63 |
|
$container->setAlias('fos_rest.'.$key, $service); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
63 |
|
$this->loadForm($config, $loader, $container); |
|
101
|
63 |
|
$this->loadException($config, $loader, $container); |
|
102
|
63 |
|
$this->loadBodyConverter($config, $loader, $container); |
|
103
|
63 |
|
$this->loadView($config, $loader, $container); |
|
104
|
|
|
|
|
105
|
63 |
|
$this->loadBodyListener($config, $loader, $container); |
|
106
|
63 |
|
$this->loadFormatListener($config, $loader, $container); |
|
107
|
63 |
|
$this->loadVersioning($config, $loader, $container); |
|
108
|
63 |
|
$this->loadParamFetcherListener($config, $loader, $container); |
|
109
|
63 |
|
$this->loadAllowedMethodsListener($config, $loader, $container); |
|
110
|
63 |
|
$this->loadAccessDeniedListener($config, $loader, $container); |
|
111
|
63 |
|
$this->loadZoneMatcherListener($config, $loader, $container); |
|
112
|
|
|
|
|
113
|
|
|
// Needs RequestBodyParamConverter and View Handler loaded. |
|
114
|
63 |
|
$this->loadSerializer($config, $container); |
|
115
|
63 |
|
} |
|
116
|
|
|
|
|
117
|
63 |
|
private function loadForm(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
118
|
|
|
{ |
|
119
|
63 |
|
if (!empty($config['disable_csrf_role'])) { |
|
120
|
2 |
|
$loader->load('forms.xml'); |
|
121
|
|
|
|
|
122
|
2 |
|
$definition = $container->getDefinition('fos_rest.form.extension.csrf_disable'); |
|
123
|
2 |
|
$definition->replaceArgument(1, $config['disable_csrf_role']); |
|
124
|
|
|
|
|
125
|
|
|
// BC for Symfony < 2.8: the extended_type attribute is used on higher versions |
|
126
|
2 |
|
if (!method_exists(AbstractType::class, 'getBlockPrefix')) { |
|
127
|
|
|
$definition->addTag('form.type_extension', ['alias' => 'form']); |
|
128
|
|
|
} else { |
|
129
|
2 |
|
$definition->addTag('form.type_extension', ['extended_type' => FormType::class]); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
63 |
|
} |
|
133
|
|
|
|
|
134
|
63 |
|
private function loadAccessDeniedListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
135
|
|
|
{ |
|
136
|
63 |
|
if ($config['access_denied_listener']['enabled'] && !empty($config['access_denied_listener']['formats'])) { |
|
137
|
|
|
$loader->load('access_denied_listener.xml'); |
|
138
|
|
|
|
|
139
|
|
|
$service = $container->getDefinition('fos_rest.access_denied_listener'); |
|
140
|
|
|
|
|
141
|
|
|
if (!empty($config['access_denied_listener']['service'])) { |
|
142
|
|
|
$service->clearTag('kernel.event_subscriber'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$service->replaceArgument(0, $config['access_denied_listener']['formats']); |
|
146
|
|
|
$service->replaceArgument(1, $config['unauthorized_challenge']); |
|
147
|
|
|
} |
|
148
|
63 |
|
} |
|
149
|
|
|
|
|
150
|
63 |
|
private function loadAllowedMethodsListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
151
|
|
|
{ |
|
152
|
63 |
|
if ($config['allowed_methods_listener']['enabled']) { |
|
153
|
3 |
|
if (!empty($config['allowed_methods_listener']['service'])) { |
|
154
|
|
|
$service = $container->getDefinition('fos_rest.allowed_methods_listener'); |
|
155
|
|
|
$service->clearTag('kernel.event_listener'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
$loader->load('allowed_methods_listener.xml'); |
|
159
|
|
|
|
|
160
|
3 |
|
$container->getDefinition('fos_rest.allowed_methods_loader')->replaceArgument(1, $config['cache_dir']); |
|
161
|
|
|
} |
|
162
|
63 |
|
} |
|
163
|
|
|
|
|
164
|
63 |
|
private function loadBodyListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
165
|
|
|
{ |
|
166
|
63 |
|
if ($config['body_listener']['enabled']) { |
|
167
|
62 |
|
$loader->load('body_listener.xml'); |
|
168
|
|
|
|
|
169
|
62 |
|
$service = $container->getDefinition('fos_rest.body_listener'); |
|
170
|
|
|
|
|
171
|
62 |
|
if (!empty($config['body_listener']['service'])) { |
|
172
|
|
|
$service->clearTag('kernel.event_listener'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
62 |
|
$service->replaceArgument(1, $config['body_listener']['throw_exception_on_unsupported_content_type']); |
|
176
|
62 |
|
$service->addMethodCall('setDefaultFormat', array($config['body_listener']['default_format'])); |
|
177
|
|
|
|
|
178
|
62 |
|
$container->getDefinition('fos_rest.decoder_provider')->replaceArgument(1, $config['body_listener']['decoders']); |
|
179
|
|
|
|
|
180
|
62 |
|
if (class_exists(ServiceLocatorTagPass::class)) { |
|
181
|
62 |
|
$decoderServicesMap = array(); |
|
182
|
|
|
|
|
183
|
62 |
|
foreach ($config['body_listener']['decoders'] as $id) { |
|
184
|
62 |
|
$decoderServicesMap[$id] = new Reference($id); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
62 |
|
$decodersServiceLocator = ServiceLocatorTagPass::register($container, $decoderServicesMap); |
|
188
|
62 |
|
$container->getDefinition('fos_rest.decoder_provider')->replaceArgument(0, $decodersServiceLocator); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
62 |
|
$arrayNormalizer = $config['body_listener']['array_normalizer']; |
|
192
|
|
|
|
|
193
|
62 |
|
if (null !== $arrayNormalizer['service']) { |
|
194
|
3 |
|
$bodyListener = $container->getDefinition('fos_rest.body_listener'); |
|
195
|
3 |
|
$bodyListener->addArgument(new Reference($arrayNormalizer['service'])); |
|
196
|
3 |
|
$bodyListener->addArgument($arrayNormalizer['forms']); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
63 |
|
} |
|
200
|
|
|
|
|
201
|
63 |
|
private function loadFormatListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
202
|
|
|
{ |
|
203
|
63 |
View Code Duplication |
if ($config['format_listener']['enabled'] && !empty($config['format_listener']['rules'])) { |
|
204
|
6 |
|
$loader->load('format_listener.xml'); |
|
205
|
|
|
|
|
206
|
6 |
|
if (!empty($config['format_listener']['service'])) { |
|
207
|
|
|
$service = $container->getDefinition('fos_rest.format_listener'); |
|
208
|
|
|
$service->clearTag('kernel.event_listener'); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
6 |
|
$container->setParameter( |
|
212
|
6 |
|
'fos_rest.format_listener.rules', |
|
213
|
6 |
|
$config['format_listener']['rules'] |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
63 |
|
} |
|
217
|
|
|
|
|
218
|
63 |
|
private function loadVersioning(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
219
|
|
|
{ |
|
220
|
63 |
|
if (!empty($config['versioning']['enabled'])) { |
|
221
|
2 |
|
$loader->load('versioning.xml'); |
|
222
|
|
|
|
|
223
|
2 |
|
$versionListener = $container->getDefinition('fos_rest.versioning.listener'); |
|
224
|
2 |
|
$versionListener->replaceArgument(1, $config['versioning']['default_version']); |
|
225
|
|
|
|
|
226
|
2 |
|
$resolvers = []; |
|
227
|
2 |
View Code Duplication |
if ($config['versioning']['resolvers']['query']['enabled']) { |
|
228
|
2 |
|
$resolvers['query'] = $container->getDefinition('fos_rest.versioning.query_parameter_resolver'); |
|
229
|
2 |
|
$resolvers['query']->replaceArgument(0, $config['versioning']['resolvers']['query']['parameter_name']); |
|
230
|
|
|
} |
|
231
|
2 |
View Code Duplication |
if ($config['versioning']['resolvers']['custom_header']['enabled']) { |
|
232
|
2 |
|
$resolvers['custom_header'] = $container->getDefinition('fos_rest.versioning.header_resolver'); |
|
233
|
2 |
|
$resolvers['custom_header']->replaceArgument(0, $config['versioning']['resolvers']['custom_header']['header_name']); |
|
234
|
|
|
} |
|
235
|
2 |
View Code Duplication |
if ($config['versioning']['resolvers']['media_type']['enabled']) { |
|
236
|
2 |
|
$resolvers['media_type'] = $container->getDefinition('fos_rest.versioning.media_type_resolver'); |
|
237
|
2 |
|
$resolvers['media_type']->replaceArgument(0, $config['versioning']['resolvers']['media_type']['regex']); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
2 |
|
$chainResolver = $container->getDefinition('fos_rest.versioning.chain_resolver'); |
|
241
|
2 |
|
foreach ($config['versioning']['guessing_order'] as $resolver) { |
|
242
|
2 |
|
if (isset($resolvers[$resolver])) { |
|
243
|
2 |
|
$chainResolver->addMethodCall('addResolver', [$resolvers[$resolver]]); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
63 |
|
} |
|
248
|
|
|
|
|
249
|
63 |
|
private function loadParamFetcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
250
|
|
|
{ |
|
251
|
63 |
|
if ($config['param_fetcher_listener']['enabled']) { |
|
252
|
5 |
|
if (!class_exists(Constraint::class)) { |
|
253
|
|
|
@trigger_error('Enabling the fos_rest.param_fetcher_listener option when the Symfony Validator component is not installed is deprecated since FOSRestBundle 2.6 and will throw an exception in 3.0. Disable the feature or install the symfony/validator package.', E_USER_DEPRECATED); |
|
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
5 |
|
$loader->load('param_fetcher_listener.xml'); |
|
257
|
|
|
|
|
258
|
5 |
|
if (!empty($config['param_fetcher_listener']['service'])) { |
|
259
|
|
|
$service = $container->getDefinition('fos_rest.param_fetcher_listener'); |
|
260
|
|
|
$service->clearTag('kernel.event_listener'); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
5 |
|
if ($config['param_fetcher_listener']['force']) { |
|
264
|
1 |
|
$container->getDefinition('fos_rest.param_fetcher_listener')->replaceArgument(1, true); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
63 |
|
} |
|
268
|
|
|
|
|
269
|
63 |
|
private function loadBodyConverter(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
270
|
|
|
{ |
|
271
|
63 |
|
if (!$this->isConfigEnabled($container, $config['body_converter'])) { |
|
272
|
56 |
|
return; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
7 |
|
$loader->load('request_body_param_converter.xml'); |
|
276
|
|
|
|
|
277
|
7 |
|
if (!empty($config['body_converter']['validation_errors_argument'])) { |
|
278
|
7 |
|
$container->getDefinition('fos_rest.converter.request_body')->replaceArgument(4, $config['body_converter']['validation_errors_argument']); |
|
279
|
|
|
} |
|
280
|
7 |
|
} |
|
281
|
|
|
|
|
282
|
63 |
|
private function loadView(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
283
|
|
|
{ |
|
284
|
63 |
|
if (!empty($config['view']['jsonp_handler'])) { |
|
285
|
1 |
|
$childDefinitionClass = class_exists(ChildDefinition::class) ? ChildDefinition::class : DefinitionDecorator::class; |
|
286
|
1 |
|
$handler = new $childDefinitionClass($config['service']['view_handler']); |
|
287
|
1 |
|
$handler->setPublic(true); |
|
288
|
|
|
|
|
289
|
1 |
|
$jsonpHandler = new Reference('fos_rest.view_handler.jsonp'); |
|
290
|
1 |
|
$handler->addMethodCall('registerHandler', ['jsonp', [$jsonpHandler, 'createResponse']]); |
|
291
|
1 |
|
$container->setDefinition('fos_rest.view_handler', $handler); |
|
292
|
|
|
|
|
293
|
1 |
|
$container->getDefinition('fos_rest.view_handler.jsonp')->replaceArgument(0, $config['view']['jsonp_handler']['callback_param']); |
|
294
|
|
|
|
|
295
|
1 |
|
if (empty($config['view']['mime_types']['jsonp'])) { |
|
296
|
1 |
|
$config['view']['mime_types']['jsonp'] = $config['view']['jsonp_handler']['mime_type']; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
63 |
View Code Duplication |
if ($config['view']['mime_types']['enabled']) { |
|
301
|
3 |
|
$loader->load('mime_type_listener.xml'); |
|
302
|
|
|
|
|
303
|
3 |
|
if (!empty($config['mime_type_listener']['service'])) { |
|
304
|
|
|
$service = $container->getDefinition('fos_rest.mime_type_listener'); |
|
305
|
|
|
$service->clearTag('kernel.event_listener'); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
3 |
|
$container->getDefinition('fos_rest.mime_type_listener')->replaceArgument(0, $config['view']['mime_types']['formats']); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
63 |
View Code Duplication |
if ($config['view']['view_response_listener']['enabled']) { |
|
312
|
11 |
|
$loader->load('view_response_listener.xml'); |
|
313
|
11 |
|
$service = $container->getDefinition('fos_rest.view_response_listener'); |
|
314
|
|
|
|
|
315
|
11 |
|
if (!empty($config['view_response_listener']['service'])) { |
|
316
|
|
|
$service->clearTag('kernel.event_listener'); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
11 |
|
$service->replaceArgument(1, $config['view']['view_response_listener']['force']); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
63 |
|
$formats = []; |
|
323
|
63 |
View Code Duplication |
foreach ($config['view']['formats'] as $format => $enabled) { |
|
324
|
63 |
|
if ($enabled) { |
|
325
|
63 |
|
$formats[$format] = false; |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
63 |
View Code Duplication |
foreach ($config['view']['templating_formats'] as $format => $enabled) { |
|
329
|
63 |
|
if ($enabled) { |
|
330
|
63 |
|
$formats[$format] = true; |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
63 |
|
if ($config['routing_loader']['enabled']) { |
|
335
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.yaml_collection')->replaceArgument(3, $formats); |
|
336
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.xml_collection')->replaceArgument(3, $formats); |
|
337
|
10 |
|
$container->getDefinition('fos_rest.routing.loader.reader.action')->replaceArgument(4, $formats); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
63 |
|
foreach ($config['view']['force_redirects'] as $format => $code) { |
|
341
|
7 |
|
if (true === $code) { |
|
342
|
7 |
|
$config['view']['force_redirects'][$format] = Response::HTTP_FOUND; |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
63 |
View Code Duplication |
if (!is_numeric($config['view']['failed_validation'])) { |
|
347
|
|
|
$config['view']['failed_validation'] = constant(sprintf('%s::%s', Response::class, $config['view']['failed_validation'])); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
63 |
View Code Duplication |
if (!is_numeric($config['view']['empty_content'])) { |
|
351
|
|
|
$config['view']['empty_content'] = constant(sprintf('%s::%s', Response::class, $config['view']['empty_content'])); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
63 |
|
$defaultViewHandler = $container->getDefinition('fos_rest.view_handler.default'); |
|
355
|
|
|
|
|
356
|
63 |
|
if ([] !== $config['view']['force_redirects']) { |
|
357
|
7 |
|
@trigger_error('Not setting the "fos_rest.view.force_redirects" configuration option to an empty array is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED); |
|
|
|
|
|
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
63 |
|
if (null === $config['service']['templating'] && [] === $config['view']['force_redirects'] && null === $config['view']['default_engine']) { |
|
361
|
56 |
|
$defaultViewHandler->setFactory([ViewHandler::class, 'create']); |
|
362
|
56 |
|
$defaultViewHandler->setArguments([ |
|
363
|
56 |
|
new Reference('fos_rest.router'), |
|
364
|
56 |
|
new Reference('fos_rest.serializer'), |
|
365
|
56 |
|
new Reference('request_stack'), |
|
366
|
56 |
|
$formats, |
|
367
|
56 |
|
$config['view']['failed_validation'], |
|
368
|
56 |
|
$config['view']['empty_content'], |
|
369
|
56 |
|
$config['view']['serialize_null'], |
|
370
|
|
|
]); |
|
371
|
|
|
} else { |
|
372
|
7 |
|
$defaultViewHandler->setArguments([ |
|
373
|
7 |
|
new Reference('fos_rest.router'), |
|
374
|
7 |
|
new Reference('fos_rest.serializer'), |
|
375
|
7 |
|
new Reference('fos_rest.templating', ContainerInterface::NULL_ON_INVALID_REFERENCE), |
|
376
|
7 |
|
new Reference('request_stack'), |
|
377
|
7 |
|
$formats, |
|
378
|
7 |
|
$config['view']['failed_validation'], |
|
379
|
7 |
|
$config['view']['empty_content'], |
|
380
|
7 |
|
$config['view']['serialize_null'], |
|
381
|
7 |
|
$config['view']['force_redirects'], |
|
382
|
7 |
|
$config['view']['default_engine'], |
|
383
|
|
|
]); |
|
384
|
|
|
} |
|
385
|
63 |
|
} |
|
386
|
|
|
|
|
387
|
63 |
|
private function loadException(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
388
|
|
|
{ |
|
389
|
63 |
|
if ($config['exception']['enabled']) { |
|
390
|
60 |
|
$loader->load('exception_listener.xml'); |
|
391
|
|
|
|
|
392
|
60 |
|
if ($config['exception']['map_exception_codes']) { |
|
393
|
1 |
|
$container->register('fos_rest.exception.response_status_code_listener', ResponseStatusCodeListener::class) |
|
394
|
1 |
|
->setArguments([ |
|
395
|
1 |
|
new Reference('fos_rest.exception.codes_map'), |
|
396
|
|
|
]) |
|
397
|
1 |
|
->addTag('kernel.event_subscriber'); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
60 |
|
if ($config['exception']['exception_listener']) { |
|
401
|
11 |
|
if (!empty($config['exception']['service'])) { |
|
402
|
|
|
$service = $container->getDefinition('fos_rest.exception_listener'); |
|
403
|
|
|
$service->clearTag('kernel.event_subscriber'); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
11 |
|
$controller = $config['exception']['exception_controller'] ?? null; |
|
407
|
|
|
|
|
408
|
11 |
|
if (class_exists(ErrorListener::class)) { |
|
409
|
11 |
|
$container->register('fos_rest.error_listener', ErrorListener::class) |
|
410
|
11 |
|
->setArguments([ |
|
411
|
11 |
|
$controller, |
|
412
|
11 |
|
new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE), |
|
413
|
11 |
|
'%kernel.debug%', |
|
414
|
|
|
]) |
|
415
|
11 |
|
->addTag('monolog.logger', ['channel' => 'request']); |
|
416
|
|
|
} else { |
|
417
|
|
|
$container->register('fos_rest.error_listener', LegacyHttpKernelExceptionListener::class) |
|
418
|
|
|
->setArguments([ |
|
419
|
|
|
$controller, |
|
420
|
|
|
new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE), |
|
421
|
|
|
]) |
|
422
|
|
|
->addTag('monolog.logger', ['channel' => 'request']); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
11 |
|
$container->getDefinition('fos_rest.exception.controller') |
|
426
|
11 |
|
->replaceArgument(2, $config['exception']['debug']); |
|
427
|
|
|
} else { |
|
428
|
49 |
|
$container->removeDefinition('fos_rest.exception_listener'); |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
60 |
|
$container->getDefinition('fos_rest.exception.codes_map') |
|
432
|
60 |
|
->replaceArgument(0, $config['exception']['codes']); |
|
433
|
60 |
|
$container->getDefinition('fos_rest.exception.messages_map') |
|
434
|
60 |
|
->replaceArgument(0, $config['exception']['messages']); |
|
435
|
|
|
|
|
436
|
60 |
|
$container->getDefinition('fos_rest.serializer.flatten_exception_handler') |
|
437
|
60 |
|
->replaceArgument(1, $config['exception']['debug']); |
|
438
|
60 |
|
$container->getDefinition('fos_rest.serializer.flatten_exception_handler') |
|
439
|
60 |
|
->replaceArgument(2, 'rfc7807' === $config['exception']['flatten_exception_format']); |
|
440
|
60 |
|
$container->getDefinition('fos_rest.serializer.flatten_exception_normalizer') |
|
441
|
60 |
|
->replaceArgument(1, $config['exception']['debug']); |
|
442
|
60 |
|
$container->getDefinition('fos_rest.serializer.flatten_exception_normalizer') |
|
443
|
60 |
|
->replaceArgument(2, 'rfc7807' === $config['exception']['flatten_exception_format']); |
|
444
|
|
|
|
|
445
|
60 |
|
if ($config['exception']['serialize_exceptions']) { |
|
446
|
15 |
|
$container->getDefinition('fos_rest.serializer.exception_normalizer.jms') |
|
447
|
15 |
|
->replaceArgument(1, $config['exception']['debug']); |
|
448
|
15 |
|
$container->getDefinition('fos_rest.serializer.exception_normalizer.symfony') |
|
449
|
15 |
|
->replaceArgument(1, $config['exception']['debug']); |
|
450
|
|
|
} else { |
|
451
|
45 |
|
$container->removeDefinition('fos_rest.serializer.exception_normalizer.jms'); |
|
452
|
45 |
|
$container->removeDefinition('fos_rest.serializer.exception_normalizer.symfony'); |
|
453
|
|
|
} |
|
454
|
|
|
} |
|
455
|
63 |
|
} |
|
456
|
|
|
|
|
457
|
63 |
|
private function loadSerializer(array $config, ContainerBuilder $container) |
|
458
|
|
|
{ |
|
459
|
63 |
|
$bodyConverter = $container->hasDefinition('fos_rest.converter.request_body') ? $container->getDefinition('fos_rest.converter.request_body') : null; |
|
460
|
63 |
|
$viewHandler = $container->getDefinition('fos_rest.view_handler.default'); |
|
461
|
63 |
|
$options = array(); |
|
462
|
|
|
|
|
463
|
63 |
View Code Duplication |
if (!empty($config['serializer']['version'])) { |
|
464
|
|
|
if ($bodyConverter) { |
|
465
|
|
|
$bodyConverter->replaceArgument(2, $config['serializer']['version']); |
|
466
|
|
|
} |
|
467
|
|
|
$options['exclusionStrategyVersion'] = $config['serializer']['version']; |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
63 |
View Code Duplication |
if (!empty($config['serializer']['groups'])) { |
|
471
|
|
|
if ($bodyConverter) { |
|
472
|
|
|
$bodyConverter->replaceArgument(1, $config['serializer']['groups']); |
|
473
|
|
|
} |
|
474
|
|
|
$options['exclusionStrategyGroups'] = $config['serializer']['groups']; |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
63 |
|
$options['serializeNullStrategy'] = $config['serializer']['serialize_null']; |
|
478
|
63 |
|
$viewHandler->addArgument($options); |
|
479
|
63 |
|
} |
|
480
|
|
|
|
|
481
|
63 |
|
private function loadZoneMatcherListener(array $config, XmlFileLoader $loader, ContainerBuilder $container) |
|
482
|
|
|
{ |
|
483
|
63 |
|
if (!empty($config['zone'])) { |
|
484
|
1 |
|
$loader->load('zone_matcher_listener.xml'); |
|
485
|
1 |
|
$zoneMatcherListener = $container->getDefinition('fos_rest.zone_matcher_listener'); |
|
486
|
|
|
|
|
487
|
1 |
|
foreach ($config['zone'] as $zone) { |
|
488
|
1 |
|
$matcher = $this->createZoneRequestMatcher( |
|
489
|
1 |
|
$container, |
|
490
|
1 |
|
$zone['path'], |
|
491
|
1 |
|
$zone['host'], |
|
492
|
1 |
|
$zone['methods'], |
|
493
|
1 |
|
$zone['ips'] |
|
494
|
|
|
); |
|
495
|
|
|
|
|
496
|
1 |
|
$zoneMatcherListener->addMethodCall('addRequestMatcher', array($matcher)); |
|
497
|
|
|
} |
|
498
|
|
|
} |
|
499
|
63 |
|
} |
|
500
|
|
|
|
|
501
|
1 |
|
private function createZoneRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = array(), $ip = null) |
|
502
|
|
|
{ |
|
503
|
1 |
|
if ($methods) { |
|
|
|
|
|
|
504
|
|
|
$methods = array_map('strtoupper', (array) $methods); |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
1 |
|
$serialized = serialize(array($path, $host, $methods, $ip)); |
|
508
|
1 |
|
$id = 'fos_rest.zone_request_matcher.'.md5($serialized).sha1($serialized); |
|
509
|
|
|
|
|
510
|
|
|
// only add arguments that are necessary |
|
511
|
1 |
|
$arguments = array($path, $host, $methods, $ip); |
|
512
|
1 |
|
while (count($arguments) > 0 && !end($arguments)) { |
|
513
|
1 |
|
array_pop($arguments); |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
1 |
|
$childDefinitionClass = class_exists(ChildDefinition::class) ? ChildDefinition::class : DefinitionDecorator::class; |
|
517
|
|
|
$container |
|
518
|
1 |
|
->setDefinition($id, new $childDefinitionClass('fos_rest.zone_request_matcher')) |
|
519
|
1 |
|
->setArguments($arguments) |
|
520
|
|
|
; |
|
521
|
|
|
|
|
522
|
1 |
|
return new Reference($id); |
|
523
|
|
|
} |
|
524
|
|
|
} |
|
525
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: