Completed
Push — master ( e32a6c...5f3630 )
by Taosikai
15:51 queued 54s
created

Route.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * slince routing library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Routing;
7
8
class Route
9
{
10
    /**
11
     * head
12
     * @var string
13
     */
14
    const HEAD = 'HEAD';
15
16
    /**
17
     * get
18
     * @var string
19
     */
20
    const GET = 'GET';
21
22
    /**
23
     * post
24
     * @var string
25
     */
26
    const POST = 'POST';
27
28
    /**
29
     * put
30
     * @var string
31
     */
32
    const PUT = 'PUT';
33
34
    /**
35
     * patch
36
     * @var string
37
     */
38
    const PATCH = 'PATCH';
39
40
    /**
41
     * delete
42
     * @var string
43
     */
44
    const DELETE = 'DELETE';
45
46
    /**
47
     * purge
48
     * @var string
49
     */
50
    const PURGE = 'PURGE';
51
52
    /**
53
     * options
54
     * @var string
55
     */
56
    const OPTIONS = 'OPTIONS';
57
58
    /**
59
     * trace
60
     * @var string
61
     */
62
    const TRACE = 'TRACE';
63
64
    /**
65
     * connect
66
     * @var string
67
     */
68
    const CONNECT = 'CONNECT';
69
70
    /**
71
     * The route name
72
     * @var string
73
     */
74
    protected $name;
75
76
    /**
77
     * Path
78
     * @var string
79
     */
80
    protected $path;
81
82
    /**
83
     * Action
84
     * @var mixed
85
     */
86
    protected $action;
87
88
    /**
89
     * schemes
90
     * @var array
91
     */
92
    protected $schemes = [];
93
94
    /**
95
     * host
96
     * @var string
97
     */
98
    protected $host;
99
100
    /**
101
     * methods
102
     * @var array
103
     */
104
    protected $methods = [];
105
106
    /**
107
     * requirements
108
     * @var array
109
     */
110
    protected $requirements = [];
111
112
    /**
113
     * Defaults
114
     * @var array
115
     */
116
    protected $defaults= [];
117
118
    /**
119
     * parameters
120
     * @var array
121
     */
122
    protected $parameters = [];
123
124
    /**
125
     * The computed parameters
126
     * @var array
127
     */
128
    protected $computedParameters = [];
129
130
    /**
131
     * whether the route has been compiled
132
     * @var bool
133
     */
134
    protected $isCompiled = false;
135
136
    /**
137
     * the host regex
138
     * @var string
139
     */
140
    protected $hostRegex;
141
142
    /**
143
     * the path regex
144
     * @var string
145
     */
146
    protected $pathRegex;
147
148
    protected $hostVariables = [];
149
150
    protected $pathVariables = [];
151
152
    /**
153
     * 变量
154
     * @var array
155
     */
156
    protected $variables = [];
157
158
    public function __construct($path, $action) {
159
        $this->setPath($path);
160
        $this->setAction($action);
161
    }
162
163
    /**
164
     * Sets route name
165
     * @param string $name
166
     * @return $this;
0 ignored issues
show
The doc-type $this; could not be parsed: Expected "|" or "end of type", but got ";" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
167
     */
168
    public function setName($name)
169
    {
170
        $this->name = $name;
171
        return $this;
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    public function getName()
178
    {
179
        return $this->name;
180
    }
181
182
    /**
183
     * Sets the route path
184
     * @param string $path
185
     * @return $this
186
     */
187
    public function setPath($path)
188
    {
189
        $this->path = '/' . trim($path, '/');
190
        return $this;
191
    }
192
193
    /**
194
     * Gets the route path
195
     * @return string path
196
     */
197
    public function getPath()
198
    {
199
        return $this->path;
200
    }
201
202
    /**
203
     * Gets the path regex
204
     * @return string
205
     */
206
    public function getPathRegex()
207
    {
208
        return $this->pathRegex;
209
    }
210
211
    /**
212
     * Sets the action for the route
213
     * @param mixed $action
214
     * @return $this
215
     */
216
    public function setAction($action)
217
    {
218
        $this->action = $action;
219
        return $this;
220
    }
221
222
    /**
223
     * Gets the action
224
     * @return mixed
225
     */
226
    public function getAction()
227
    {
228
        return $this->action;
229
    }
230
231
    /**
232
     * Sets the route schemes
233
     * @param array $schemes
234
     * @return $this
235
     */
236
    public function setSchemes(array $schemes)
237
    {
238
        $this->schemes = array_map('strtolower', $schemes);
239
        return $this;
240
    }
241
242
    /**
243
     * Checks whether the route has the scheme
244
     * @param string $scheme
245
     * @return bool
246
     */
247
    public function hasScheme($scheme)
248
    {
249
        return in_array(strtolower($scheme), $this->schemes);
250
    }
251
252
    /**
253
     * Gets all schemes
254
     * @return array
255
     */
256
    public function getSchemes()
257
    {
258
        return $this->schemes;
259
    }
260
261
    /**
262
     * Sets the route request methods
263
     * @param array $methods
264
     * @return $this
265
     */
266
    public function setMethods(array $methods)
267
    {
268
        $this->methods = array_map('strtoupper', $methods);
269
        return $this;
270
    }
271
272
    /**
273
     * Checks whether the route has the request method
274
     * @param string $method
275
     * @return bool
276
     */
277
    public function hasMethod($method)
278
    {
279
        return in_array(strtoupper($method), $this->methods);
280
    }
281
282
    /**
283
     * Gets all request methods that the route supports
284
     * @return array
285
     */
286
    public function getMethods()
287
    {
288
        return $this->methods;
289
    }
290
291
    /**
292
     * Set the route host
293
     * @param string $host
294
     * @return $this
295
     */
296
    public function setHost($host)
297
    {
298
        $this->host = $host;
299
        return $this;
300
    }
301
302
    /**
303
     * Gets the host
304
     * @return string
305
     */
306
    public function getHost()
307
    {
308
        return $this->host;
309
    }
310
311
    /**
312
     * Gets the route host regex
313
     * @return string
314
     */
315
    public function getHostRegex()
316
    {
317
        return $this->hostRegex;
318
    }
319
320
    /**
321
     * Sets the requirements of the route
322
     * @param array $requirements
323
     * @return $this
324
     */
325
    public function setRequirements(array $requirements)
326
    {
327
        $this->requirements = $requirements;
328
        return $this;
329
    }
330
331
    /**
332
     * Sets a requirement by specified name and value
333
     * @param string $name
334
     * @param string $requirement
335
     * @return $this
336
     */
337
    public function setRequirement($name, $requirement)
338
    {
339
        $this->requirements[$name] = $requirement;
340
        return $this;
341
    }
342
343
    /**
344
     * Add the requirements of the route (not replace)
345
     * @param array $requirements
346
     * @return $this
347
     */
348
    public function addRequirements(array $requirements)
349
    {
350
        $this->requirements += $requirements;
351
        return $this;
352
    }
353
354
    /**
355
     * Gets all requirements
356
     * @return array
357
     */
358
    public function getRequirements()
359
    {
360
        return $this->requirements;
361
    }
362
363
    /**
364
     * Checks whether the route has the requirement
365
     * @param string $name
366
     * @return bool
367
     */
368
    public function hasRequirement($name)
369
    {
370
        return isset($this->requirements[$name]);
371
    }
372
373
    /**
374
     * Gets a requirement by its name
375
     * @param string $name
376
     * @param string $default
377
     * @return string|null
378
     */
379
    public function getRequirement($name, $default = null)
380
    {
381
        return isset($this->requirements[$name]) ? $this->requirements[$name] : $default;
382
    }
383
384
    /**
385
     *  Gets the defaults
386
     */
387
    public function getDefaults()
388
    {
389
        return $this->defaults;
390
    }
391
392
    /**
393
     * Sets a default item
394
     * @param string $name
395
     * @param string $value
396
     * @return $this
397
     */
398
    public function setDefault($name, $value)
399
    {
400
        $this->defaults[$name] = $value;
401
        return $this;
402
    }
403
404
    /**
405
     * Sets the defaults
406
     * @param array $defaults
407
     * @return $this
408
     */
409
    public function setDefaults(array $defaults)
410
    {
411
        $this->defaults = $defaults;
412
        return $this;
413
    }
414
415
    /**
416
     * Add the defaults of the route (not replace)
417
     * @param array $defaults
418
     * @return $this
419
     */
420
    public function addDefaults(array $defaults)
421
    {
422
        $this->defaults += $defaults;
423
        return $this;
424
    }
425
426
    /**
427
     * Gets the default option value by its name
428
     * @param string $name
429
     * @return mixed|null
430
     */
431
    public function getDefault($name)
432
    {
433
        return isset($this->defaults[$name]) ? $this->defaults[$name] : null;
434
    }
435
436
    /**
437
     * Checks whether the route has the default option
438
     * @param string $name
439
     * @return bool
440
     */
441
    public function hasDefault($name)
442
    {
443
        return array_key_exists($name, $this->defaults);
444
    }
445
446
    /**
447
     * Sets the route parameters
448
     * @param string $name
449
     * @param mixed $parameter
450
     * @return $this
451
     */
452
    public function setParameter($name, $parameter)
453
    {
454
        $this->parameters[$name] = $parameter;
455
        return $this;
456
    }
457
458
    /**
459
     * Gets the route parameters
460
     * @param string $name
461
     * @param string $default
462
     * @return mixed
463
     */
464
    public function getParameter($name, $default = null)
465
    {
466
        return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
467
    }
468
469
    /**
470
     * Checks whether the parameter exists
471
     * @param string $name
472
     * @return mixed
473
     */
474
    public function hasParameter($name)
475
    {
476
        return isset($this->parameters[$name]);
477
    }
478
479
    /**
480
     * Sets all parameters
481
     * @param array $parameters
482
     * @return $this
483
     */
484
    public function setParameters(array $parameters)
485
    {
486
        $this->parameters = $parameters;
487
        return $this;
488
    }
489
490
    /**
491
     * Add the paramters of the route (not replace)
492
     * @param array $parameters
493
     * @return $this
494
     */
495
    public function addParameters(array $parameters)
496
    {
497
        $this->parameters += $parameters;
498
        return $this;
499
    }
500
501
    /**
502
     * Gets all parameters
503
     * @return array
504
     */
505
    public function getParameters()
506
    {
507
        return $this->parameters;
508
    }
509
510
    /**
511
     * @param array $computedParameters
512
     */
513
    public function setComputedParameters($computedParameters)
514
    {
515
        $this->computedParameters = $computedParameters;
516
    }
517
518
    /**
519
     * @return array
520
     */
521
    public function getComputedParameters()
522
    {
523
        return $this->computedParameters;
524
    }
525
526
    /**
527
     * {@inheritdoc}
528
     */
529
    public function isCompiled()
530
    {
531
        return $this->isCompiled;
532
    }
533
534
    /**
535
     * Complies the route
536
     * @param boolean $reCompile
537
     * @return Route
538
     */
539
    public function compile($reCompile = false)
540
    {
541
        if (!$this->isCompiled || $reCompile) {
542
            if ($this->getHost()) {
543
                $this->hostRegex = $this->parsePattern($this->getHost(), true);
544
            }
545
            $this->pathRegex = $this->parsePattern($this->getPath(), false);
546
            $this->variables = array_merge($this->hostVariables, $this->pathVariables);
547
            $this->isCompiled = true;
548
        }
549
        return $this;
550
    }
551
552
    /**
553
     * Gets all variables that been compiled
554
     * @return array
555
     */
556
    public function getVariables()
557
    {
558
        return $this->variables;
559
    }
560
561
    /**
562
     * Parses the path to regex
563
     * @param string $path
564
     * @param boolean $isHost
565
     * @return string
566
     */
567
    protected function parsePattern($path, $isHost)
568
    {
569
        $variables = [];
570
        $regex = preg_replace_callback('#[/\.]?\{([a-zA-Z0-9_,]*)\}#i', function ($matches) use(&$variables){
571
            $variables[] = $matches[1];
572
            $subRegex = "(?P<{$matches[1]}>" . (isset($this->requirements[$matches[1]]) ? $this->requirements[$matches[1]] : '[^/\.]+') . ')';
573
            $regex = str_replace('\{' . $matches[1] . '\}', $subRegex, preg_quote($matches[0], '#'));
574
            if ($this->hasDefault($matches[1])) {
575
                $regex = "(?:{$regex})?";
576
            }
577
            return $regex;
578
        }, $path);
579
        $regex = "#^{$regex}$#";
580
        if ($isHost) {
581
            $regex .= 'i';
582
            $this->hostVariables = $variables;
583
        } else {
584
            $this->pathVariables = $variables;
585
        }
586
        return $regex;
587
    }
588
}