Completed
Push — master ( be02a5...a1ec95 )
by Nick
22:04 queued 02:39
created

RouteRegistrar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace EloquentJs\Controllerless;
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, $controller)
26
    {
27
        $this->router = $router;
28
        $this->controller = '\\'.$controller;
29
    }
30
31
    /**
32
     * Add the resourceful routes for the given uri and resource.
33
     *
34
     * @param string $uri
35
     * @param string $resource
36
     * @param array $options
37
     */
38
    public function addRoute($uri, $resource, array $options = [])
39
    {
40
        // The $router->resource() method doesn't allow custom route attributes
41
        // in the $options array. So, while the group() may look redundant here,
42
        // we need it to attach the relevant resource classname to each of the
43
        // individual restful routes being defined.
44
        // This is so when we come to resolve the controller, we
45
        // can easily tell what type of resource we need, i.e. which model.
46
        $this->router->group(
47
            $this->groupOptions($resource, $options),
48
            function($router) use ($uri, $options) {
49
50
                $router->resource(
51
                    $uri,
52
                    $this->controller,
53
                    $this->routeOptions($options)
54
                );
55
56
            }
57
        );
58
    }
59
60
    /**
61
     * Get the options for the resource route.
62
     *
63
     * @param array $options
64
     * @return array
65
     */
66
    protected function routeOptions(array $options)
67
    {
68
        // Exclude the routes for displaying forms
69
        if (empty($options['only'])) {
70
            $options['except'] = array_merge((array) array_get($options, 'except', []), ['create', 'edit']);
71
        }
72
73
        return $options;
74
    }
75
76
    /**
77
     * Get the options for the route group.
78
     *
79
     * @param string $resource
80
     * @param array $options
81
     * @return array
82
     */
83
    protected function groupOptions($resource, array $options)
84
    {
85
        return array_merge(compact('resource'), array_except($options, ['only', 'except']));
86
    }
87
88
    /**
89
     * Get the name of the resource currently being accessed.
90
     *
91
     * @return string
92
     */
93
    public function getCurrentResource()
94
    {
95
        return $this->router->current()->getAction()['resource'];
96
    }
97
}
98