-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP][Routing] Add support for 'priority' option for annotated Routes #11747
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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 bool | ||
*/ | ||
protected $routeWithPriority = false; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -92,6 +97,16 @@ public function setRouteAnnotationClass($class) | |
$this->routeAnnotationClass = $class; | ||
} | ||
|
||
/** | ||
* Tells if one of the annotationed route has priority info | ||
* | ||
* @return bool | ||
*/ | ||
public function hasRouteWithPriority() | ||
{ | ||
return $this->routeWithPriority; | ||
} | ||
|
||
/** | ||
* Loads from annotations from a class. | ||
* | ||
|
@@ -104,6 +119,8 @@ public function setRouteAnnotationClass($class) | |
*/ | ||
public function load($class, $type = null) | ||
{ | ||
$hasPriority = false; | ||
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. unused variable |
||
|
||
if (!class_exists($class)) { | ||
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class)); | ||
} | ||
|
@@ -123,6 +140,9 @@ public function load($class, $type = null) | |
foreach ($this->reader->getMethodAnnotations($method) as $annot) { | ||
if ($annot instanceof $this->routeAnnotationClass) { | ||
$this->addRoute($collection, $annot, $globals, $class, $method); | ||
if (array_key_exists('priority',$annot->getOptions())) { | ||
$this->routeWithPriority = true; | ||
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. The loop can be left then since all following routes wouldn't change the 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. you mean check if routeWithPriority still false? 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. Never mind, I didn't see the call to |
||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Symfony\Component\Routing\Loader; | ||
|
||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Config\Resource\DirectoryResource; | ||
|
||
|
@@ -58,6 +59,15 @@ public function load($path, $type = null) | |
} | ||
} | ||
|
||
if ($this->loader->hasRouteWithPriority()) { | ||
$collection->getIterator()->uasort(function(Route $a, Route $b) { | ||
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. add a space after |
||
if ($a->getOption('priority') == $b->getOption('priority')) { | ||
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. Use |
||
return 0; | ||
} | ||
return $a->getOption('priority') > $b->getOption('priority') ? 1 : -1; | ||
}); | ||
} | ||
|
||
return $collection; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
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. add a comma at the end of the line 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 I add also to all of the others? Cause I just copy paste one of the previous and there were no coma 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. I would say no as it leads to mainly unrelated file diffs. 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. Then only to this line or leave this like this? 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. Only this line, there's no harm in adding it here. It's part of the diff anyway. |
||
), | ||
); | ||
} | ||
|
||
|
@@ -120,9 +125,10 @@ 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->assertSame(array_replace($methodArgs, $routeDatas['defaults']), $route->getDefaults(), '->load preserves defaults annotation'); | ||
$this->assertEquals($routeDatas['condition'], $route->getCondition(), '->load preserves condition annotation'); | ||
$this->assertSame(isset($routeDatas['options']['priority']), $this->loader->hasRouteWithPriority()); | ||
} | ||
|
||
public function testClassRouteLoad() | ||
|
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.
routes