Skip to content

Commit af2a5e9

Browse files
Merge branch '4.0'
* 4.0: Fix typos [Routing] remove useless failing mocks [appveyor] Workaround GitHub disabling of low versions of TLS Use long array syntax [Routing] Fix GC control of PHP-DSL [Routing] Don't throw 405 when scheme requirement doesn't match [Routing] Revert throwing 405 on missed slash/scheme redirections [WebProfilerBundle] fix test after ajax path updated Fix ArrayInput::toString() for InputArgument::IS_ARRAY args Update excluded_ajax_paths for sf4 Add missing use for RoleInterface Add missing use of Role [Routing] fix CS add container.autowiring.strict_mode to 3.4 docs Set controller without __invoke method from invokable class [VarDumper] Fixed PHPDoc
2 parents 469668e + fc60a2b commit af2a5e9

14 files changed

+140
-74
lines changed

Loader/Configurator/CollectionConfigurator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ class CollectionConfigurator
2323
use Traits\RouteTrait;
2424

2525
private $parent;
26+
private $parentConfigurator;
2627

27-
public function __construct(RouteCollection $parent, string $name)
28+
public function __construct(RouteCollection $parent, string $name, self $parentConfigurator = null)
2829
{
2930
$this->parent = $parent;
3031
$this->name = $name;
3132
$this->collection = new RouteCollection();
3233
$this->route = new Route('');
34+
$this->parentConfigurator = $parentConfigurator; // for GC control
3335
}
3436

3537
public function __destruct()
@@ -45,7 +47,7 @@ final public function add(string $name, string $path): RouteConfigurator
4547
{
4648
$this->collection->add($this->name.$name, $route = clone $this->route);
4749

48-
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name);
50+
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name, $this);
4951
}
5052

5153
/**
@@ -55,7 +57,7 @@ final public function add(string $name, string $path): RouteConfigurator
5557
*/
5658
final public function collection($name = '')
5759
{
58-
return new self($this->collection, $this->name.$name);
60+
return new self($this->collection, $this->name.$name, $this);
5961
}
6062

6163
/**

Loader/Configurator/RouteConfigurator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ class RouteConfigurator
2222
use Traits\AddTrait;
2323
use Traits\RouteTrait;
2424

25-
public function __construct(RouteCollection $collection, Route $route, string $name = '')
25+
public function __construct(RouteCollection $collection, Route $route, string $name = '', CollectionConfigurator $parentConfigurator = null)
2626
{
2727
$this->collection = $collection;
2828
$this->route = $route;
2929
$this->name = $name;
30+
$this->parentConfigurator = $parentConfigurator; // for GC control
3031
}
3132
}

Loader/Configurator/Traits/AddTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ trait AddTrait
2929
*/
3030
final public function add(string $name, string $path): RouteConfigurator
3131
{
32+
$parentConfigurator = $this instanceof RouteConfigurator ? $this->parentConfigurator : null;
3233
$this->collection->add($this->name.$name, $route = new Route($path));
3334

34-
return new RouteConfigurator($this->collection, $route);
35+
return new RouteConfigurator($this->collection, $route, '', $parentConfigurator);
3536
}
3637

3738
/**

Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,26 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
538538
if ($this->supportsRedirections) {
539539
$code .= <<<EOF
540540
541-
if (\$requiredSchemes && !isset(\$requiredSchemes[\$context->getScheme()])) {
541+
\$hasRequiredScheme = !\$requiredSchemes || isset(\$requiredSchemes[\$context->getScheme()]);
542+
if (\$requiredMethods && !isset(\$requiredMethods[\$canonicalMethod]) && !isset(\$requiredMethods[\$requestMethod])) {
543+
if (\$hasRequiredScheme) {
544+
\$allow += \$requiredMethods;
545+
}
546+
break;
547+
}
548+
if (!\$hasRequiredScheme) {
542549
if ('GET' !== \$canonicalMethod) {
543-
\$allow['GET'] = 'GET';
544550
break;
545551
}
546552
547553
return \$this->redirect(\$rawPathinfo, \$ret['_route'], key(\$requiredSchemes)) + \$ret;
548554
}
549555
556+
return \$ret;
557+
550558
EOF;
551-
}
552-
$code .= <<<EOF
559+
} else {
560+
$code .= <<<EOF
553561
554562
if (\$requiredMethods && !isset(\$requiredMethods[\$canonicalMethod]) && !isset(\$requiredMethods[\$requestMethod])) {
555563
\$allow += \$requiredMethods;
@@ -559,6 +567,7 @@ private function compileSwitchDefault(bool $hasVars, bool $matchHost): string
559567
return \$ret;
560568
561569
EOF;
570+
}
562571

563572
return $code;
564573
}
@@ -637,11 +646,20 @@ private function compileRoute(Route $route, string $name, bool $checkHost): stri
637646
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
638647
}
639648
$schemes = self::export(array_flip($schemes));
640-
$code .= <<<EOF
649+
if ($methods) {
650+
$methodVariable = isset($methods['GET']) ? '$canonicalMethod' : '$requestMethod';
651+
$methods = self::export($methods);
652+
$code .= <<<EOF
641653
\$requiredSchemes = $schemes;
642-
if (!isset(\$requiredSchemes[\$context->getScheme()])) {
654+
\$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]);
655+
if (!isset((\$a = {$methods})[{$methodVariable}])) {
656+
if (\$hasRequiredScheme) {
657+
\$allow += \$a;
658+
}
659+
goto $gotoname;
660+
}
661+
if (!\$hasRequiredScheme) {
643662
if ('GET' !== \$canonicalMethod) {
644-
\$allow['GET'] = 'GET';
645663
goto $gotoname;
646664
}
647665
@@ -650,9 +668,21 @@ private function compileRoute(Route $route, string $name, bool $checkHost): stri
650668
651669
652670
EOF;
653-
}
671+
} else {
672+
$code .= <<<EOF
673+
\$requiredSchemes = $schemes;
674+
if (!isset(\$requiredSchemes[\$context->getScheme()])) {
675+
if ('GET' !== \$canonicalMethod) {
676+
goto $gotoname;
677+
}
678+
679+
return \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes)) + \$ret;
680+
}
654681
655-
if ($methods) {
682+
683+
EOF;
684+
}
685+
} elseif ($methods) {
656686
$methodVariable = isset($methods['GET']) ? '$canonicalMethod' : '$requestMethod';
657687
$methods = self::export($methods);
658688

Matcher/UrlMatcher.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
135135
continue;
136136
}
137137

138+
$status = $this->handleRouteRequirements($pathinfo, $name, $route);
139+
140+
if (self::REQUIREMENT_MISMATCH === $status[0]) {
141+
continue;
142+
}
143+
138144
// check HTTP method requirement
139145
if ($requiredMethods = $route->getMethods()) {
140146
// HEAD and GET are equivalent as per RFC
@@ -143,18 +149,14 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
143149
}
144150

145151
if (!in_array($method, $requiredMethods)) {
146-
$this->allow = array_merge($this->allow, $requiredMethods);
152+
if (self::REQUIREMENT_MATCH === $status[0]) {
153+
$this->allow = array_merge($this->allow, $requiredMethods);
154+
}
147155

148156
continue;
149157
}
150158
}
151159

152-
$status = $this->handleRouteRequirements($pathinfo, $name, $route);
153-
154-
if (self::REQUIREMENT_MISMATCH === $status[0]) {
155-
continue;
156-
}
157-
158160
return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array()));
159161
}
160162
}

RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function remove($name)
117117
* Adds a route collection at the end of the current set by appending all
118118
* routes of the added collection.
119119
*/
120-
public function addCollection(RouteCollection $collection)
120+
public function addCollection(self $collection)
121121
{
122122
// we need to remove all routes with the same names first because just replacing them
123123
// would not place the new route at the end of the merged array

Tests/Fixtures/dumper/url_matcher11.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
105105
}
106106
}
107107

108-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
108+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
109+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
110+
if ($hasRequiredScheme) {
111+
$allow += $requiredMethods;
112+
}
113+
break;
114+
}
115+
if (!$hasRequiredScheme) {
109116
if ('GET' !== $canonicalMethod) {
110-
$allow['GET'] = 'GET';
111117
break;
112118
}
113119

114120
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
115121
}
116122

117-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
118-
$allow += $requiredMethods;
119-
break;
120-
}
121-
122123
return $ret;
123124
}
124125

Tests/Fixtures/dumper/url_matcher2.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
8282
}
8383
}
8484

85-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
85+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
86+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
87+
if ($hasRequiredScheme) {
88+
$allow += $requiredMethods;
89+
}
90+
break;
91+
}
92+
if (!$hasRequiredScheme) {
8693
if ('GET' !== $canonicalMethod) {
87-
$allow['GET'] = 'GET';
8894
break;
8995
}
9096

9197
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
9298
}
9399

94-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
95-
$allow += $requiredMethods;
96-
break;
97-
}
98-
99100
return $ret;
100101
}
101102

@@ -236,20 +237,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
236237
}
237238
}
238239

239-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
240+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
241+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
242+
if ($hasRequiredScheme) {
243+
$allow += $requiredMethods;
244+
}
245+
break;
246+
}
247+
if (!$hasRequiredScheme) {
240248
if ('GET' !== $canonicalMethod) {
241-
$allow['GET'] = 'GET';
242249
break;
243250
}
244251

245252
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
246253
}
247254

248-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
249-
$allow += $requiredMethods;
250-
break;
251-
}
252-
253255
return $ret;
254256
}
255257

Tests/Fixtures/dumper/url_matcher5.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
6464
}
6565
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo];
6666

67-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
67+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
68+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
69+
if ($hasRequiredScheme) {
70+
$allow += $requiredMethods;
71+
}
72+
break;
73+
}
74+
if (!$hasRequiredScheme) {
6875
if ('GET' !== $canonicalMethod) {
69-
$allow['GET'] = 'GET';
7076
break;
7177
}
7278

7379
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
7480
}
7581

76-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
77-
$allow += $requiredMethods;
78-
break;
79-
}
80-
8182
return $ret;
8283
}
8384

@@ -106,20 +107,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
106107
}
107108
}
108109

109-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
110+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
111+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
112+
if ($hasRequiredScheme) {
113+
$allow += $requiredMethods;
114+
}
115+
break;
116+
}
117+
if (!$hasRequiredScheme) {
110118
if ('GET' !== $canonicalMethod) {
111-
$allow['GET'] = 'GET';
112119
break;
113120
}
114121

115122
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
116123
}
117124

118-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
119-
$allow += $requiredMethods;
120-
break;
121-
}
122-
123125
return $ret;
124126
}
125127

Tests/Fixtures/dumper/url_matcher7.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
6060
}
6161
list($ret, $requiredHost, $requiredMethods, $requiredSchemes) = $routes[$pathinfo];
6262

63-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
63+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
64+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
65+
if ($hasRequiredScheme) {
66+
$allow += $requiredMethods;
67+
}
68+
break;
69+
}
70+
if (!$hasRequiredScheme) {
6471
if ('GET' !== $canonicalMethod) {
65-
$allow['GET'] = 'GET';
6672
break;
6773
}
6874

6975
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
7076
}
7177

72-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
73-
$allow += $requiredMethods;
74-
break;
75-
}
76-
7778
return $ret;
7879
}
7980

@@ -118,20 +119,21 @@ private function doMatch(string $rawPathinfo, array &$allow = array()): ?array
118119
}
119120
}
120121

121-
if ($requiredSchemes && !isset($requiredSchemes[$context->getScheme()])) {
122+
$hasRequiredScheme = !$requiredSchemes || isset($requiredSchemes[$context->getScheme()]);
123+
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
124+
if ($hasRequiredScheme) {
125+
$allow += $requiredMethods;
126+
}
127+
break;
128+
}
129+
if (!$hasRequiredScheme) {
122130
if ('GET' !== $canonicalMethod) {
123-
$allow['GET'] = 'GET';
124131
break;
125132
}
126133

127134
return $this->redirect($rawPathinfo, $ret['_route'], key($requiredSchemes)) + $ret;
128135
}
129136

130-
if ($requiredMethods && !isset($requiredMethods[$canonicalMethod]) && !isset($requiredMethods[$requestMethod])) {
131-
$allow += $requiredMethods;
132-
break;
133-
}
134-
135137
return $ret;
136138
}
137139

0 commit comments

Comments
 (0)