Router::controllers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mfrancois
5
 * Date: 17/09/2016
6
 * Time: 16:46
7
 */
8
9
namespace Distilleries\Expendable\Http\Router;
10
11
use Illuminate\Support\Arr;
12
13
class Router extends \Illuminate\Routing\Router
14
{
15
16
17
    /**
18
     * Register an array of controllers with wildcard routing.
19
     *
20
     * @param  array  $controllers
21
     * @return void
22
     *
23
     */
24
    public function controllers(array $controllers)
25
    {
26
        foreach ($controllers as $uri => $controller) {
27
            $this->controller($uri, $controller);
28
        }
29
    }
30
    /**
31
     * Route a controller to a URI with wildcard routing.
32
     *
33
     * @param  string  $uri
34
     * @param  string  $controller
35
     * @param  array  $names
36
     * @return void
37
     *
38
     */
39 288
    public function controller($uri, $controller, $names = [])
40
    {
41 288
        $prepended = $controller;
42
        // First, we will check to see if a controller prefix has been registered in
43
        // the route group. If it has, we will need to prefix it before trying to
44
        // reflect into the class instance and pull out the method for routing.
45 288
        if (!empty($this->groupStack)) {
46 288
            $prepended = $this->prependGroupUses($controller);
47
        }
48 288
        $routable = (new ControllerInspector)
49 288
            ->getRoutable($prepended, $uri);
50
        // When a controller is routed using this method, we use Reflection to parse
51
        // out all of the routable methods for the controller, then register each
52
        // route explicitly for the developers, so reverse routing is possible.
53 288
        foreach ($routable as $method => $routes) {
54 288
            foreach ($routes as $route) {
55 288
                $this->registerInspected($route, $controller, $method, $names);
56
            }
57
        }
58 288
        $this->addFallthroughRoute($controller, $uri);
59
    }
60
61
    /**
62
     * Prepend the last group uses onto the use clause.
63
     *
64
     * @param  string  $uses
65
     * @return string
66
     */
67 288
    protected function prependGroupUses($uses)
68
    {
69 288
        $group = last($this->groupStack);
70
71 288
        return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses;
72
    }
73
74
    
75
    /**
76
     * Register an inspected controller route.
77
     *
78
     * @param  array   $route
79
     * @param  string  $controller
80
     * @param  string  $method
81
     * @param  array  $names
82
     * @return void
83
     *
84
     */
85 288
    protected function registerInspected($route, $controller, $method, &$names)
86
    {
87 288
        $action = ['uses' => $controller.'@'.$method];
88
        // If a given controller method has been named, we will assign the name to the
89
        // controller action array, which provides for a short-cut to method naming
90
        // so you don't have to define an individual route for these controllers.
91 288
        $action['as'] = Arr::get($names, $method);
92 288
        $this->{$route['verb']}($route['uri'], $action);
93
    }
94
    /**
95
     * Add a fallthrough route for a controller.
96
     *
97
     * @param  string  $controller
98
     * @param  string  $uri
99
     * @return void
100
     *
101
     */
102 288
    protected function addFallthroughRoute($controller, $uri)
103
    {
104 288
        $missing = $this->any($uri.'/{_missing}', $controller.'@missingMethod');
105 288
        $missing->where('_missing', '(.*)');
106
    }
107
    
108
}