Skip to content

Commit

Permalink
Adds functionality to retrieve routes with applied filter
Browse files Browse the repository at this point in the history
  • Loading branch information
csolisprimus committed Mar 12, 2018
1 parent 17e4420 commit b4516bf
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions src/Phroute/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,20 @@ public function addRoute($httpMethod, $route, $handler, array $filters = []) {
list($route, $name) = $route;
}

if (!isset($name)) {
throw new BadRouteException("Missing route name for: '$route'");
}

$route = $this->addPrefix($this->trim($route));

list($routeData, $reverseData) = $this->routeParser->parse($route);

if(isset($name))
{
$this->reverse[$name] = $reverseData;
$action = explode('.', $name)[0];
$this->registeredRoutes[$httpMethod][$action] = [
'name' => $name, 'path' => $route
];
}
$this->reverse[$name] = $reverseData;
$this->generateRegisteredRoutesCollection($httpMethod, $route, $name);

$filters = array_merge_recursive($this->globalFilters, $filters);

foreach ($filters as $filter) {
foreach ($filter as $item) {
$this->routesWithFilters[$item][] = $route;
}
}
$this->generateRoutesWithFiltersCollection($route, $filters, $name);

isset($routeData[1]) ?
$this->addVariableRoute($httpMethod, $routeData, $handler, $filters) :
Expand Down Expand Up @@ -496,4 +490,42 @@ public function getRoutesWithFilters()
{
return $this->routesWithFilters;
}

/**
* @param $route
* @param array $filters
* @param $name
*/
private function generateRoutesWithFiltersCollection($route, $filters, $name)
{
foreach ($filters as $filter) {
if (is_array($filter)) {
foreach ($filter as $item) {
$this->routesWithFilters[$item][] = [
'name' => $name,
'path' => $route
];
}
} else {
$this->routesWithFilters[$filter][] = [
'name' => $name,
'path' => $route
];
}
}
}

/**
* @param $httpMethod
* @param $route
* @param $name
*/
private function generateRegisteredRoutesCollection($httpMethod, $route, $name)
{
$action = explode('.', $name)[0];
$this->registeredRoutes[$httpMethod][$action] = [
'name' => $name,
'path' => $route
];
}
}

0 comments on commit b4516bf

Please sign in to comment.