-
-
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 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 array | ||
*/ | ||
protected $routesWithPriority = array(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -122,11 +127,33 @@ public function load($class, $type = null) | |
$this->defaultRouteIndex = 0; | ||
foreach ($this->reader->getMethodAnnotations($method) as $annot) { | ||
if ($annot instanceof $this->routeAnnotationClass) { | ||
$this->addRoute($collection, $annot, $globals, $class, $method); | ||
$priority = (int) $annot->getOption('priority'); | ||
$this->routesWithPriority[$priority][] = array($annot, $globals, $class, $method); | ||
} | ||
} | ||
} | ||
|
||
$collection = $this->addPriorityRoutes($collection); | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* Add the prioritised routes to the collection | ||
* | ||
* @param $collection | ||
* @return RouteCollection | ||
*/ | ||
public function addPriorityRoutes($collection) | ||
{ | ||
krsort($this->routesWithPriority); | ||
|
||
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; | ||
} | ||
|
||
|
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.
you must reset the state of the loader here
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.
I looked for a way for that, but at the moment RouteCollection don't have such function, and if it's called in a directoryLoader, the named route will be removed and always appended to the end of the array, here in priority order. Or what do you mean by reseting?
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.
$routesWithPriority
is making the loader stateful. This means that if you import the routes from several classes explicitly, the second import will still see the priority routes of the first import, potentially creating weird issuesThere 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.
Oh, I see what you mean. The original goal was that if you import the routes for the Controller directory, you can set up priority between the files.
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.
see my comment in the main discussion. This "feature" makes it very easy to break lots of things