Completed
Push — master ( 413e31...6028d7 )
by Changwan
06:35
created

Router::getIterator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Router;
3
4
use IteratorAggregate;
5
6
class Router implements IteratorAggregate
7
{
8
    /** @var \Wandu\Router\Route[] */
9
    protected $routes = [];
10
11
    /** @var string */
12
    protected $prefix = '';
13
14
    /** @var array */
15
    protected $middlewares = [];
16
17
    /** @var string */
18
    protected $host;
19
    
20
    /**
21
     * @param string $prefix
22
     * @param callable $handler
23
     */
24 3
    public function prefix($prefix, callable $handler)
25
    {
26 3
        $beforePrefix = $this->prefix;
27 3
        $this->prefix = "{$beforePrefix}/" . ($prefix ?? '');
28
29 3
        call_user_func($handler, $this);
30
31 3
        $this->prefix = $beforePrefix;
32 3
    }
33
34
    /**
35
     * @param array|string $middlewares
36
     * @param callable $handler
37
     */
38 23
    public function middleware($middlewares, callable $handler)
39
    {
40 23
        $middlewares = array_filter((array) $middlewares);
41 23
        $beforeMiddlewares = $this->middlewares;
42 23
        $this->middlewares = array_merge($beforeMiddlewares, $middlewares);
43 23
        call_user_func($handler, $this);
44 23
        $this->middlewares = $beforeMiddlewares;
45 23
    }
46
47
    /**
48
     * @param array $attributes
49
     * @param callable $handler
50
     */
51
    public function group(array $attributes, callable $handler)
52
    {
53
        $this->prefix($attributes['prefix'] ?? '', function () use ($attributes, $handler) {
54 2
            $this->middleware($attributes['middleware'] ?? [], function () use ($attributes, $handler) {
55 2
                call_user_func($handler, $this);
56 2
            });
57 2
        });
58 2
    }
59
60
    /**
61
     * @param callable $handler
62
     */
63
    public function append(callable $handler)
64
    {
65
        call_user_func($handler, $this);
66
    }
67
68
    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...
69
    {
70
        $this->get('', $className, 'index');
71
        $this->get('create', $className, 'create');
72
        $this->post('', $className, 'store');
73
        $this->get('{id}', $className, 'show');
74
        $this->put('{id}', $className, 'update');
75
        $this->delete('{id}', $className, 'destroy');
76
    }
77
78
    /**
79
     * @param string $path
80
     * @param string $className
81
     * @param string $methodName
82
     * @param array $middlewares
83
     * @return Route
84
     */
85 5
    public function get($path, $className, $methodName = 'index', array $middlewares = [])
86
    {
87 5
        return $this->createRoute(['GET', 'HEAD'], $path, $className, $methodName, $middlewares);
88
    }
89
90
    /**
91
     * @param string $path
92
     * @param string $className
93
     * @param string $methodName
94
     * @param array $middlewares
95
     * @return Route
96
     */
97 2
    public function post($path, $className, $methodName = 'index', array $middlewares = [])
98
    {
99 2
        return $this->createRoute(['POST'], $path, $className, $methodName, $middlewares);
100
    }
101
102
    /**
103
     * @param string $path
104
     * @param string $className
105
     * @param string $methodName
106
     * @param array $middlewares
107
     * @return Route
108
     */
109 1
    public function put($path, $className, $methodName = 'index', array $middlewares = [])
110
    {
111 1
        return $this->createRoute(['PUT'], $path, $className, $methodName, $middlewares);
112
    }
113
114
    /**
115
     * @param string $path
116
     * @param string $className
117
     * @param string $methodName
118
     * @param array $middlewares
119
     * @return Route
120
     */
121 1
    public function delete($path, $className, $methodName = 'index', array $middlewares = [])
122
    {
123 1
        return $this->createRoute(['DELETE'], $path, $className, $methodName, $middlewares);
124
    }
125
126
    /**
127
     * @param string $path
128
     * @param string $className
129
     * @param string $methodName
130
     * @param array $middlewares
131
     * @return Route
132
     */
133 1
    public function options($path, $className, $methodName = 'index', array $middlewares = [])
134
    {
135 1
        return $this->createRoute(['OPTIONS'], $path, $className, $methodName, $middlewares);
136
    }
137
138
    /**
139
     * @param string $path
140
     * @param string $className
141
     * @param string $methodName
142
     * @param array $middlewares
143
     * @return Route
144
     */
145 1
    public function patch($path, $className, $methodName = 'index', array $middlewares = [])
146
    {
147 1
        return $this->createRoute(['PATCH'], $path, $className, $methodName, $middlewares);
148
    }
149
150
    /**
151
     * @param string $path
152
     * @param string $className
153
     * @param string $methodName
154
     * @param array $middlewares
155
     * @return Route
156
     */
157 1
    public function any($path, $className, $methodName = 'index', array $middlewares = [])
158
    {
159 1
        return $this->createRoute([
160 1
            'GET',
161
            'HEAD',
162
            'POST',
163
            'PUT',
164
            'DELETE',
165
            'OPTIONS',
166
            'PATCH'
167
        ], $path, $className, $methodName, $middlewares);
168
    }
169
    
170
    /**
171
     * @param array $methods
172
     * @param string $path
173
     * @param string $className
174
     * @param string $methodName
175
     * @param array|string $middlewares
176
     * @return \Wandu\Router\Route
177
     */
178 24
    public function createRoute(array $methods, $path, $className, $methodName = 'index', array $middlewares = [])
179
    {
180 24
        $path = trim("{$this->prefix}/{$path}", '/');
181 24
        while(strpos($path, '//') !== false) {
182 2
            $path = str_replace('//', '/', $path);
183
        }
184 24
        $path = '/' . $path;
185 24
        $middlewares = array_merge($this->middlewares, $middlewares);
186 24
        $route = new Route($className, $methodName, $middlewares);
187 24
        $this->routes[] = [$methods, $path, $route];
188 24
        return $route;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194 24
    public function getIterator()
195
    {
196 24
        foreach ($this->routes as $route) {
197 24
            yield $route;
198
        }
199 24
    }
200
}
201