-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Add support for 'priority' option for annotated Routes #11753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,11 @@ abstract class AnnotationClassLoader implements LoaderInterface | |
*/ | ||
protected $defaultRouteIndex = 0; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $routesWithPriority = array(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -92,6 +97,11 @@ public function setRouteAnnotationClass($class) | |
$this->routeAnnotationClass = $class; | ||
} | ||
|
||
public function hasRoutesWithPriority() | ||
{ | ||
return !empty($this->routesWithPriority); | ||
} | ||
|
||
/** | ||
* Loads from annotations from a class. | ||
* | ||
|
@@ -122,6 +132,10 @@ public function load($class, $type = null) | |
$this->defaultRouteIndex = 0; | ||
foreach ($this->reader->getMethodAnnotations($method) as $annot) { | ||
if ($annot instanceof $this->routeAnnotationClass) { | ||
if ($annot->getOption('priority')) { | ||
$this->routesWithPriority[$annot->getOption('priority')][] | ||
= array($annot, $globals, $class, $method); | ||
} | ||
$this->addRoute($collection, $annot, $globals, $class, $method); | ||
} | ||
} | ||
|
@@ -130,6 +144,20 @@ public function load($class, $type = null) | |
return $collection; | ||
} | ||
|
||
public function adddPriorityRoutes() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
{ | ||
krsort($this->routesWithPriority); | ||
$collection = new RouteCollection(); | ||
|
||
foreach ($this->routesWithPriority as $priorityGroup) { | ||
foreach ($priorityGroup as $route) { | ||
$this->addRoute($collection, $route[0], $route[1], $route[2], $route[3]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, here I had 2 option, one is when there is a priority key, I say continue, and I will not add the route initially there, and only here, but with this, if I add it again, routeCollection will remove the first addition and add the new one to the end of the list. |
||
} | ||
} | ||
|
||
return $collection; | ||
} | < 8000 /tr>||
|
||
protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method) | ||
{ | ||
$name = $annot->getName(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Symfony\Component\Routing\Tests\Loader; | ||
|
||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Component\Routing\Loader\AnnotationFileLoader; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
|
@@ -44,4 +45,26 @@ public function testSupports() | |
$this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified'); | ||
$this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified'); | ||
} | ||
|
||
public function testRoutesWithPriority() | ||
{ | ||
$routeDatas = array( | ||
'foo' => new Route(array('name' => 'foo', 'path' => '/foo')), | ||
'bar' => new Route(array('name' => 'bar', 'path' => '/bar')), | ||
'static_id' => new Route(array('name' => 'static_id', 'path' => '/static/{id}', 'options' => array('priority' => 1))), | ||
'home' => new Route(array('name' => 'home', 'path' => '/home')), | ||
'static_contact' => new Route(array('name' => 'static_contact', 'path' => '/static/contact', 'options' => array('priority' => 5))), | ||
'login' => new Route(array('name' => 'login', 'path' => '/login')), | ||
'static_location' => new Route(array('name' => 'static_location', 'path' => '/static/location', 'options' => array('priority' => 5))), | ||
); | ||
|
||
$this->reader | ||
->expects($this->once()) | ||
->method('getMethodAnnotations') | ||
-&g 10000 t;will($this->returnValue($routeDatas)); | ||
|
||
$routeCollection = $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/BarClass.php'); | ||
$expectedOrder = array('foo', 'bar', 'home', 'login', 'static_contact', 'static_location', 'static_id'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should'nt be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the original Issue there were no actual discussion about this and Ryan suggested to implement it in a way and let's see what will the community think. Of course that is also a viable option to set them to 0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree with @jeremy-derusse: If there's no priority on a route, it should be treated like a 0. And so, a priority of 1 should be before all routes without a priority. This would be consistent with how event priority's are done: (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/EventDispatcher/EventDispatcher.php#L95 and (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/EventDispatcher/EventDispatcher.php#L176) As far as implementing this, I'm not sure if the current approach of calling Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I didn't wanted to touch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds good to me! My proposal above also didn't touch |
||
$this->assertEquals($expectedOrder, array_keys($routeCollection->all())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where
$this->routesWithPriority[$annot->getOption('priority')]
is initialized ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which part do you mean? Priority key in option or routesWithPriority? first one will be in Route annotation from frameworkextrabundle, second is before the constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I thought, that this code will raise a notice...
Because
$foo['bar']
is not defined, but it's works withE_STRICT
...