Skip to content

Commit c1dd945

Browse files
fix merge
1 parent e85ffb2 commit c1dd945

File tree

6 files changed

+80
-118
lines changed

6 files changed

+80
-118
lines changed

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,21 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
318318
EOF;
319319
}
320320

321+
if ($methods) {
322+
$methodVariable = in_array('GET', $methods) ? '$canonicalMethod' : '$requestMethod';
323+
$methods = implode("', '", $methods);
324+
}
325+
321326
if ($schemes = $route->getSchemes()) {
322327
if (!$supportsRedirections) {
323328
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
324329
}
325330
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
326331
if ($methods) {
327-
$methods = implode("', '", $methods);
328332
$code .= <<<EOF
329333
\$requiredSchemes = $schemes;
330334
\$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]);
331-
if (!in_array(\$context->getMethod(), array('$methods'))) {
335+
if (!in_array($methodVariable, array('$methods'))) {
332336
if (\$hasRequiredScheme) {
333337
\$allow = array_merge(\$allow, array('$methods'));
334338
}
@@ -359,56 +363,14 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
359363
EOF;
360364
}
361365
} elseif ($methods) {
362-
if (1 === count($methods)) {
363-
if ('HEAD' === $methods[0]) {
364-
$code .= <<<EOF
365-
if ('HEAD' !== \$requestMethod) {
366-
\$allow[] = 'HEAD';
367-
goto $gotoname;
368-
}
369-
370-
371-
EOF;
372-
} else {
373-
$code .= <<<EOF
374-
if ('$methods[0]' !== \$canonicalMethod) {
375-
\$allow[] = '$methods[0]';
376-
goto $gotoname;
377-
}
378-
379-
380-
EOF;
381-
}
382-
} else {
383-
$methodVariable = 'requestMethod';
384-
385-
if (in_array('GET', $methods)) {
386-
// Since we treat HEAD requests like GET requests we don't need to match it.
387-
$methodVariable = 'canonicalMethod';
388-
$methods = array_values(array_filter($methods, function ($method) { return 'HEAD' !== $method; }));
389-
}
390-
391-
if (1 === count($methods)) {
392-
$code .= <<<EOF
393-
if ('$methods[0]' !== \$$methodVariable) {
394-
\$allow[] = '$methods[0]';
395-
goto $gotoname;
396-
}
397-
398-
399-
EOF;
400-
} else {
401-
$methods = implode("', '", $methods);
402-
$code .= <<<EOF
403-
if (!in_array(\$$methodVariable, array('$methods'))) {
366+
$code .= <<<EOF
367+
if (!in_array($methodVariable, array('$methods'))) {
404368
\$allow = array_merge(\$allow, array('$methods'));
405369
goto $gotoname;
406370
}
407371
408372
409373
EOF;
410-
}
411-
}
412374
}
413375

414376
if ($hasTrailingSlash || $schemes || $methods) {

Tests/Fixtures/dumper/url_matcher1.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function match($rawPathinfo)
4545
// bar
4646
if (preg_match('#^/bar/(?P<foo>[^/]++)$#sD', $pathinfo, $matches)) {
4747
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ());
48-
if ('GET' !== $canonicalMethod) {
49-
$allow[] = 'GET';
48+
if (!in_array($canonicalMethod, array('GET', 'HEAD'))) {
49+
$allow = array_merge($allow, array('GET', 'HEAD'));
5050
goto not_bar;
5151
}
5252

@@ -57,8 +57,8 @@ public function match($rawPathinfo)
5757
// barhead
5858
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]++)$#sD', $pathinfo, $matches)) {
5959
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ());
60-
if ('GET' !== $canonicalMethod) {
61-
$allow[] = 'GET';
60+
if (!in_array($canonicalMethod, array('GET'))) {
61+
$allow = array_merge($allow, array('GET'));
6262
goto not_barhead;
6363
}
6464

@@ -95,8 +95,8 @@ public function match($rawPathinfo)
9595
// baz5
9696
if (preg_match('#^/test/(?P<foo>[^/]++)/$#sD', $pathinfo, $matches)) {
9797
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ());
98-
if ('POST' !== $canonicalMethod) {
99-
$allow[] = 'POST';
98+
if (!in_array($requestMethod, array('POST'))) {
99+
$allow = array_merge($allow, array('POST'));
100100
goto not_baz5;
101101
}
102102

@@ -107,8 +107,8 @@ public function match($rawPathinfo)
107107
// baz.baz6
108108
if (preg_match('#^/test/(?P<foo>[^/]++)/$#sD', $pathinfo, $matches)) {
109109
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ());
110-
if ('PUT' !== $canonicalMethod) {
111-
$allow[] = 'PUT';
110+
if (!in_array($requestMethod, array('PUT'))) {
111+
$allow = array_merge($allow, array('PUT'));
112112
goto not_bazbaz6;
113113
}
114114

Tests/Fixtures/dumper/url_matcher2.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function match($rawPathinfo)
4545
// bar
4646
if (preg_match('#^/bar/(?P<foo>[^/]++)$#sD', $pathinfo, $matches)) {
4747
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ());
48-
if ('GET' !== $canonicalMethod) {
49-
$allow[] = 'GET';
48+
if (!in_array($canonicalMethod, array('GET', 'HEAD'))) {
49+
$allow = array_merge($allow, array('GET', 'HEAD'));
5050
goto not_bar;
5151
}
5252

@@ -57,8 +57,8 @@ public function match($rawPathinfo)
5757
// barhead
5858
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]++)$#sD', $pathinfo, $matches)) {
5959
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ());
60-
if ('GET' !== $canonicalMethod) {
61-
$allow[] = 'GET';
60+
if (!in_array($canonicalMethod, array('GET'))) {
61+
$allow = array_merge($allow, array('GET'));
6262
goto not_barhead;
6363
}
6464

@@ -115,8 +115,8 @@ public function match($rawPathinfo)
115115
// baz5
116116
if (preg_match('#^/test/(?P<foo>[^/]++)/$#sD', $pathinfo, $matches)) {
117117
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ());
118-
if ('POST' !== $canonicalMethod) {
119-
$allow[] = 'POST';
118+
if (!in_array($requestMethod, array('POST'))) {
119+
$allow = array_merge($allow, array('POST'));
120120
goto not_baz5;
121121
}
122122

@@ -127,8 +127,8 @@ public function match($rawPathinfo)
127127
// baz.baz6
128128
if (preg_match('#^/test/(?P<foo>[^/]++)/$#sD', $pathinfo, $matches)) {
129129
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ());
130-
if ('PUT' !== $canonicalMethod) {
131-
$allow[] = 'PUT';
130+
if (!in_array($requestMethod, array('PUT'))) {
131+
$allow = array_merge($allow, array('PUT'));
132132
goto not_bazbaz6;
133133
}
134134

Tests/Fixtures/dumper/url_matcher4.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function match($rawPathinfo)
3131
// just_head
3232
if ('/just_head' === $pathinfo) {
3333
$ret = array('_route' => 'just_head');
34-
if ('HEAD' !== $requestMethod) {
35-
$allow[] = 'HEAD';
34+
if (!in_array($requestMethod, array('HEAD'))) {
35+
$allow = array_merge($allow, array('HEAD'));
3636
goto not_just_head;
3737
}
3838

@@ -43,8 +43,8 @@ public function match($rawPathinfo)
4343
// head_and_get
4444
if ('/head_and_get' === $pathinfo) {
4545
$ret = array('_route' => 'head_and_get');
46-
if ('GET' !== $canonicalMethod) {
47-
$allow[] = 'GET';
46+
if (!in_array($canonicalMethod, array('HEAD', 'GET'))) {
47+
$allow = array_merge($allow, array('HEAD', 'GET'));
4848
goto not_head_and_get;
4949
}
5050

@@ -55,8 +55,8 @@ public function match($rawPathinfo)
5555
// get_and_head
5656
if ('/get_and_head' === $pathinfo) {
5757
$ret = array('_route' => 'get_and_head');
58-
if ('GET' !== $canonicalMethod) {
59-
$allow[] = 'GET';
58+
if (!in_array($canonicalMethod, array('GET', 'HEAD'))) {
59+
$allow = array_merge($allow, array('GET', 'HEAD'));
6060
goto not_get_and_head;
6161
}
6262

@@ -92,8 +92,8 @@ public function match($rawPathinfo)
9292
// put_and_get_and_head
9393
if ('/put_and_post' === $pathinfo) {
9494
$ret = array('_route' => 'put_and_get_and_head');
95-
if (!in_array($canonicalMethod, array('PUT', 'GET'))) {
96-
$allow = array_merge($allow, array('PUT', 'GET'));
95+
if (!in_array($canonicalMethod, array('PUT', 'GET', 'HEAD'))) {
96+
$allow = array_merge($allow, array('PUT', 'GET', 'HEAD'));
9797
goto not_put_and_get_and_head;
9898
}
9999

Tests/Fixtures/dumper/url_matcher6.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function match($rawPathinfo)
3737
// simple_trailing_slash_GET_method
3838
if ('/trailing/simple/get-method/' === $pathinfo) {
3939
$ret = array('_route' => 'simple_trailing_slash_GET_method');
40-
if ('GET' !== $canonicalMethod) {
41-
$allow[] = 'GET';
40+
if (!in_array($canonicalMethod, array('GET'))) {
41+
$allow = array_merge($allow, array('GET'));
4242
goto not_simple_trailing_slash_GET_method;
4343
}
4444

@@ -49,8 +49,8 @@ public function match($rawPathinfo)
4949
// simple_trailing_slash_HEAD_method
5050
if ('/trailing/simple/head-method/' === $pathinfo) {
5151
$ret = array('_route' => 'simple_trailing_slash_HEAD_method');
52-
if ('HEAD' !== $requestMethod) {
53-
$allow[] = 'HEAD';
52+
if (!in_array($requestMethod, array('HEAD'))) {
53+
$allow = array_merge($allow, array('HEAD'));
5454
goto not_simple_trailing_slash_HEAD_method;
5555
}
5656

@@ -61,8 +61,8 @@ public function match($rawPathinfo)
6161
// simple_trailing_slash_POST_method
6262
if ('/trailing/simple/post-method/' === $pathinfo) {
6363
$ret = array('_route' => 'simple_trailing_slash_POST_method');
64-
if ('POST' !== $canonicalMethod) {
65-
$allow[] = 'POST';
64+
if (!in_array($requestMethod, array('POST'))) {
65+
$allow = array_merge($allow, array('POST'));
6666
goto not_simple_trailing_slash_POST_method;
6767
}
6868

@@ -81,8 +81,8 @@ public function match($rawPathinfo)
8181
// regex_trailing_slash_GET_method
8282
if (0 === strpos($pathinfo, '/trailing/regex/get-method') && preg_match('#^/trailing/regex/get\\-method/(?P<param>[^/]++)/$#sD', $pathinfo, $matches)) {
8383
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_GET_method')), array ());
84-
if ('GET' !== $canonicalMethod) {
85-
$allow[] = 'GET';
84+
if (!in_array($canonicalMethod, array('GET'))) {
85+
$allow = array_merge($allow, array('GET'));
8686
goto not_regex_trailing_slash_GET_method;
8787
}
8888

@@ -93,8 +93,8 @@ public function match($rawPathinfo)
9393
// regex_trailing_slash_HEAD_method
9494
if (0 === strpos($pathinfo, '/trailing/regex/head-method') && preg_match('#^/trailing/regex/head\\-method/(?P<param>[^/]++)/$#sD', $pathinfo, $matches)) {
9595
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_HEAD_method')), array ());
96-
if ('HEAD' !== $requestMethod) {
97-
$allow[] = 'HEAD';
96+
if (!in_array($requestMethod, array('HEAD'))) {
97+
$allow = array_merge($allow, array('HEAD'));
9898
goto not_regex_trailing_slash_HEAD_method;
9999
}
100100

@@ -105,8 +105,8 @@ public function match($rawPathinfo)
105105
// regex_trailing_slash_POST_method
106106
if (0 === strpos($pathinfo, '/trailing/regex/post-method') && preg_match('#^/trailing/regex/post\\-method/(?P<param>[^/]++)/$#sD', $pathinfo, $matches)) {
107107
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_POST_method')), array ());
108-
if ('POST' !== $canonicalMethod) {
109-
$allow[] = 'POST';
108+
if (!in_array($requestMethod, array('POST'))) {
109+
$allow = array_merge($allow, array('POST'));
110110
goto not_regex_trailing_slash_POST_method;
111111
}
112112

@@ -125,8 +125,8 @@ public function match($rawPathinfo)
125125
// simple_not_trailing_slash_GET_method
126126
if ('/not-trailing/simple/get-method' === $pathinfo) {
127127
$ret = array('_route' => 'simple_not_trailing_slash_GET_method');
128-
if ('GET' !== $canonicalMethod) {
129-
$allow[] = 'GET';
128+
if (!in_array($canonicalMethod, array('GET'))) {
129+
$allow = array_merge($allow, array('GET'));
130130
goto not_simple_not_trailing_slash_GET_method;
131131
}
132132

@@ -137,8 +137,8 @@ public function match($rawPathinfo)
137137
// simple_not_trailing_slash_HEAD_method
138138
if ('/not-trailing/simple/head-method' === $pathinfo) {
139139
$ret = array('_route' => 'simple_not_trailing_slash_HEAD_method');
140-
if ('HEAD' !== $requestMethod) {
141-
$allow[] = 'HEAD';
140+
if (!in_array($requestMethod, array('HEAD'))) {
141+
$allow = array_merge($allow, array('HEAD'));
142142
goto not_simple_not_trailing_slash_HEAD_method;
143143
}
144144

@@ -149,8 +149,8 @@ public function match($rawPathinfo)
149149
// simple_not_trailing_slash_POST_method
150150
if ('/not-trailing/simple/post-method' === $pathinfo) {
151151
$ret = array('_route' => 'simple_not_trailing_slash_POST_method');
152-
if ('POST' !== $canonicalMethod) {
153-
$allow[] = 'POST';
152+
if (!in_array($requestMethod, array('POST'))) {
153+
$allow = array_merge($allow, array('POST'));
154154
goto not_simple_not_trailing_slash_POST_method;
155155
}
156156

@@ -169,8 +169,8 @@ public function match($rawPathinfo)
169169
// regex_not_trailing_slash_GET_method
170170
if (0 === strpos($pathinfo, '/not-trailing/regex/get-method') && preg_match('#^/not\\-trailing/regex/get\\-method/(?P<param>[^/]++)$#sD', $pathinfo, $matches)) {
171171
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_not_trailing_slash_GET_method')), array ());
172-
if ('GET' !== $canonicalMethod) {
173-
$allow[] = 'GET';
172+
if (!in_array($canonicalMethod, array('GET'))) {
173+
$allow = array_merge($allow, array('GET'));
174174
goto not_regex_not_trailing_slash_GET_method;
175175
}
176176

@@ -181,8 +181,8 @@ public function match($rawPathinfo)
181181
// regex_not_trailing_slash_HEAD_method
182182
if (0 === strpos($pathinfo, '/not-trailing/regex/head-method') && preg_match('#^/not\\-trailing/regex/head\\-method/(?P<param>[^/]++)$#sD', $pathinfo, $matches)) {
183183
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_not_trailing_slash_HEAD_method')), array ());
184-
if ('HEAD' !== $requestMethod) {
185-
$allow[] = 'HEAD';
184+
if (!in_array($requestMethod, array('HEAD'))) {
185+
$allow = array_merge($allow, array('HEAD'));
186186
goto not_regex_not_trailing_slash_HEAD_method;
187187
}
188188

@@ -193,8 +193,8 @@ public function match($rawPathinfo)
193193
// regex_not_trailing_slash_POST_method
194194
if (0 === strpos($pathinfo, '/not-trailing/regex/post-method') && preg_match('#^/not\\-trailing/regex/post\\-method/(?P<param>[^/]++)$#sD', $pathinfo, $matches)) {
195195
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_not_trailing_slash_POST_method')), array ());
196-
if ('POST' !== $canonicalMethod) {
197-
$allow[] = 'POST';
196+
if (!in_array($requestMethod, array('POST'))) {
197+
$allow = array_merge($allow, array('POST'));
198198
goto not_regex_not_trailing_slash_POST_method;
199199
}
200200

0 commit comments

Comments
 (0)