Skip to content

Commit a505ff4

Browse files
committed
[Routing] added supported for multiple route annotations for a single controller
1 parent e35832e commit a505ff4

File tree

2 files changed

+85
-18
lines changed

2 files changed

+85
-18
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Annotation;
13+
14+
/**
15+
* Annotation class for @Routes().
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
class Routes
20+
{
21+
protected $routes;
22+
23+
/**
24+
* Constructor.
25+
*
26+
* @param array $data An array of key/value parameters.
27+
*/
28+
public function __construct(array $data)
29+
{
30+
if (!isset($data['value']) || !is_array($data['value'])) {
31+
throw new \LogicException('A @Routes annotation must have an array of @Route annotation as argument.');
32+
}
33+
34+
$this->routes = $data['value'];
35+
}
36+
37+
public function setRoutes($routes)
38+
{
39+
$this->routes = $routes;
40+
}
41+
42+
public function getRoutes()
43+
{
44+
return $this->routes;
45+
}
46+
}

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

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
abstract class AnnotationClassLoader implements LoaderInterface
5959
{
6060
protected $reader;
61-
protected $annotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
61+
protected $routeAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Route';
62+
protected $routesAnnotationClass = 'Symfony\\Component\\Routing\\Annotation\\Routes';
6263

6364
/**
6465
* Constructor.
@@ -73,11 +74,21 @@ public function __construct(AnnotationReader $reader)
7374
/**
7475
* Sets the annotation class to read route properties from.
7576
*
76-
* @param string $annotationClass A fully-qualified class name
77+
* @param string $class A fully-qualified class name
7778
*/
78-
public function setAnnotationClass($annotationClass)
79+
public function setRouteAnnotationClass($class)
7980
{
80-
$this->annotationClass = $annotationClass;
81+
$this->routeAnnotationClass = $class;
82+
}
83+
84+
/**
85+
* Sets the annotation class to read routes properties from.
86+
*
87+
* @param string $class A fully-qualified class name
88+
*/
89+
public function setRoutesAnnotationClass($class)
90+
{
91+
$this->routesAnnotationClass = $class;
8192
}
8293

8394
/**
@@ -104,7 +115,7 @@ public function load($class, $type = null)
104115
);
105116

106117
$class = new \ReflectionClass($class);
107-
if ($annot = $this->reader->getClassAnnotation($class, $this->annotationClass)) {
118+
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
108119
if (null !== $annot->getPattern()) {
109120
$globals['pattern'] = $annot->getPattern();
110121
}
@@ -124,25 +135,35 @@ public function load($class, $type = null)
124135

125136
$collection = new RouteCollection();
126137
$collection->addResource(new FileResource($class->getFileName()));
138+
127139
foreach ($class->getMethods() as $method) {
128-
if ($annot = $this->reader->getMethodAnnotation($method, $this->annotationClass)) {
129-
if (null === $annot->getName()) {
130-
$annot->setName($this->getDefaultRouteName($class, $method));
140+
if ($annots = $this->reader->getMethodAnnotation($method, $this->routesAnnotationClass)) {
141+
foreach ($annots->getRoutes() as $annot) {
142+
$this->addRoute($collection, $annot, $globals, $annot, $class, $method);
131143
}
144+
} elseif ($annot = $this->reader->getMethodAnnotation($method, $this->routeAnnotationClass)) {
145+
$this->addRoute($collection, $annot, $globals, $annot, $class, $method);
146+
}
147+
}
132148

133-
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
134-
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
135-
$options = array_merge($globals['options'], $annot->getOptions());
149+
return $collection;
150+
}
151+
152+
protected function addRoute(RouteCollection $collection, $annot, $globals, $annot, \ReflectionClass $class, \ReflectionMethod $method)
153+
{
154+
if (null === $annot->getName()) {
155+
$annot->setName($this->getDefaultRouteName($class, $method));
156+
}
136157

137-
$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
158+
$defaults = array_merge($globals['defaults'], $annot->getDefaults());
159+
$requirements = array_merge($globals['requirements'], $annot->getRequirements());
160+
$options = array_merge($globals['options'], $annot->getOptions());
138161

139-
$this->configureRoute($route, $class, $method);
162+
$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options);
140163

141-
$collection->add($annot->getName(), $route);
142-
}
143-
}
164+
$this->configureRoute($route, $class, $method, $annot);
144165

145-
return $collection;
166+
$collection->add($annot->getName(), $route);
146167
}
147168

148169
/**
@@ -189,5 +210,5 @@ protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMetho
189210
return strtolower(str_replace('\\', '_', $class->getName()).'_'.$method->getName());
190211
}
191212

192-
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method);
213+
abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot);
193214
}

0 commit comments

Comments
 (0)