-
-
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 |
---|---|---|
|
@@ -97,11 +97,6 @@ public function setRouteAnnotationClass($class) | |
$this->routeAnnotationClass = $class; | ||
} | ||
|
||
public function hasRoutesWithPriority() | ||
{ | ||
return !empty($this->routesWithPriority); | ||
} | ||
|
||
/** | ||
* Loads from annotations from a class. | ||
* | ||
|
@@ -132,40 +127,32 @@ 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')) { | ||
//These routes will be added to the collection later in File or Directory Loader | ||
$this->routesWithPriority[$annot->getOption('priority')][] | ||
= array($annot, $globals, $class, $method); | ||
continue; | ||
} | ||
$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; | ||
} | ||
|
||
/** | ||
* Create a new collection of routes with priority | ||
* These routes will be already sorted by priority | ||
* and after that append all the other routes which | ||
* don't have any priority | ||
* Add the prioritised routes to the collection | ||
* | ||
* @param $originalCollection | ||
* @param $collection | ||
* @return RouteCollection | ||
*/ | ||
public function addPriorityRoutes($originalCollection) | ||
public function addPriorityRoutes($collection) | ||
{ | ||
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. |
||
} | ||
} | ||
$collection->addCollection($originalCollection); | ||
|
||
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