Completed
Push — master ( 1d5273...930607 )
by ARCANEDEV
03:40
created

RoutesViewer::getRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanesoft\Foundation\Services;
2
3
use Illuminate\Contracts\Routing\Registrar as RouterContract;
4
use Illuminate\Support\Arr;
5
use Illuminate\Support\Str;
6
7
/**
8
 * Class     RoutesViewer
9
 *
10
 * @package  Arcanesoft\Foundation\Services
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class RoutesViewer
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /** @var \Illuminate\Routing\Router */
20
    protected $router;
21
22
    /* ------------------------------------------------------------------------------------------------
23
     |  Constructor
24
     | ------------------------------------------------------------------------------------------------
25
     */
26
    public function __construct(RouterContract $router)
27
    {
28
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router of type object<Illuminate\Contracts\Routing\Registrar> is incompatible with the declared type object<Illuminate\Routing\Router> of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
    }
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Getters & Setters
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * Get the route collection.
37
     *
38
     * @return \Illuminate\Routing\RouteCollection
39
     */
40
    public function getRoutes()
41
    {
42
        return $this->router->getRoutes();
43
    }
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Main Functions
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    public function all()
50
    {
51
        $routes = collect($this->getRoutes()->getRoutes());
52
53
        return $routes->filter(function (\Illuminate\Routing\Route $route) {
54
            return ! Str::startsWith($route->uri(), config('arcanesoft.foundation.routes-viewer.uris.hide', []));
55
        })->transform(function (\Illuminate\Routing\Route $route) {
56
            return [
57
                'methods'     => $this->prepareMethods($route),
58
                'domain'      => $route->domain(),
59
                'uri'         => $route->uri(),
60
                'name'        => $route->getName(),
61
                'action'      => $route->getActionName(),
62
                'middlewares' => $this->prepareMiddlewares($route),
63
            ];
64
        });
65
    }
66
67
    private function prepareMethods(\Illuminate\Routing\Route $route)
68
    {
69
        $methods = array_diff(
70
            $route->getMethods(),
71
            config('arcanesoft.foundation.routes-viewer.methods.hide', ['HEAD'])
72
        );
73
        $colors  = config('arcanesoft.foundation.routes-viewer.methods.colors', [
74
            'GET'    => 'success',
75
            'HEAD'   => 'default',
76
            'POST'   => 'primary',
77
            'PUT'    => 'warning',
78
            'PATCH'  => 'info',
79
            'DELETE' => 'danger',
80
        ]);
81
82
        return array_map(function ($method) use ($colors) {
83
            return [
84
                'name'  => $method,
85
                'color' => Arr::get($colors, $method),
86
            ];
87
        }, $methods);
88
    }
89
90
    private function prepareMiddlewares(\Illuminate\Routing\Route $route)
91
    {
92
        /** @var array $middleware */
93
        $middleware = $route->middleware();
94
95
        return method_exists($route, 'controllerMiddleware')
96
            ? array_merge($middleware, $route->controllerMiddleware())
97
            : $middleware;
98
    }
99
}
100