8000 [Routing] Add support for 'priority' option for annotated Routes by ghostika · Pull Request #11753 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement priority routes for annotated controller actions
  • Loading branch information
Andras Ratz committed Aug 24, 2014
commit 1ddfd1bef7367ad13f3df2958d43863bb3bbbe1d
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public function getOptions()
return $this->options;
}

public function getOption($name)
{
return isset($this->options[$name]) ? $this->options[$name] : null;
}

public function setDefaults($defaults)
{
$this->defaults = $defaults;
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ abstract class AnnotationClassLoader implements LoaderInterface
*/
protected $defaultRouteIndex = 0;

/**
* @var array
*/
protected $routesWithPriority = array();

/**
* Constructor.
*
Expand All @@ -92,6 +97,11 @@ public function setRouteAnnotationClass($class)
$this->routeAnnotationClass = $class;
}

public function hasRoutesWithPriority()
{
return !empty($this->routesWithPriority);
}

/**
* Loads from annotations from a class.
*
Expand Down Expand Up @@ -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')][]
Copy link
Member

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 ?

Copy link
Author

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

Copy link
Member

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...

$foo = array();
$foo['bar'][] = 'baz';

Because $foo['bar'] is not defined, but it's works with E_STRICT...

= array($annot, $globals, $class, $method);
}
$this->addRoute($collection, $annot, $globals, $class, $method);
}
}
Expand All @@ -130,6 +144,20 @@ public function load($class, $type = null)
return $collection;
}

public function adddPriorityRoutes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: addd

{
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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The 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;
}

protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
{
$name = $annot->getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public function load($path, $type = null)
}
}

if ($this->loader->hasRoutesWithPriority()) {
$collection->addCollection($this->loader->adddPriorityRoutes());
}

return $collection;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function load($file, $type = null)
$collection->addCollection($this->loader->load($class, $type));
}

if ($this->loader->hasRoutesWithPriority()) {
$collection->addCollection($this->loader->adddPriorityRoutes());
}

return $collection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function getLoadTests()
array('name' => 'route1', 'defaults' => array('arg2' => 'foo'), 'condition' => 'context.getMethod() == "GET"'),
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3')
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('name' => 'route_priority', 'options' => array('priority' => 10)),
array('arg2' => 'defaultValue2', 'arg3' =>'defaultValue3'),
),
);
}

Expand Down Expand Up @@ -120,7 +125,8 @@ public function testLoad($className, $routeDatas = array(), $methodArgs = array(

$this->assertSame($routeDatas['path'], $route->getPath(), '->load preserves path annotation');
$this->assertSame($routeDatas['requirements'],$route->getRequirements(), '->load preserves requirements annotation');
$this->assertCount(0, array_intersect($route->getOptions(), $routeDatas['options']), '->load preserves options annotation');
$this->assertSame(count($routeDatas['options']), count(array_intersect($route->getOptions(), $routeDatas['options'])), '->load preserves options annotation');
$this->assertEquals(array_key_exists('priority', $routeDatas['options']), $this->loader->hasRoutesWithPriority());
$this->assertSame(array_replace($methodArgs, $routeDatas['defaults']), $route->getDefaults(), '->load preserves defaults annotation');
$this->assertEquals($routeDatas['condition'], $route->getCondition(), '->load preserves condition annotation');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Annotation\Route;


class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
{
Expand Down Expand Up @@ -50,4 +52,26 @@ public function testSupports()
$this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
$this->assertFalse($this->loader->supports($fixturesDir, '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')
->will($this->returnValue($routeDatas));

$routeCollection = $this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
$expectedOrder = array('foo', 'bar', 'home', 'login', 'static_contact', 'static_location', 'static_id');
$this->assertEquals($expectedOrder, array_keys($routeCollection->all()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should'nt be array('static_contact', 'static_location', 'static_id', 'foo', 'bar', 'home', 'login'); ? How to prioritize route before a route without priority.
Mays be routes without priority could be sets to 0.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 addPriorityRoutes will work or not. We need to somehow take the final route $collection (i.e. after the foreach in AnnotationClassLoader and re-order it based on the priority of all routes in that list. Perhaps a function that looks like $this->loader->reorderCollection($collection) that compares the route names with an internal priority array that AnnotationClassLoader has?

Thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I didn't wanted to touch RouteCollection at all, but I have a proposal, RouteCollection has a method, addOptions(), where if there is a route with priority at the end of the process I can pass in array(priority => 0)
and then every route in the RouteCollection will have this value, and at the end in the DirectoryLoader I add the routes with real priority option and with this, I don't need to change anything in the RouterCollection.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me! My proposal above also didn't touch RouteCollection either, but as long as there's no downside to adding the option (I don't know of any), your idea will be cleaner.

$this->assertEquals($expectedOrder, array_keys($routeCollection->all()));
}
}
0