|
1
|
|
|
<?php |
|
2
|
|
|
namespace Te7aHoudini\Laroute\Routes; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Routing\Route; |
|
5
|
|
|
use Illuminate\Routing\RouteCollection; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Te7aHoudini\Laroute\Routes\Exceptions\ZeroRoutesException; |
|
8
|
|
|
class Collection extends \Illuminate\Support\Collection |
|
9
|
|
|
{ |
|
10
|
|
|
public function __construct(RouteCollection $routes, $filter, $namespace) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->items = $this->parseRoutes($routes, $filter, $namespace); |
|
13
|
|
|
} |
|
14
|
|
|
/** |
|
15
|
|
|
* Parse the routes into a jsonable output. |
|
16
|
|
|
* |
|
17
|
|
|
* @param RouteCollection $routes |
|
18
|
|
|
* @param string $filter |
|
19
|
|
|
* @param string $namespace |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
* @throws ZeroRoutesException |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function parseRoutes(RouteCollection $routes, $filter, $namespace) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->guardAgainstZeroRoutes($routes); |
|
27
|
|
|
$results = []; |
|
28
|
|
|
foreach ($routes as $route) { |
|
29
|
|
|
$results[] = $this->getRouteInformation($route, $filter, $namespace); |
|
30
|
|
|
} |
|
31
|
|
|
return array_values(array_filter($results)); |
|
32
|
|
|
} |
|
33
|
|
|
/** |
|
34
|
|
|
* Throw an exception if there aren't any routes to process |
|
35
|
|
|
* |
|
36
|
|
|
* @param RouteCollection $routes |
|
37
|
|
|
* |
|
38
|
|
|
* @throws ZeroRoutesException |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function guardAgainstZeroRoutes(RouteCollection $routes) |
|
41
|
|
|
{ |
|
42
|
|
|
if (count($routes) < 1) { |
|
43
|
|
|
throw new ZeroRoutesException("You don't have any routes!"); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
/** |
|
47
|
|
|
* Get the route information for a given route. |
|
48
|
|
|
* |
|
49
|
|
|
* @param $route \Illuminate\Routing\Route |
|
50
|
|
|
* @param $filter string |
|
51
|
|
|
* @param $namespace string |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getRouteInformation(Route $route, $filter, $namespace) |
|
56
|
|
|
{ |
|
57
|
|
|
$host = $route->domain(); |
|
58
|
|
|
$methods = $route->methods(); |
|
59
|
|
|
$uri = $route->uri(); |
|
60
|
|
|
$name = $route->getName(); |
|
61
|
|
|
$action = $route->getActionName(); |
|
62
|
|
|
$laroute = Arr::get($route->getAction(), 'laroute', null); |
|
63
|
|
|
if(!empty($namespace)) { |
|
64
|
|
|
$a = $route->getAction(); |
|
65
|
|
|
if(isset($a['controller'])) { |
|
66
|
|
|
$action = str_replace($namespace.'\\', '', $action); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
switch ($filter) { |
|
70
|
|
|
case 'all': |
|
71
|
|
|
if($laroute === false) return null; |
|
72
|
|
|
break; |
|
73
|
|
|
case 'only': |
|
74
|
|
|
if($laroute !== true) return null; |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
return compact('host', 'methods', 'uri', 'name', 'action'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|