-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[5.5] Route: simplify RouteCollection. #18417
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
lucasmichot
wants to merge
1
commit into
laravel:master
from
lucasmichot:feature/master/route-collection
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,43 +3,32 @@ | |
namespace Illuminate\Routing; | ||
|
||
use Countable; | ||
use ArrayIterator; | ||
use IteratorAggregate; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Collection; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; | ||
|
||
class RouteCollection implements Countable, IteratorAggregate | ||
{ | ||
/** | ||
* An array of the routes keyed by method. | ||
* The Route collection. | ||
* | ||
* @var array | ||
* @var \Illuminate\Support\Collection | ||
*/ | ||
protected $routes = []; | ||
protected $collection; | ||
|
||
/** | ||
* An flattened array of all of the routes. | ||
* Create a new RouteCollection instance. | ||
* | ||
* @var array | ||
*/ | ||
protected $allRoutes = []; | ||
|
||
/** | ||
* A look-up table of routes by their names. | ||
* | ||
* @var array | ||
*/ | ||
protected $nameList = []; | ||
|
||
/** | ||
* A look-up table of routes by controller action. | ||
* | ||
* @var array | ||
* @return void | ||
*/ | ||
protected $actionList = []; | ||
public function __construct() | ||
{ | ||
$this->collection = new Collection; | ||
} | ||
|
||
/** | ||
* Add a Route instance to the collection. | ||
|
@@ -49,83 +38,25 @@ class RouteCollection implements Countable, IteratorAggregate | |
*/ | ||
public function add(Route $route) | ||
{ | ||
$this->addToCollections($route); | ||
|
||
$this->addLookups($route); | ||
|
||
return $route; | ||
} | ||
|
||
/** | ||
* Add the given route to the arrays of routes. | ||
* | ||
* @param \Illuminate\Routing\Route $route | ||
* @return void | ||
*/ | ||
protected function addToCollections($route) | ||
{ | ||
$domainAndUri = $route->domain().$route->uri(); | ||
|
||
foreach ($route->methods() as $method) { | ||
$this->routes[$method][$domainAndUri] = $route; | ||
} | ||
|
||
$this->allRoutes[$method.$domainAndUri] = $route; | ||
} | ||
|
||
/** | ||
* Add the route to any look-up tables if necessary. | ||
* | ||
* @param \Illuminate\Routing\Route $route | ||
* @return void | ||
*/ | ||
protected function addLookups($route) | ||
{ | ||
// If the route has a name, we will add it to the name look-up table so that we | ||
// will quickly be able to find any route associate with a name and not have | ||
// to iterate through every route every time we need to perform a look-up. | ||
$action = $route->getAction(); | ||
|
||
if (isset($action['as'])) { | ||
$this->nameList[$action['as']] = $route; | ||
} | ||
|
||
// When the route is routing to a controller we will also store the action that | ||
// is used by the route. This will let us reverse route to controllers while | ||
// processing a request and easily generate URLs to the given controllers. | ||
if (isset($action['controller'])) { | ||
$this->addToActionList($action, $route); | ||
$this->collection->put($method.$route->domain().$route->uri(), $route); | ||
} | ||
} | ||
|
||
/** | ||
* Add a route to the controller action dictionary. | ||
* | ||
* @param array $action | ||
* @param \Illuminate\Routing\Route $route | ||
* @return void | ||
*/ | ||
protected function addToActionList($action, $route) | ||
{ | ||
$this->actionList[trim($action['controller'], '\\')] = $route; | ||
return $route; | ||
} | ||
|
||
/** | ||
* Refresh the name look-up table. | ||
* | ||
* This is done in case any names are fluently defined. | ||
* | ||
* @deprecated since version 5.5. | ||
* | ||
* @return void | ||
*/ | ||
public function refreshNameLookups() | ||
{ | ||
$this->nameList = []; | ||
|
||
foreach ($this->allRoutes as $route) { | ||
if ($route->getName()) { | ||
$this->nameList[$route->getName()] = $route; | ||
} | ||
} | ||
// | ||
} | ||
|
||
/** | ||
|
@@ -241,7 +172,9 @@ protected function methodNotAllowed(array $others) | |
*/ | ||
public function get($method = null) | ||
{ | ||
return is_null($method) ? $this->getRoutes() : Arr::get($this->routes, $method, []); | ||
return is_null($method) ? $this->getRoutes() : $this->collection->filter(function (Route $route) use ($method) { | ||
return in_array($method, $route->methods()); | ||
})->toArray(); | ||
} | ||
|
||
/** | ||
|
@@ -263,7 +196,9 @@ public function hasNamedRoute($name) | |
*/ | ||
public function getByName($name) | ||
{ | ||
return isset($this->nameList[$name]) ? $this->nameList[$name] : null; | ||
return $this->collection->first(function (Route $route) use ($name) { | ||
return Arr::get($route->getAction(), 'as') === $name; | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -274,7 +209,9 @@ public function getByName($name) | |
*/ | ||
public function getByAction($action) | ||
{ | ||
return isset($this->actionList[$action]) ? $this->actionList[$action] : null; | ||
return $this->collection->first(function (Route $route) use ($action) { | ||
return trim(Arr::get($route->getAction(), 'controller'), '\\') === $action; | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -284,7 +221,7 @@ public function getByAction($action) | |
*/ | ||
public function getRoutes() | ||
{ | ||
return array_values($this->allRoutes); | ||
return $this->getUniqueRoutes()->values()->toArray(); | ||
} | ||
|
||
/** | ||
|
@@ -294,7 +231,23 @@ public function getRoutes() | |
*/ | ||
public function getRoutesByMethod() | ||
{ | ||
return $this->routes; | ||
return $this->collection->mapWithKeys(function (Route $route) { | ||
foreach ($route->methods() as $method) { | ||
return [$method => [$route->domain().$route->uri() => $route]]; | ||
} | ||
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. Wouldn't this only return for the first method the route supports? |
||
})->toArray(); | ||
} | ||
|
||
/** | ||
* Get the unique routes. | ||
* | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
protected function getUniqueRoutes() | ||
{ | ||
return $this->collection->unique(function (Route $route) { | ||
return Arr::last($route->methods()).$route->domain().$route->uri(); | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -304,7 +257,7 @@ public function getRoutesByMethod() | |
*/ | ||
public function getIterator() | ||
{ | ||
return new ArrayIterator($this->getRoutes()); | ||
return $this->getUniqueRoutes()->getIterator(); | ||
} | ||
|
||
/** | ||
|
@@ -314,6 +267,6 @@ public function getIterator() | |
*/ | ||
public function count() | ||
{ | ||
return count($this->getRoutes()); | ||
return $this->getUniqueRoutes()->count(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn't this much slower than looking it up in a map? People may have many
route(...)
calls in their views.