RouteRegistrar::groupOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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