Passed
Push — master ( 21057b...383a4e )
by Alexander
01:22
created

Route.php$0 ➔ getParameters()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
crap 1
1
<?php
2
3
namespace Yiisoft\Router;
4
5
use Yiisoft\Router\Middleware\Callback;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
/**
12
 * Route defines a mapping from URL to callback / name and vice versa
13
 */
14
final class Route implements MiddlewareInterface, RequestHandlerInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var array
23
     */
24
    private $methods;
25
26
    /**
27
     * @var string
28
     */
29
    private $pattern;
30
31
    /**
32
     * @var string
33
     */
34
    private $host;
35
36
    /**
37
     * @var array
38
     */
39
    private $middlewares;
40
41
    /**
42
     * @var array
43
     */
44
    private $parameters = [];
45
46
    /**
47
     * @var array
48
     */
49
    private $defaults = [];
50
51
    /**
52
     * @var RequestHandlerInterface
53
     */
54
    private $nextHandler;
55
56
    private function __construct()
57
    {
58
    }
59
60 16
    public static function get(string $pattern): self
61
    {
62 16
        $route = new static();
63 16
        $route->methods = [Method::GET];
64 16
        $route->pattern = $pattern;
65 16
        return $route;
66
    }
67
68 3
    public static function post(string $pattern): self
69
    {
70 3
        $route = new static();
71 3
        $route->methods = [Method::POST];
72 3
        $route->pattern = $pattern;
73 3
        return $route;
74
    }
75
76 1
    public static function put(string $pattern): self
77
    {
78 1
        $route = new static();
79 1
        $route->methods = [Method::PUT];
80 1
        $route->pattern = $pattern;
81 1
        return $route;
82
    }
83
84 1
    public static function delete(string $pattern): self
85
    {
86 1
        $route = new static();
87 1
        $route->methods = [Method::DELETE];
88 1
        $route->pattern = $pattern;
89 1
        return $route;
90
    }
91
92 1
    public static function patch(string $pattern): self
93
    {
94 1
        $route = new static();
95 1
        $route->methods = [Method::PATCH];
96 1
        $route->pattern = $pattern;
97 1
        return $route;
98
    }
99
100 1
    public static function head(string $pattern): self
101
    {
102 1
        $route = new static();
103 1
        $route->methods = [Method::HEAD];
104 1
        $route->pattern = $pattern;
105 1
        return $route;
106
    }
107
108 1
    public static function options(string $pattern): self
109
    {
110 1
        $route = new static();
111 1
        $route->methods = [Method::OPTIONS];
112 1
        $route->pattern = $pattern;
113 1
        return $route;
114
    }
115
116 2
    public static function methods(array $methods, string $pattern): self
117
    {
118 2
        $route = new static();
119 2
        $route->methods = $methods;
120 2
        $route->pattern = $pattern;
121 2
        return $route;
122
    }
123
124 1
    public static function anyMethod(string $pattern): self
125
    {
126 1
        $route = new static();
127 1
        $route->methods = Method::ANY;
128 1
        $route->pattern = $pattern;
129 1
        return $route;
130
    }
131
132 2
    public function name(string $name): self
133
    {
134 2
        $route = clone $this;
135 2
        $route->name = $name;
136 2
        return $route;
137
    }
138
139 1
    public function pattern(string $pattern): self
140
    {
141 1
        $new = clone $this;
142 1
        $new->pattern = $pattern;
143 1
        return $new;
144
    }
145
146 2
    public function host(string $host): self
147
    {
148 2
        $route = clone $this;
149 2
        $route->host = rtrim($host, '/');
150 2
        return $route;
151
    }
152
153
    /**
154
     * Parameter validation rules indexed by parameter names
155
     *
156
     * @param array $parameters
157
     * @return Route
158
     */
159 1
    public function parameters(array $parameters): self
160
    {
161 1
        $route = clone $this;
162 1
        $route->parameters = $parameters;
163 1
        return $route;
164
    }
165
166
    /**
167
     * Parameter default values indexed by parameter names
168
     *
169
     * @param array $defaults
170
     * @return Route
171
     */
172 1
    public function defaults(array $defaults): self
173
    {
174 1
        $route = clone $this;
175 1
        $route->defaults = $defaults;
176 1
        return $route;
177
    }
178
179 7
    private function prepareMiddlware($middleware): MiddlewareInterface
180
    {
181 7
        if (\is_callable($middleware)) {
182 1
            $middleware = new Callback($middleware);
183
        }
184
185 7
        if (!$middleware instanceof MiddlewareInterface) {
186 1
            throw new \InvalidArgumentException('Parameter should be either a PSR middleware or a callable.');
187
        }
188
189 6
        return $middleware;
190
    }
191
192
    /**
193
     * Adds a handler that should be invoked for a matching route.
194
     * It can be either a PSR middleware or a callable with the following signature:
195
     *
196
     * ```
197
     * function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
198
     * ```
199
     *
200
     * @param MiddlewareInterface|callable $middleware
201
     * @return Route
202
     */
203 7
    public function to($middleware): self
204
    {
205 7
        $route = clone $this;
206 7
        $route->middlewares[] = $this->prepareMiddlware($middleware);
207 6
        return $route;
208
    }
209
210
    /**
211
     * Adds a handler that should be invoked for a matching route.
212
     * It can be either a PSR middleware or a callable with the following signature:
213
     *
214
     * ```
215
     * function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
216
     * ```
217
     *
218
     * @param MiddlewareInterface|callable $middleware
219
     * @return Route
220
     */
221 1
    public function then($middleware): self
222
    {
223 1
        return $this->to($middleware);
224
    }
225
226
    /**
227
     * Prepends a handler that should be invoked for a matching route.
228
     * It can be either a PSR middleware or a callable with the following signature:
229
     *
230
     * ```
231
     * function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
232
     * ```
233
     *
234
     * @param MiddlewareInterface|callable $middleware
235
     * @return Route
236
     */
237 1
    public function prepend($middleware): self
238
    {
239 1
        $route = clone $this;
240 1
        array_unshift($route->middlewares, $this->prepareMiddlware($middleware));
241 1
        return $route;
242
    }
243
244 1
    public function __toString()
245
    {
246 1
        $result = '';
247
248 1
        if ($this->name !== null) {
249 1
            $result .= '[' . $this->name . '] ';
250
        }
251
252 1
        if ($this->methods !== null) {
253 1
            $result .= implode(',', $this->methods) . ' ';
254
        }
255 1
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
256 1
            $result .= $this->host;
257
        }
258 1
        $result .= $this->pattern;
259
260 1
        return $result;
261
    }
262
263 2
    public function getName(): string
264
    {
265 2
        return $this->name ?? (implode(', ', $this->methods) . ' ' . $this->pattern);
266
    }
267
268 9
    public function getMethods(): array
269
    {
270 9
        return $this->methods;
271
    }
272
273 1
    public function getPattern(): string
274
    {
275 1
        return $this->pattern;
276
    }
277
278 1
    public function getHost(): ?string
279
    {
280 1
        return $this->host;
281
    }
282
283 1
    public function getParameters(): array
284
    {
285 1
        return $this->parameters;
286
    }
287
288 1
    public function getDefaults(): array
289
    {
290 1
        return $this->defaults;
291
    }
292
293
    /**
294
     * @internal please use {@see dispatch()} or {@see process()}
295
     * @param ServerRequestInterface $request
296
     * @return ResponseInterface
297
     */
298 6
    public function handle(ServerRequestInterface $request): ResponseInterface
299
    {
300 6
        $middleware = \current($this->middlewares);
301 6
        \next($this->middlewares);
302 6
        if ($middleware === false) {
303
            if (!$this->nextHandler !== null) {
0 ignored issues
show
introduced by
The condition ! $this->nextHandler !== null is always true.
Loading history...
304
                return $this->nextHandler->handle($request);
305
            }
306
307
            throw new \LogicException('Middleware stack exhausted');
308
        }
309
310 6
        return $middleware->process($request, $this);
311
    }
312
313 6
    public function dispatch(ServerRequestInterface $request): ResponseInterface
314
    {
315 6
        \reset($this->middlewares);
316 6
        return $this->handle($request);
317
    }
318
319 6
    public function process(ServerRequestInterface $request, RequestHandlerInterface $nextHandler): ResponseInterface
320
    {
321 6
        $this->nextHandler = $nextHandler;
322 6
        return $this->dispatch($request);
323
    }
324
}
325