-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing][WIP] Add priorities to routing component #26132
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 |
---|---|---|
|
@@ -26,9 +26,14 @@ | |
class RouteCollection implements \IteratorAggregate, \Countable | ||
{ | ||
/** | ||
* @var Route[] | ||
* @var Route[][] | ||
*/ | ||
private $routes = array(); | ||
private $prioritizedRoutes = array(); | ||
|
||
/** | ||
* @var int[] | ||
*/ | ||
private $priorities = array(); | ||
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. routePriorities? |
||
|
||
/** | ||
* @var array | ||
|
@@ -37,8 +42,10 @@ class RouteCollection implements \IteratorAggregate, \Countable | |
|
||
public function __clone() | ||
{ | ||
foreach ($this->routes as $name => $route) { | ||
$this->routes[$name] = clone $route; | ||
foreach ($this->prioritizedRoutes as $priority => $routes) { | ||
foreach ($routes as $name => $route) { | ||
$this->prioritizedRoutes[$priority][$name] = clone $route; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -48,12 +55,13 @@ public function __clone() | |
* It implements \IteratorAggregate. | ||
* | ||
* @see all() | ||
* @todo Change return type to Iterator|Route[] in 5.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. should be removed |
||
* | ||
* @return \ArrayIterator|Route[] An \ArrayIterator object for iterating over routes | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->routes); | ||
return new \ArrayIterator($this->all()); | ||
} | ||
|
||
/** | ||
|
@@ -63,7 +71,7 @@ public function getIterator() | |
*/ | ||
public function count() | ||
{ | ||
return count($this->routes); | ||
return count($this->priorities); | ||
} | ||
|
||
/** | ||
|
@@ -74,19 +82,40 @@ public function count() | |
*/ | ||
public function add($name, Route $route) | ||
{ | ||
unset($this->routes[$name]); | ||
$this->addWithPriority($name, $route, 0); | ||
} | ||
|
||
$this->routes[$name] = $route; | ||
/** | ||
* Adds a route. | ||
* | ||
* @param string $name The route name | ||
* @param Route $route A Route instance | ||
*/ | ||
public function addWithPriority($name, Route $route, int $priority = 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. Instead of this, I would have used the deprecation mechanism with args and merged this with 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 agree 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 be removed and a third arg added to |
||
{ | ||
$this->remove($name); | ||
|
||
$this->priorities[$name] = $priority; | ||
$this->prioritizedRoutes[$priority][$name] = $route; | ||
} | ||
|
||
|
||
/** | ||
* Returns all routes in this collection. | ||
* | ||
* @return Route[] An array of routes | ||
*/ | ||
public function all() | ||
{ | ||
return $this->routes; | ||
// We can't use array_merge here, because that doesn't preserve numeric keys | ||
krsort($this->prioritizedRoutes); | ||
|
||
$all = []; | ||
foreach ($this->prioritizedRoutes as $priority => $routes) { | ||
$all = $all + $routes; | ||
} | ||
|
||
return $all; | ||
} | ||
|
||
/** | ||
|
@@ -98,7 +127,7 @@ public function all() | |
*/ | ||
public function get($name) | ||
{ | ||
return isset($this->routes[$name]) ? $this->routes[$name] : null; | ||
return isset($this->priorities[$name]) ? $this->prioritizedRoutes[$this->priorities[$name]][$name] : null; | ||
} | ||
|
||
/** | ||
|
@@ -109,7 +138,11 @@ public function get($name) | |
public function remove($name) | ||
{ | ||
foreach ((array) $name as $n) { | ||
unset($this->routes[$n]); | ||
if (!isset($this->priorities[$n])) { | ||
continue; | ||
}; | ||
unset($this->prioritizedRoutes[$this->priorities[$n]][$n]); | ||
unset($this->priorities[$n]); | ||
} | ||
} | ||
|
||
|
@@ -119,11 +152,10 @@ public function remove($name) | |
*/ | ||
public function addCollection(RouteCollection $collection) | ||
{ | ||
// we need to remove all routes with the same names first because just replacing them | ||
// would not place the new route at the end of the merged array | ||
foreach ($collection->all() as $name => $route) { | ||
unset($this->routes[$name]); | ||
$this->routes[$name] = $route; | ||
foreach ($collection->prioritizedRoutes as $priority => $routes) { | ||
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. this breaks classes extending |
||
foreach ($routes as $name => $route) { | ||
$this->addWithPriority($name, $route, $priority); | ||
} | ||
} | ||
|
||
foreach ($collection->getResources() as $resource) { | ||
|
@@ -146,7 +178,7 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement | |
return; | ||
} | ||
|
||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->setPath('/'.$prefix.$route->getPath()); | ||
$route->addDefaults($defaults); | ||
$route->addRequirements($requirements); | ||
|
@@ -158,13 +190,18 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement | |
*/ | ||
public function addNamePrefix(string $prefix) | ||
{ | ||
$prefixedRoutes = array(); | ||
$prefixedRoutes = []; | ||
$prefixedPriorites = []; | ||
|
||
foreach ($this->routes as $name => $route) { | ||
$prefixedRoutes[$prefix.$name] = $route; | ||
foreach ($this->prioritizedRoutes as $priority => $routes) { | ||
foreach ($routes as $name => $route) { | ||
$prefixedPriorites[$prefix.$name] = $priority; | ||
$prefixedRoutes[$priority][$prefix.$name] = $route; | ||
} | ||
} | ||
|
||
$this->routes = $prefixedRoutes; | ||
$this->priorities = $prefixedPriorites; | ||
$this->prioritizedRoutes = $prefixedRoutes; | ||
} | ||
|
||
/** | ||
|
@@ -176,7 +213,7 @@ public function addNamePrefix(string $prefix) | |
*/ | ||
public function setHost($pattern, array $defaults = array(), array $requirements = array()) | ||
{ | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->setHost($pattern); | ||
$route->addDefaults($defaults); | ||
$route->addRequirements($requirements); | ||
|
@@ -192,7 +229,7 @@ public function setHost($pattern, array $defaults = array(), array $requirements | |
*/ | ||
public function setCondition($condition) | ||
{ | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->setCondition($condition); | ||
} | ||
} | ||
|
@@ -207,7 +244,7 @@ public function setCondition($condition) | |
public function addDefaults(array $defaults) | ||
{ | ||
if ($defaults) { | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->addDefaults($defaults); | ||
} | ||
} | ||
|
@@ -223,7 +260,7 @@ public function addDefaults(array $defaults) | |
public function addRequirements(array $requirements) | ||
{ | ||
if ($requirements) { | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->addRequirements($requirements); | ||
} | ||
} | ||
|
@@ -239,7 +276,7 @@ public function addRequirements(array $requirements) | |
public function addOptions(array $options) | ||
{ | ||
if ($options) { | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->addOptions($options); | ||
} | ||
} | ||
|
@@ -252,7 +289,7 @@ public function addOptions(array $options) | |
*/ | ||
public function setSchemes($schemes) | ||
{ | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->setSchemes($schemes); | ||
} | ||
} | ||
|
@@ -264,7 +301,7 @@ public function setSchemes($schemes) | |
*/ | ||
public function setMethods($methods) | ||
{ | ||
foreach ($this->routes as $route) { | ||
foreach ($this->all() as $route) { | ||
$route->setMethods($methods); | ||
} | ||
} | ||
|
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.
routesByPriority?