Completed
Push — master ( 7949b1...be02a5 )
by Nick
07:10
created

RouteRegistrar::addRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3143
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
namespace EloquentJs\Laravel;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Routing\Router;
7
8
class RouteRegistrar
9
{
10
    /**
11
     * @var Router
12
     */
13
    protected $router;
14
15
    /**
16
     * @var string controller to handle generic resources
17
     */
18
    protected $controller;
19
20
    /**
21
     * Create a new RouteRegistrar instance.
22
     *
23
     * @param Router $router
24
     */
25
    public function __construct(Router $router)
26
    {
27
        $this->router = $router;
28
    }
29
30
    /**
31
     * Add the resourceful routes for the given uri and resource.
32
     *
33
     * @param string $uri
34
     * @param string $resource
35
     * @param array $options
36
     */
37
    public function addRoute($uri, $resource, array $options = [])
38
    {
39
        // The $router->resource() method doesn't allow custom route attributes
40
        // in the $options array. So, while the group() may look redundant here,
41
        // we need it to attach the relevant resource classname to each of the
42
        // individual restful routes being defined.
43
        // This is so when we come to resolve the controller, we
44
        // can easily tell what type of resource we need, i.e. which model.
45
        $this->router->group(
46
            $this->groupOptions($resource, $options),
47
            function($router) use ($uri, $options) {
48
49
                $router->resource(
50
                    $uri,
51
                    $this->controller,
52
                    $this->routeOptions($options)
53
                );
54
55
            }
56
        );
57
    }
58
59
    /**
60
     * Set the controller to use for the resource.
61
     *
62
     * @param string $controller
63
     * @return $this
64
     */
65
    public function setController($controller)
66
    {
67
        $this->controller = $controller;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Get the options for the resource route.
74
     *
75
     * @param array $options
76
     * @return array
77
     */
78
    protected function routeOptions(array $options)
79
    {
80
        // Exclude the routes for displaying forms
81
        if (empty($options['only'])) {
82
            $options['except'] = array_merge((array) array_get($options, 'except', []), ['create', 'edit']);
83
        }
84
85
        return $options;
86
    }
87
88
    /**
89
     * Get the options for the route group.
90
     *
91
     * @param string $resource
92
     * @param array $options
93
     * @return array
94
     */
95
    protected function groupOptions($resource, array $options)
96
    {
97
        return array_merge(compact('resource'), array_except($options, ['only', 'except']));
98
    }
99
100
    /**
101
     * Get the name of the resource currently being accessed.
102
     *
103
     * @return string
104
     */
105
    public function getCurrentResource()
106
    {
107
        return $this->router->current()->getAction()['resource'];
108
    }
109
}
110