|
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\Negotiation; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\Util\StopFormatListenerException; |
|
15
|
|
|
use Negotiation\Accept; |
|
16
|
|
|
use Negotiation\Negotiator as BaseNegotiator; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\RequestMatcherInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @internal |
|
23
|
|
|
*/ |
|
24
|
|
|
class BaseFormatNegotiator extends BaseNegotiator |
|
25
|
|
|
{ |
|
26
|
|
|
private $map = []; |
|
27
|
|
|
private $requestStack; |
|
28
|
|
|
private $mimeTypes; |
|
29
|
|
|
|
|
30
|
29 |
|
public function __construct(RequestStack $requestStack, array $mimeTypes = []) |
|
31
|
|
|
{ |
|
32
|
29 |
|
$this->requestStack = $requestStack; |
|
33
|
29 |
|
$this->mimeTypes = $mimeTypes; |
|
34
|
29 |
|
} |
|
35
|
|
|
|
|
36
|
27 |
|
public function add(RequestMatcherInterface $requestMatcher, array $options = []) |
|
37
|
|
|
{ |
|
38
|
27 |
|
$this->map[] = [$requestMatcher, $options]; |
|
39
|
27 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @internal |
|
43
|
|
|
*/ |
|
44
|
24 |
|
protected function doGetBest($header, array $priorities = []) |
|
45
|
|
|
{ |
|
46
|
24 |
|
$request = $this->getRequest(); |
|
47
|
24 |
|
$header = $header ?: $request->headers->get('Accept'); |
|
48
|
|
|
|
|
49
|
24 |
|
foreach ($this->map as $elements) { |
|
50
|
|
|
// Check if the current RequestMatcherInterface matches the current request |
|
51
|
22 |
|
if (!$elements[0]->matches($request)) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
22 |
|
$options = &$elements[1]; // Do not reallow memory for this variable |
|
55
|
|
|
|
|
56
|
22 |
|
if (!empty($options['stop'])) { |
|
57
|
1 |
|
throw new StopFormatListenerException('Stopped format listener'); |
|
58
|
|
|
} |
|
59
|
21 |
|
if (empty($options['priorities']) && empty($priorities)) { |
|
60
|
4 |
|
if (!empty($options['fallback_format'])) { |
|
61
|
4 |
|
return new Accept($request->getMimeType($options['fallback_format'])); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
17 |
|
if (isset($options['prefer_extension']) && $options['prefer_extension'] && !isset($extensionHeader)) { |
|
68
|
12 |
|
$extension = pathinfo($request->getPathInfo(), PATHINFO_EXTENSION); |
|
69
|
|
|
|
|
70
|
12 |
|
if (!empty($extension)) { |
|
71
|
|
|
// $extensionHeader will now be either a non empty string or an empty string |
|
72
|
6 |
|
$extensionHeader = $request->getMimeType($extension); |
|
73
|
|
|
|
|
74
|
6 |
|
if ($extensionHeader) { |
|
|
|
|
|
|
75
|
3 |
|
$header = $extensionHeader.'; q='.$options['prefer_extension'].($header ? ','.$header : ''); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
17 |
|
if ($header) { |
|
81
|
16 |
|
$mimeTypes = $this->normalizePriorities( |
|
82
|
16 |
|
$request, |
|
83
|
16 |
|
empty($priorities) ? $options['priorities'] : $priorities |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
16 |
|
$mimeType = parent::getBest($header, $mimeTypes); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
16 |
|
if (null !== $mimeType) { |
|
89
|
15 |
|
return $mimeType; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
if (isset($options['fallback_format'])) { |
|
94
|
|
|
// if false === fallback_format then we fail here instead of considering more rules |
|
95
|
2 |
|
if (false === $options['fallback_format']) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// stop looking at rules since we have a fallback defined |
|
100
|
2 |
|
return new Accept($request->getMimeType($options['fallback_format'])); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
4 |
|
} |
|
104
|
|
|
|
|
105
|
16 |
|
private function sanitize(array $values): array |
|
106
|
|
|
{ |
|
107
|
|
|
return array_map(function ($value) { |
|
108
|
16 |
|
return preg_replace('/\s+/', '', strtolower($value)); |
|
109
|
16 |
|
}, $values); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string[] $priorities |
|
114
|
|
|
* |
|
115
|
|
|
* @return string[] formatted priorities |
|
116
|
|
|
*/ |
|
117
|
16 |
|
private function normalizePriorities(Request $request, array $priorities): array |
|
118
|
|
|
{ |
|
119
|
16 |
|
$priorities = $this->sanitize($priorities); |
|
120
|
|
|
|
|
121
|
16 |
|
$mimeTypes = []; |
|
122
|
16 |
|
foreach ($priorities as $priority) { |
|
123
|
16 |
|
if (strpos($priority, '/')) { |
|
124
|
4 |
|
$mimeTypes[] = $priority; |
|
125
|
|
|
|
|
126
|
4 |
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
13 |
|
if (method_exists(Request::class, 'getMimeTypes')) { |
|
130
|
13 |
|
$mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority)); |
|
131
|
|
|
} elseif (null !== $request->getMimeType($priority)) { |
|
132
|
|
|
$class = new \ReflectionClass(Request::class); |
|
133
|
|
|
$properties = $class->getStaticProperties(); |
|
134
|
|
|
$mimeTypes = array_merge($mimeTypes, $properties['formats'][$priority]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
13 |
|
if (isset($this->mimeTypes[$priority])) { |
|
138
|
3 |
|
foreach ($this->mimeTypes[$priority] as $mimeType) { |
|
139
|
3 |
|
$mimeTypes[] = $mimeType; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
16 |
|
return $mimeTypes; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
24 |
View Code Duplication |
private function getRequest(): Request |
|
|
|
|
|
|
148
|
|
|
{ |
|
149
|
24 |
|
$request = $this->requestStack->getCurrentRequest(); |
|
150
|
24 |
|
if (null === $request) { |
|
151
|
|
|
throw new \RuntimeException('There is no current request.'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
24 |
|
return $request; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
if (method_exists(BaseNegotiator::class, 'getOrderedElements')) { |
|
159
|
|
|
/** |
|
160
|
|
|
* @author Guilhem Niot <[email protected]> |
|
161
|
|
|
* |
|
162
|
|
|
* @final since 2.8 |
|
163
|
|
|
*/ |
|
164
|
|
|
class FormatNegotiator extends BaseFormatNegotiator |
|
165
|
|
|
{ |
|
166
|
|
|
public function getBest($header, array $priorities = [], $strict = false) |
|
167
|
|
|
{ |
|
168
|
24 |
|
return $this->doGetBest($header, $priorities); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} else { |
|
172
|
|
|
/** |
|
173
|
|
|
* @author Guilhem Niot <[email protected]> |
|
174
|
|
|
* |
|
175
|
|
|
* @final since 2.8 |
|
176
|
|
|
*/ |
|
177
|
|
|
class FormatNegotiator extends BaseFormatNegotiator |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
|
|
public function getBest($header, array $priorities = []) |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->doGetBest($header, $priorities); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: