8000 [Routing] renamed pattern to path · symfony/symfony@5082994 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5082994

Browse files
committed
[Routing] renamed pattern to path
1 parent b357caf commit 5082994

17 files changed

+136
-69
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class Route
2222
{
23-
private $pattern;
23+
private $path;
2424
private $name;
2525
private $requirements;
2626
private $options;
@@ -39,7 +39,7 @@ public function __construct(array $data)
3939
$this->defaults = array();
4040

4141
if (isset($data['value'])) {
42-
$data['pattern'] = $data['value'];
42+
$data['path'] = $data['value'];
4343
unset($data['value']);
4444
}
4545

@@ -52,14 +52,30 @@ public function __construct(array $data)
5252
}
5353
}
5454

55+
/**
56+
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead.
57+
*/
5558
public function setPattern($pattern)
5659
{
57-
$this->pattern = $pattern;
60+
$this->path = $pattern;
5861
}
5962

63+
/**
64+
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use getPath instead.
65+
*/
6066
public function getPattern()
6167
{
62-
return $this->pattern;
68+
return $this->path;
69+
}
70+
71+
public function setPath($path)
72+
{
73+
$this->path = $path;
74+
}
75+
76+
public function getPath()
77+
{
78+
return $this->path;
6379
}
6480

6581
public function setHostname($pattern)

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
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
31+
* The @Route annotation main value is the route path. The annotation also
3232
* recognizes three parameters: requirements, options, and name. The name parameter
3333
* is mandatory. Here is an example of how you should be able to use it:
3434
*
@@ -108,7 +108,7 @@ public function load($class, $type = null)
108108
}
109109

110110
$globals = array(
111-
'pattern' => '',
111+
'path' => '',
112112
'requirements' => array(),
113113
'options' => array(),
114114
'defaults' => array(),
@@ -121,8 +121,11 @@ public function load($class, $type = null)
121121
}
122122

123123
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
124-
if (null !== $annot->getPattern()) {
125-
$globals['pattern'] = $annot->getPattern();
124+
// for BC reasons
125+
if (null !== $annot->getPath()) {
126+
$globals['path'] = $annot->getPath();
127+
} elseif (null !== $annot->getPattern()) {
128+
$globals['path'] = $annot->getPattern();
126129
}
127130

128131
if (null !== $annot->getRequirements()) {
@@ -178,7 +181,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
178181
$hostname = $globals['hostname'];
179182
}
180183

181-
$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options, $hostname);
184+
$route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $hostname);
182185

183186
$this->configureRoute($route, $class, $method, $annot);
184187

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,25 @@ public function supports($resource, $type = null)
112112
*/
113113
protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
114114
{
115-
if ('' === ($id = $node->getAttribute('id')) || !$node->hasAttribute('pattern')) {
116-
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "pattern" attribute.', $path));
115+
if ('' === ($id = $node->getAttribute('id')) || (!$node->hasAttribute('pattern') && !$node->hasAttribute('path'))) {
116+
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "path" attribute.', $path));
117+
}
118+
119+
if ($node->hasAttribute('pattern')) {
120+
if ($node->hasAttribute('path')) {
121+
throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
122+
}
123+
124+
$node->setAttribute('path', $node->getAttribute('pattern'));
125+
$node->removeAttribute('pattern');
117126
}
118127

119128
$schemes = array_filter(explode(' ', $node->getAttribute('schemes')));
120129
$methods = array_filter(explode(' ', $node->getAttribute('methods')));
121130

122131
list($defaults, $requirements, $options) = $this->parseConfigs($node, $path);
123132

124-
$route = new Route($node->getAttribute('pattern'), $defaults, $requirements, $options, $node->getAttribute('hostname'), $schemes, $methods);
133+
$route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('hostname'), $schemes, $methods);
125134
$collection->add($id, $route);
126135
}
127136

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class YamlFileLoader extends FileLoader
2929
{
3030
private static $availableKeys = array(
31-
'resource', 'type', 'prefix', 'pattern', 'hostname', 'schemes', 'methods', 'defaults', 'requirements', 'options',
31+
'resource', 'type', 'prefix', 'pattern', 'path', 'hostname', 'schemes', 'methods', 'defaults', 'requirements', 'options',
3232
);
3333

3434
/**
@@ -63,6 +63,15 @@ public function load($file, $type = null)
6363
}
6464

6565
foreach ($config as $name => $config) {
66+
if (isset($config['pattern'])) {
67+
if (isset($config['path'])) {
68+
throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
69+
}
70+
71+
$config['path'] = $config['pattern'];
72+
unset($config['pattern']);
73+
}
74+
6675
$this->validate($config, $name, $path);
6776

6877
if (isset($config['resource'])) {
@@ -102,7 +111,7 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
102111
$schemes = isset($config['schemes']) ? $config['schemes'] : array();
103112
$methods = isset($config['methods']) ? $config['methods'] : array();
104113

105-
$route = new Route($config['pattern'], $defaults, $requirements, $options, $hostname, $schemes, $methods);
114+
$route = new Route($config['path'], $defaults, $requirements, $options, $hostname, $schemes, $methods);
106115

107116
$collection->add($name, $route);
108117
}
@@ -168,9 +177,9 @@ protected function validate($config, $name, $path)
168177
$path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
169178
));
170179
}
171-
if (isset($config['resource']) && isset($config['pattern'])) {
180+
if (isset($config['resource']) && isset($config['path'])) {
172181
throw new \InvalidArgumentException(sprintf(
173-
'The routing file "%s" must not specify both the "resource" key and the "pattern" key for "%s". Choose between an import and a route definition.',
182+
'The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.',
174183
$path, $name
175184
));
176185
}
@@ -180,9 +189,9 @@ protected function validate($config, $name, $path)
180189
$name, $path
181190
));
182191
}
183-
if (!isset($config['resource']) && !isset($config['pattern'])) {
192+
if (!isset($config['resource']) && !isset($config['path'])) {
184193
throw new \InvalidArgumentException(sprintf(
185-
'You must define a "pattern" for the route "%s" in file "%s".',
194+
'You must define a "path" for the route "%s" in file "%s".',
186195
$name, $path
187196
));
188197
}

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
<xsd:group ref="configs" minOccurs="0" maxOccurs="unbounded" />
4747

4848
<xsd:attribute name="id" type="xsd:string" use="required" />
49-
<xsd:attribute name="pattern" type="xsd:string" use="required" />
49+
<xsd:attribute name="path" type="xsd:string" />
50+
<xsd:attribute name="pattern" type="xsd:string" />
5051
<xsd:attribute name="hostname" type="xsd:string" />
5152
<xsd:attribute name="schemes" type="stringlist" />
5253
<xsd:attribute name="methods" type="stringlist" />

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
4848

4949
if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
5050
// does it match without any requirements?
51-
$r = new Route($route->getPattern(), $route->getDefaults(), array(), $route->getOptions());
51+
$r = new Route($route->getPath(), $route->getDefaults(), array(), $route->getOptions());
5252
$cr = $r->compile();
5353
if (!preg_match($cr->getRegex(), $pathinfo)) {
54-
$this->addTrace(sprintf('Pattern "%s" does not match', $route->getPattern()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
54+
$this->addTrace(sprintf('Path "%s" does not match', $route->getPath()), self::ROUTE_DOES_NOT_MATCH, $name, $route);
5555

5656
continue;
5757
}
5858

5959
foreach ($route->getRequirements() as $n => $regex) {
60-
$r = new Route($route->getPattern(), $route->getDefaults(), array($n => $regex), $route->getOptions());
60+
$r = new Route($route->getPath(), $route->getDefaults(), array($n => $regex), $route->getOptions());
6161
$cr = $r->compile();
6262

6363
if (in_array($n, $cr->getVariables()) && !preg_match($cr->getRegex(), $pathinfo)) {
@@ -104,10 +104,10 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
104104
private function addTrace($log, $level = self::ROUTE_DOES_NOT_MATCH, $name = null, $route = null)
105105
{
106106
$this->traces[] = array(
107-
'log' => $log,
108-
'name' => $name,
109-
'level' => $level,
110-
'pattern' => null !== $route ? $route->getPattern() : null,
107+
'log' => $log,
108+
'name' => $name,
109+
'level' => $level,
110+
'path' => null !== $route ? $route->getPath() : null,
111111
);
112112
}
113113
}

src/Symfony/Component/Routing/Route.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Route implements \Serializable
2424
/**
2525
* @var string
2626
*/
27-
private $pattern = '/';
27+
private $path = '/';
2828

2929
/**
3030
* @var string
@@ -70,7 +70,7 @@ class Route implements \Serializable
7070
*
7171
* * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
7272
*
73-
* @param string $pattern The path pattern to match
73+
* @param string $path The path pattern to match
7474
* @param array $defaults An array of default parameter values
7575
* @param array $requirements An array of requirements for parameters (regexes)
7676
* @param array $options An array of options
@@ -80,9 +80,9 @@ class Route implements \Serializable
8080
*
8181
* @api
8282
*/
83-
public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array(), $hostname = '', $schemes = array(), $methods = array())
83+
public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $hostname = '', $schemes = array(), $methods = array())
8484
{
85-
$this->setPattern($pattern);
85+
$this->setPath($path);
8686
$this->setDefaults($defaults);
8787
$this->setRequirements($requirements);
8888
$this->setOptions($options);
@@ -100,7 +100,7 @@ public function __construct($pattern, array $defaults = array(), array $requirem
100100
public function serialize()
101101
{
102102
return serialize(array(
103-
'pattern' => $this->pattern,
103+
'path' => $this->path,
104104
'hostname' => $this->hostname,
105105
'defaults' => $this->defaults,
106106
'requirements' => $this->requirements,
@@ -113,7 +113,7 @@ public function serialize()
113113
public function unserialize($data)
114114
{
115115
$data = unserialize($data);
116-
$this->pattern = $data['pattern'];
116+
$this->path = $data['path'];
117117
$this->hostname = $data['hostname'];
118118
$this->defaults = $data['defaults'];
119119
$this->requirements = $data['requirements'];
@@ -126,10 +126,12 @@ public function unserialize($data)
126126
* Returns the pattern for the path.
127127
*
128128
* @return string The pattern
129+
*
130+
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead.
129131
*/
130132
public function getPattern()
131133
{
132-
return $this->pattern;
134+
return $this->path;
133135
}
134136

135137
/**
@@ -140,12 +142,38 @@ public function getPattern()
140142
* @param string $pattern The pattern
141143
*
142144
* @return Route The current Route instance
145+
*
146+
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead.
143147
*/
144148
public function setPattern($pattern)
149+
{
150+
return $this->setPath($pattern);
151+
}
152+
153+
/**
154+
* Returns the pattern for the path.
155+
*
156+
* @return string The path pattern
157+
*/
158+
public function getPath()
159+
{
160+
return $this->path;
161+
}
162+
163+
/**
164+
* Sets the pattern for the path.
165+
*
166+
* This method implements a fluent interface.
167+
*
168+
* @param string $path The path pattern
169+
*
170+
* @return Route The current Route instance
171+
*/
172+
public function setPath($path)
145173
{
146174
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
147175
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
148-
$this->pattern = '/' . ltrim(trim($pattern), '/');
176+
$this->path = '/' . ltrim(trim($path), '/');
149177
$this->compiled = null;
150178

151179
return $this;

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
235235
$options = func_num_args() > 3 ? func_get_arg(3) : array();
236236

237237
foreach ($this->routes as $route) {
238-
$route->setPattern('/' . $prefix . $route->getPattern());
238+
$route->setPath('/' . $prefix . $route->getPath());
239239
$route->addDefaults($defaults);
240240
$route->addRequirements($requirements);
241241
$route->addOptions($options);

src/Symfony/Component/Routing/RouteCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function compile(Route $route)
5656
$hostnameRegex = $result['regex'];
5757
}
5858

59-
$pattern = $route->getPattern();
59+
$path = $route->getPath();
6060

61-
$result = $this->compilePattern($route, $pattern, false);
61+
$result = $this->compilePattern($route, $path, false);
6262

6363
$staticPrefix = $result['staticPrefix'];
6464

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function getValidParameters()
3636
{
3737
return array(
3838
array('value', '/Blog', 'getPattern'),
39+
array('value', '/Blog', 'getPath'),
3940
array('requirements', array('_method' => 'GET'), 'getRequirements'),
4041
array('options', array('compiler_class' => 'RouteCompiler'), 'getOptions'),
4142
array('name', 'blog_index', 'getName'),

src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testLoad($className, $routeDatas = array(), $methodArgs = array(
8989
{
9090
$routeDatas = array_replace(array(
9191
'name' => 'route',
92-
'pattern' => '/',
92+
'path' => '/',
9393
'requirements' => array(),
9494
'options' => array(),
9595
'defaults' => array(),
@@ -103,7 +103,7 @@ public function testLoad($className, $routeDatas = array(), $methodArgs = array(
103103
$routeCollection = $this->loader->load($className);
104104
$route = $routeCollection->get($routeDatas['name']);
105105

106-
$this->assertSame($routeDatas['pattern'], $route->getPattern(), '->load preserves pattern annotation');
106+
$this->assertSame($routeDatas['path'], $route->getPath(), '->load preserves path annotation');
107107
$this->assertSame($routeDatas['requirements'],$route->getRequirements(), '->load preserves requirements annotation');
108108
$this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
109109
$this->assertSame(array_replace($routeDatas['defaults'], $methodArgs), $route->getDefaults(), '->load preserves defaults annotation');

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testLoadWithRoute()
4343
$this->assertEquals(1, count($routes), 'One route is loaded');
4444
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
4545
$route = $routes['blog_show'];
46-
$this->assertEquals('/blog/{slug}', $route->getPattern());
46+
$this->assertEquals('/blog/{slug}', $route->getPath());
4747
$this->assertEquals('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
4848
$this->assertEquals('GET', $route->getRequirement('_method'));
4949
$this->assertEquals('{locale}.example.com', $route->getHostname());

0 commit comments

Comments
 (0)
0