Completed
Push — master ( d2e043...09ed12 )
by Changwan
12:25
created

Router::append()   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 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Router;
3
4
use FastRoute\DataGenerator\GroupCountBased as GCBDataGenerator;
5
use FastRoute\RouteCollector;
6
use FastRoute\RouteParser\Std;
7
8
class Router
9
{
10
    use ShortRouterMethods;
11
12
    /** @var \FastRoute\RouteCollector */
13
    protected $collector;
14
15
    /** @var \Wandu\Router\Route[] */
16
    protected $routes = [];
17
18
    /** @var string */
19
    protected $prefix = '';
20
21
    /** @var array */
22
    protected $middlewares = [];
23
24 21
    public function __construct()
25
    {
26 21
        $this->collector = new RouteCollector(new Std(), new GCBDataGenerator());
27 21
    }
28
29
    /**
30
     * @return \FastRoute\RouteCollector
31
     */
32 21
    public function getCollector()
33
    {
34 21
        return $this->collector;
35
    }
36
37
    /**
38
     * @return \Wandu\Router\Route[]
39
     */
40 21
    public function getRoutes()
41
    {
42 21
        return $this->routes;
43
    }
44
45
    /**
46
     * @param string $prefix
47
     * @param callable $handler
48
     */
49 1
    public function prefix($prefix, callable $handler)
50
    {
51 1
        $beforePrefix = $this->prefix;
52 1
        $this->prefix = $beforePrefix . $prefix ?: '/';
53 1
        call_user_func($handler, $this);
54 1
        $this->prefix = $beforePrefix;
55 1
    }
56
57
    /**
58
     * @param array|string $middlewares
59
     * @param callable $handler
60
     */
61 21
    public function middleware($middlewares, callable $handler)
62
    {
63 21
        if (!is_array($middlewares)) {
64
            $middlewares = [$middlewares];
65
        }
66 21
        $beforeMiddlewares = $this->middlewares;
67 21
        $this->middlewares = array_merge($beforeMiddlewares, $middlewares);
68 21
        $handler($this);
69 21
        $this->middlewares = $beforeMiddlewares;
70 21
    }
71
72
    /**
73
     * @param array $attributes
74
     * @param callable $handler
75
     */
76 1
    public function group(array $attributes, callable $handler)
77
    {
78 1
        $beforePrefix = $this->prefix;
79 1
        $beforeMiddlewares = $this->middlewares;
80
81 1
        if (isset($attributes['prefix'])) {
82 1
            $this->prefix = $beforePrefix . $attributes['prefix'] ?: '/';
83
        }
84 1
        if (isset($attributes['middleware'])) {
85 1
            if (!is_array($attributes['middleware'])) {
86
                $attributes['middleware'] = [$attributes['middleware']];
87
            }
88 1
            $this->middlewares = array_merge($beforeMiddlewares, $attributes['middleware']);
89
        }
90
91 1
        $handler($this);
92
93 1
        $this->prefix = $beforePrefix;
94 1
        $this->middlewares = $beforeMiddlewares;
95 1
    }
96
97
    /**
98
     * @param callable $handler
99
     */
100
    public function append(callable $handler)
101
    {
102
        $handler($this);
103
    }
104
105
    public function resource($className, $except = [], $only = [])
0 ignored issues
show
Unused Code introduced by
The parameter $except is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $only is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        $this->get('', $className, 'index');
108
        $this->get('/create', $className, 'create');
109
        $this->post('', $className, 'store');
110
        $this->get('/{id}', $className, 'show');
111
        $this->put('/{id}', $className, 'update');
112
        $this->delete('/{id}', $className, 'destroy');
113
    }
114
    
115
    /**
116
     * @param array $methods
117
     * @param string $path
118
     * @param string $className
119
     * @param string $methodName
120
     * @param array|string $middlewares
121
     * @return \Wandu\Router\Route
122
     */
123 21
    public function createRoute(array $methods, $path, $className, $methodName = 'index', array $middlewares = [])
124
    {
125 21
        $path = '/' . trim($this->prefix . $path, '/');
126 21
        if (!is_array($middlewares)) {
127
            $middlewares = [$middlewares];
128
        }
129 21
        $middlewares = array_merge($this->middlewares, $middlewares);
130
131 21
        $handler = implode(',', $methods) . $path;
132 21
        $this->collector->addRoute($methods, $path, $handler);
133 21
        return $this->routes[$handler] = new Route($className, $methodName, $middlewares);
134
    }
135
}
136