|
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\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Eduardo Gulias Davis <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @internal |
|
23
|
|
|
*/ |
|
24
|
|
|
final class FormatListenerRulesPass implements CompilerPassInterface |
|
25
|
|
|
{ |
|
26
|
15 |
|
public function process(ContainerBuilder $container): void |
|
27
|
|
|
{ |
|
28
|
15 |
|
if (!$container->hasDefinition('fos_rest.format_listener')) { |
|
29
|
12 |
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
if ($container->hasParameter('web_profiler.debug_toolbar.mode')) { |
|
33
|
1 |
|
$path = '_profiler'; |
|
34
|
1 |
|
if (2 === $container->getParameter('web_profiler.debug_toolbar.mode')) { |
|
35
|
1 |
|
$path .= '|_wdt'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$profilerRule = [ |
|
39
|
1 |
|
'host' => null, |
|
40
|
|
|
'methods' => null, |
|
41
|
1 |
|
'path' => "^/$path/", |
|
42
|
|
|
'priorities' => ['html', 'json'], |
|
43
|
1 |
|
'fallback_format' => 'html', |
|
44
|
|
|
'attributes' => array(), |
|
45
|
|
|
'prefer_extension' => true, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
1 |
|
$this->addRule($profilerRule, $container); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
$rules = $container->getParameter('fos_rest.format_listener.rules'); |
|
52
|
3 |
|
foreach ($rules as $rule) { |
|
53
|
3 |
|
$this->addRule($rule, $container); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
$container->setParameter('fos_rest.format_listener.rules', null); |
|
57
|
3 |
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
private function addRule(array $rule, ContainerBuilder $container): void |
|
60
|
|
|
{ |
|
61
|
3 |
|
$matcher = $this->createRequestMatcher( |
|
62
|
3 |
|
$container, |
|
63
|
3 |
|
$rule['path'], |
|
64
|
3 |
|
$rule['host'], |
|
65
|
3 |
|
$rule['methods'], |
|
66
|
3 |
|
$rule['attributes'] |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
3 |
|
unset($rule['path'], $rule['host']); |
|
70
|
3 |
|
if (is_bool($rule['prefer_extension']) && $rule['prefer_extension']) { |
|
71
|
3 |
|
$rule['prefer_extension'] = '2.0'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
$container->getDefinition('fos_rest.format_negotiator') |
|
75
|
3 |
|
->addMethodCall('add', [$matcher, $rule]); |
|
76
|
3 |
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
private function createRequestMatcher(ContainerBuilder $container, ?string $path = null, ?string $host = null, ?array $methods = null, array $attributes = array()): Reference |
|
79
|
|
|
{ |
|
80
|
3 |
|
$arguments = [$path, $host, $methods, null, $attributes]; |
|
81
|
3 |
|
$serialized = serialize($arguments); |
|
82
|
3 |
|
$id = 'fos_rest.request_matcher.'.md5($serialized).sha1($serialized); |
|
83
|
|
|
|
|
84
|
3 |
|
if (!$container->hasDefinition($id)) { |
|
85
|
|
|
// only add arguments that are necessary |
|
86
|
|
|
$container |
|
87
|
1 |
|
->setDefinition($id, new ChildDefinition('fos_rest.format_request_matcher')) |
|
88
|
1 |
|
->setArguments($arguments); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
return new Reference($id); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|