Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89f9b24

Browse files
committed
merged branch fabpot/routing-options (PR #6738)
This PR was merged into the master branch. Commits ------- 9fc7def added the UPGRADE file for Symfony 3.0 e84cad2 [Routing] updated CHANGELOG 65eca8a [Routing] added new schemes and methods options to the annotation loader 5082994 [Routing] renamed pattern to path b357caf [Routing] renamed hostname pattern to just hostname e803f46 made schemes and methods available in XmlFileLoader d374e70 made schemes and methods available in YamlFileLoader 2834e7e added scheme and method setter in RouteCollection 10183de make scheme and method requirements first-class citizen in Route Discussion ---------- Routing options | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #5989, #5990, #6049 | License | MIT In #5989, it has unanimously been decided to renamed `hostname_pattern` to `hostname` and `pattern` to `path`. That makes a lot of sense and I would like to do the renaming now as `hostname_pattern` is new in Symfony 2.2, so I'd like to avoid breaking BC just after the release. As we are modifying the route options, I've also included changes introduced by @Tobion in #6049 which were discussed in #5990. As everything is BC, I think it's wise to include that in 2.2. What do you think? --------------------------------------------------------------------------- by Tobion at 2013-01-14T18:25:53Z I agree it should be done in 2.2. Thanks for working on it. --------------------------------------------------------------------------- by vicb at 2013-01-14T23:11:12Z @fabpot "Everything is BC" until it breaks BC in 3.0, that's why I'd like to see [deprecations in PR summary](symfony/symfony-docs#2116) what do you think ? --------------------------------------------------------------------------- by vicb at 2013-01-14T23:16:40Z it would also be great to update the CHANGELOG with deprecations (it could also help people answering your question) --------------------------------------------------------------------------- by fabpot at 2013-01-15T07:07:03Z @vicb: I've just updated the CHANGELOG and created the UPGRADE file for 3.0. --------------------------------------------------------------------------- by vicb at 2013-01-15T07:15:32Z @fabpot thanks.
2 parents 80c30fa + 9fc7def commit 89f9b24

28 files changed

+569
-154
lines changed

UPGRADE-3.0.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
UPGRADE FROM 2.x to 3.0
2+
=======================
3+
4+
### Routing
5+
6+
* Some route settings have been renamed:
7+
8+
* The `pattern` setting for a route has been deprecated in favor of `path`
9+
* The `_scheme` and `_method` requirements have been moved to the `schemes` and `methods` settings
10+
11+
Before:
12+
13+
```
14+
article_edit:
15+
pattern: /article/{id}
16+
requirements: { '_method': 'POST|PUT', '_scheme': 'https', 'id': '\d+' }
17+
18+
<route id="article_edit" pattern="/article/{id}">
19+
<requirement key="_method">POST|PUT</requirement>
20+
<requirement key="_scheme">https</requirement>
21+
<requirement key="id">\d+</requirement>
22+
</route>
23+
24+
$route = new Route();
25+
$route->setPattern('/article/{id}');
26+
$route->setRequirement('_method', 'POST|PUT');
27+
$route->setRequirement('_scheme', 'https');
28+
```
29+
30+
After:
31+
32+
```
33+
article_edit:
34+
path: /article/{id}
35+
methods: [POST, PUT]
36+
schemes: https
37+
requirements: { 'id': '\d+' }
38+
39+
<route id="article_edit" pattern="/article/{id}" methods="POST PUT" schemes="https">
40+
<requirement key="id">\d+</requirement>
41+
</route>
42+
43+
$route = new Route();
44+
$route->setPath('/article/{id}');
45+
$route->setMethods(array('POST', 'PUT'));
46+
$route->setSchemes('https');
47+
```

src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
9292
? implode(', ', $requirements['_method']) : $requirements['_method']
9393
)
9494
: 'ANY';
95-
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
95+
$hostname = '' !== $route->getHostname() ? $route->getHostname() : 'ANY';
9696
$maxName = max($maxName, strlen($name));
9797
$maxMethod = max($maxMethod, strlen($method));
9898
$maxHostname = max($maxHostname, strlen($hostname));
@@ -109,7 +109,7 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
109109
? implode(', ', $requirements['_method']) : $requirements['_method']
110110
)
111111
: 'ANY';
112-
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
112+
$hostname = '' !== $route->getHostname() ? $route->getHostname() : 'ANY';
113113
$output->writeln(sprintf($format, $name, $method, $hostname, $route->getPattern()));
114114
}
115115
}
@@ -124,14 +124,14 @@ protected function outputRoute(OutputInterface $output, $name)
124124
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
125125
}
126126

127-
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
127+
$hostname = '' !== $route->getHostname() ? $route->getHostname() : 'ANY';
128128

129129
$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
130130

131-
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
132-
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
133-
$output->writeln(sprintf('<comment>HostnamePattern</comment> %s', $hostname));
134-
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
131+
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
132+
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
133+
$output->writeln(sprintf('<comment>Hostname</comment> %s', $hostname));
134+
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
135135

136136
$defaults = '';
137137
$d = $route->getDefaults();

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function warmUp($cacheDir)
7878
* - the route defaults,
7979
* - the route requirements,
8080
* - the route pattern.
81-
* - the route hostnamePattern.
81+
* - the route hostname.
8282
*
8383
* @param RouteCollection $collection
8484
*/
@@ -94,7 +94,7 @@ private function resolveParameters(RouteCollection $collection)
9494
}
9595

9696
$route->setPattern($this->resolve($route->getPattern()));
97-
$route->setHostnamePattern($this->resolve($route->getHostnamePattern()));
97+
$route->setHostname($this->resolve($route->getHostname()));
9898
}
9999
}
100100

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ public function testPatternPlaceholders()
117117
);
118118
}
119119

120-
public function testHostnamePatternPlaceholders()
120+
public function testHostnamePlaceholders()
121121
{
122122
$routes = new RouteCollection();
123123

124124
$route = new Route('foo');
125-
$route->setHostnamePattern('/before/%parameter.foo%/after/%%unescaped%%');
125+
$route->setHostname('/before/%parameter.foo%/after/%%unescaped%%');
126126

127127
$routes->add('foo', $route);
128128

@@ -136,7 +136,7 @@ public function testHostnamePatternPlaceholders()
136136

137137
$this->assertEquals(
138138
'/before/foo/after/%unescaped%',
139-
$route->getHostnamePattern()
139+
$route->getHostname()
140140
);
141141
}
142142

src/Symfony/Component/Routing/Annotation/Route.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
*/
2121
class Route
2222
{
23-
private $pattern;
23+
private $path;
2424
private $name;
2525
private $requirements;
2626
private $options;
2727
private $defaults;
28-
private $hostnamePattern;
28+
private $hostname;
29+
private $methods;
30+
private $schemes;
2931

3032
/**
3133
* Constructor.
@@ -39,9 +41,11 @@ public function __construct(array $data)
3941
$this->requirements = array();
4042
$this->options = array();
4143
$this->defaults = array();
44+
$this->methods = array();
45+
$this->schemes = array();
4246

4347
if (isset($data['value'])) {
44-
$data['pattern'] = $data['value'];
48+
$data['path'] = $data['value'];
4549
unset($data['value']);
4650
}
4751

@@ -54,24 +58,40 @@ public function __construct(array $data)
5458
}
5559
}
5660

61+
/**
62+
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead.
63+
*/
5764
public function setPattern($pattern)
5865
{
59-
$this->pattern = $pattern;
66+
$this->path = $pattern;
6067
}
6168

69+
/**
70+
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use getPath instead.
71+
*/
6272
public function getPattern()
6373
{
64-
return $this->pattern;
74+
return $this->path;
6575
}
6676

67-
public function setHostnamePattern($pattern)
77+
public function setPath($path)
6878
{
69-
$this->hostnamePattern = $pattern;
79+
$this->path = $path;
7080
}
7181

72-
public function getHostnamePattern()
82+
public function getPath()
7383
{
74-
return $this->hostnamePattern;
84+
return $this->path;
85+
}
86+
87+
public function setHostname($pattern)
88+
{
89+
$this->hostname = $pattern;
90+
}
91+
92+
public function getHostname()
93+
{
94+
return $this->hostname;
7595
}
7696

7797
public function setName($name)
@@ -113,4 +133,24 @@ public function getDefaults()
113133
{
114134
return $this->defaults;
115135
}
136+
137+
public function setSchemes($schemes)
138+
{
139+
$this->schemes = is_array($schemes) ? $schemes : array($schemes);
140+
}
141+
142+
public function getSchemes()
143+
{
144+
return $this->schemes;
145+
}
146+
147+
public function setMethods($methods)
148+
{
149+
$this->methods = is_array($methods) ? $methods : array($methods);
150+
}
151+
152+
public function getMethods()
153+
{
154+
return $this->methods;
155+
}
116156
}

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@ CHANGELOG
44
2.2.0
55
-----
66

7+
* [DEPRECATION] Several route settings have been renamed (the old ones will be removed in 3.0):
8+
9+
* The `pattern` setting for a route has been deprecated in favor of `path`
10+
* The `_scheme` and `_method` requirements have been moved to the `schemes` and `methods` settings
11+
12+
Before:
13+
14+
```
15+
article_edit:
16+
pattern: /article/{id}
17+
requirements: { '_method': 'POST|PUT', '_scheme': 'https', 'id': '\d+' }
18+
19+
<route id="article_edit" pattern="/article/{id}">
20+
<requirement key="_method">POST|PUT</requirement>
21+
<requirement key="_scheme">https</requirement>
22+
<requirement key="id">\d+</requirement>
23+
</route>
24+
25+
$route = new Route();
26+
$route->setPattern('/article/{id}');
27+
$route->setRequirement('_method', 'POST|PUT');
28+
$route->setRequirement('_scheme', 'https');
29+
```
30+
31+
After:
32+
33+
```
34+
article_edit:
35+
path: /article/{id}
36+
methods: [POST, PUT]
37+
schemes: https
38+
requirements: { 'id': '\d+' }
39+
40+
<route id="article_edit" pattern="/article/{id}" methods="POST PUT" schemes="https">
41+
<requirement key="id">\d+</requirement>
42+
</route>
43+
44+
$route = new Route();
45+
$route->setPath('/article/{id}');
46+
$route->setMethods(array('POST', 'PUT'));
47+
$route->setSchemes('https');
48+
```
49+
750
* [BC BREAK] RouteCollection does not behave like a tree structure anymore but as
851
a flat array of Routes. So when using PHP to build the RouteCollection, you must
952
make sure to add routes to the sub-collection before adding it to the parent

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

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
* The @Route annotation can be set on the class (for global parameters),
2929
* and on each method.
3030
*
31-
* The @Route annotation main value is the route pattern. The annotation also
32-
* recognizes three parameters: requirements, options, and name. The name parameter
33-
* is mandatory. Here is an example of how you should be able to use it:
31+
* The @Route annotation main value is the route path. The annotation also
32+
* recognizes several parameters: requirements, options, defaults, schemes,
33+
* methods, hostname, and name. The name parameter is mandatory.
34+
* Here is an example of how you should be able to use it:
3435
*
3536
* /**
3637
* * @Route("/Blog")
@@ -108,11 +109,13 @@ public function load($class, $type = null)
108109
}
109110

110111
$globals = array(
111-
'pattern' => '',
112-
'requirements' => array(),
113-
'options' => array(),
114-
'defaults' => array(),
115-
'hostname_pattern' => '',
112+
'path' => '',
113+
'requirements' => array(),
114+
'options' => array(),
115+
'defaults' => array(),
116+
'schemes' => array(),
117+
'methods' => array(),
118+
'hostname' => '',
116119
);
117120

118121
$class = new \ReflectionClass($class);
@@ -121,8 +124,11 @@ public function load($class, $type = null)
121124
}
122125

123126
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
124-
if (null !== $annot->getPattern()) {
125-
$globals['pattern'] = $annot->getPattern();
127+
// for BC reasons
128+
if (null !== $annot->getPath()) {
129+
$globals['path'] = $annot->getPath();
130+
} elseif (null !== $annot->getPattern()) {
131+
$globals['path'] = $annot->getPattern();
126132
}
127133

128134
if (null !== $annot->getRequirements()) {
@@ -137,8 +143,16 @@ public function load($class, $type = null)
137143
$globals['defaults'] = $annot->getDefaults();
138144
}
139145

140-
if (null !== $annot->getHostnamePattern()) {
141-
$globals['hostname_pattern'] = $annot->getHostnamePattern();
146+
if (null !== $annot->getSchemes()) {
147+
$globals['schemes'] = $annot->getSchemes();
148+
}
149+
150+
if (null !== $annot->getMethods()) {
151+
$globals['methods'] = $annot->getMethods();
152+
}
153+
154+
if (null !== $annot->getHostname()) {
155+
$globals['hostname'] = $annot->getHostname();
142156
}
143157
}
144158

@@ -172,13 +186,15 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
172186
}
173187
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
174188
$options = array_replace($globals['options'], $annot->getOptions());
189+
$schemes = array_replace($globals['schemes'], $annot->getSchemes());
190+
$methods = array_replace($globals['methods'], $annot->getMethods());
175191

176-
$hostnamePattern = $annot->getHostnamePattern();
177-
if (null === $hostnamePattern) {
178-
$hostnamePattern = $globals['hostname_pattern'];
192+
$hostname = $annot->getHostname();
193+
if (null === $hostname) {
194+
$hostname = $globals['hostname'];
179195
}
180196

181-
$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options, $hostnamePattern);
197+
$route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $hostname, $schemes, $methods);
182198

183199
$this->configureRoute($route, $class, $method, $annot);
184200

0 commit comments

Comments
 (0)