Skip to content

Commit bc1c5c8

Browse files
committed
[Routing] apply deprecation triggers and fix tests
1 parent 7192b2f commit bc1c5c8

27 files changed

+203
-201
lines changed

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p
122122
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
123123
}
124124

125+
trigger_error('The "pattern" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', E_USER_DEPRECATED);
126+
125127
$node->setAttribute('path', $node->getAttribute('pattern'));
126128
$node->removeAttribute('pattern');
127129
}

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function load($file, $type = null)
8181
throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
8282
}
8383

84+
trigger_error('The "pattern" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', E_USER_DEPRECATED);
85+
8486
$config['path'] = $config['pattern'];
8587
unset($config['pattern']);
8688
}

src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ public function dump(array $options = array())
9090

9191
$rules[] = $this->dumpRoute($name, $route, $options, $hostRegexUnique);
9292

93-
if ($req = $route->getRequirement('_method')) {
94-
$methods = explode('|', strtoupper($req));
95-
$methodVars = array_merge($methodVars, $methods);
96-
}
93+
$methodVars = array_merge($methodVars, $route->getMethods());
9794
}
9895
if (0 < count($methodVars)) {
9996
$rule = array('# 405 Method Not Allowed');
@@ -200,13 +197,11 @@ private function dumpRoute($name, $route, array $options, $hostRegexUnique)
200197
*/
201198
private function getRouteMethods(Route $route)
202199
{
203-
$methods = array();
204-
if ($req = $route->getRequirement('_method')) {
205-
$methods = explode('|', strtoupper($req));
206-
// GET and HEAD are equivalent
207-
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
208-
$methods[] = 'HEAD';
209-
}
200+
$methods = $route->getMethods();
201+
202+
// GET and HEAD are equivalent
203+
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
204+
$methods[] = 'HEAD';
210205
}
211206

212207
return $methods;

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,11 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
215215
$hasTrailingSlash = false;
216216
$matches = false;
217217
$hostMatches = false;
218-
$methods = array();
218+
$methods = $route->getMethods();
219219

220-
if ($req = $route->getRequirement('_method')) {
221-
$methods = explode('|', strtoupper($req));
222-
// GET and HEAD are equivalent
223-
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
224-
$methods[] = 'HEAD';
225-
}
220+
// GET and HEAD are equivalent
221+
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
222+
$methods[] = 'HEAD';
226223
}
227224

228225
$supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('HEAD', $methods));

src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
7878
}
7979

8080
// check HTTP method requirement
81-
if ($req = $route->getRequirement('_method')) {
81+
if ($requiredMethods = $route->getMethods()) {
8282
// HEAD and GET are equivalent as per RFC
8383
if ('HEAD' === $method = $this->context->getMethod()) {
8484
$method = 'GET';
8585
}
8686

87-
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
88-
$this->allow = array_merge($this->allow, $req);
87+
if (!in_array($method, $requiredMethods)) {
88+
$this->allow = array_merge($this->allow, $requiredMethods);
8989

90-
$this->addTrace(sprintf('Method "%s" does not match the requirement ("%s")', $this->context->getMethod(), implode(', ', $req)), self::ROUTE_ALMOST_MATCHES, $name, $route);
90+
$this->addTrace(sprintf('Method "%s" does not match any of the required methods (%s)', $this->context->getMethod(), implode(', ', $requiredMethods)), self::ROUTE_ALMOST_MATCHES, $name, $route);
9191

9292
continue;
9393
}
@@ -107,7 +107,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
107107
$scheme = $this->context->getScheme();
108108

109109
if (!$route->hasScheme($scheme)) {
110-
$this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes ("%s"); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route);
110+
$this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes (%s); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route);
111111

112112
return true;
113113
}

src/Symfony/Component/Routing/Matcher/UrlMatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function match($pathinfo)
9898
}
9999

100100
throw 0 < count($this->allow)
101-
? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow)))
101+
? new MethodNotAllowedException(array_unique($this->allow))
102102
: new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
103103
}
104104

@@ -152,14 +152,14 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
152152
}
153153

154154
// check HTTP method requirement
155-
if ($req = $route->getRequirement('_method')) {
155+
if ($requiredMethods = $route->getMethods()) {
156156
// HEAD and GET are equivalent as per RFC
157157
if ('HEAD' === $method = $this->context->getMethod()) {
158158
$method = 'GET';
159159
}
160160

161-
if (!in_array($method, $req = explode('|', strtoupper($req)))) {
162-
$this->allow = array_merge($this->allow, $req);
161+
if (!in_array($method, $requiredMethods)) {
162+
$this->allow = array_merge($this->allow, $requiredMethods);
163163

164164
continue;
165165
}

src/Symfony/Component/Routing/Route.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function unserialize($serialized)
151151
*/
152152
public function getPattern()
153153
{
154-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getPath() method instead and use the "path" option instead of the "pattern" option in the route definition.', E_USER_DEPRECATED);
154+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getPath() method instead.', E_USER_DEPRECATED);
155155

156156
return $this->path;
157157
}
@@ -169,7 +169,7 @@ public function getPattern()
169169
*/
170170
public function setPattern($pattern)
171171
{
172-
trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the setPath() method instead and use the "path" option instead of the "pattern" option in the route definition.', E_USER_DEPRECATED);
172+
trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the setPath() method instead.', E_USER_DEPRECATED);
173173

174174
return $this->setPath($pattern);
175175
}
@@ -548,6 +548,12 @@ public function addRequirements(array $requirements)
548548
*/
549549
public function getRequirement($key)
550550
{
551+
if ('_scheme' === $key) {
552+
trigger_error('The "_scheme" requirement is deprecated since version 2.2 and will be removed in 3.0. Use getSchemes() instead.', E_USER_DEPRECATED);
553+
} elseif ('_method' === $key) {
554+
trigger_error('The "_method" requirement is deprecated since version 2.2 and will be removed in 3.0. Use getMethods() instead.', E_USER_DEPRECATED);
555+
}
556+
551557
return isset($this->requirements[$key]) ? $this->requirements[$key] : null;
552558
}
553559

@@ -649,8 +655,12 @@ private function sanitizeRequirement($key, $regex)
649655

650656
// this is to keep BC and will be removed in a future version
651657
if ('_scheme' === $key) {
658+
trigger_error('The "_scheme" requirement is deprecated since version 2.2 and will be removed in 3.0. Use the setSchemes() method instead or the "schemes" option in the route definition.', E_USER_DEPRECATED);
659+
652660
$this->setSchemes(explode('|', $regex));
653661
} elseif ('_method' === $key) {
662+
trigger_error('The "_method" requirement is deprecated since version 2.2 and will be removed in 3.0. Use the setMethods() method instead or the "methods" option in the route definition.', E_USER_DEPRECATED);
663+
654664
$this->setMethods(explode('|', $regex));
655665
}
656666

src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function getValidParameters()
3636
{
3737
return array(
3838
array('value', '/Blog', 'getPath'),
39-
array('requirements', array('_method' => 'GET'), 'getRequirements'),
39+
array('requirements', array('locale' => 'en'), 'getRequirements'),
4040
array('options', array('compiler_class' => 'RouteCompiler'), 'getOptions'),
4141
array('name', 'blog_index', 'getName'),
4242
array('defaults', array('_controller' => 'MyBlogBundle:Blog:index'), 'getDefaults'),
4343
array('schemes', array('https'), 'getSchemes'),
4444
array('methods', array('GET', 'POST'), 'getMethods'),
45-
array('host', array('{locale}.example.com'), 'getHost'),
46-
array('condition', array('context.getMethod() == "GET"'), 'getCondition'),
45+
array('host', '{locale}.example.com', 'getHost'),
46+
array('condition', 'context.getMethod() == "GET"', 'getCondition'),
4747
);
4848
}
4949

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<routes xmlns="http://symfony.com/schema/routing"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<route id="blog_show_legacy" pattern="/blog/{slug}" host="{locale}.example.com">
8+
<default key="_controller">MyBundle:Blog:show</default>
9+
<default key="slug" xsi:nil="true" />
10+
<requirement key="_method">GET|POST|put|OpTiOnS</requirement>
11+
<requirement key="_scheme">hTTps</requirement>
12+
<requirement key="locale">\w+</requirement>
13+
<option key="compiler_class">RouteCompiler</option>
14+
<condition>context.getMethod() == "GET"</condition>
15+
</route>
16+
</routes>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
blog_show_legacy:
2+
pattern: /blog/{slug}
3+
defaults: { _controller: "MyBundle:Blog:show" }
4+
host: "{locale}.example.com"
5+
requirements: { '_method': 'GET|POST|put|OpTiOnS', _scheme: https, 'locale': '\w+' }
6+
condition: 'context.getMethod() == "GET"'
7+
options:
8+
compiler_class: RouteCompiler

0 commit comments

Comments
 (0)