|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.1 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing\Traits; |
|
19
|
|
|
|
|
20
|
|
|
use Biurad\Annotations\LoaderInterface; |
|
21
|
|
|
use Flight\Routing\DebugRoute; |
|
22
|
|
|
use Flight\Routing\Interfaces\RouteMatcherInterface; |
|
23
|
|
|
use Flight\Routing\Route; |
|
24
|
|
|
use Flight\Routing\RouteCollection; |
|
25
|
|
|
use Flight\Routing\RouteResolver; |
|
26
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
27
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
28
|
|
|
|
|
29
|
|
|
trait RouterTrait |
|
30
|
|
|
{ |
|
31
|
|
|
use MiddlewareTrait; |
|
32
|
|
|
|
|
33
|
|
|
/** @var null|RouteMatcherInterface */ |
|
34
|
|
|
private $matcher; |
|
35
|
|
|
|
|
36
|
|
|
/** @var ResponseFactoryInterface */ |
|
37
|
|
|
private $responseFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** @var UriFactoryInterface */ |
|
40
|
|
|
private $uriFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** @var null|DebugRoute */ |
|
43
|
|
|
private $debug; |
|
44
|
|
|
|
|
45
|
|
|
/** @var RouteCollection */ |
|
46
|
|
|
private $routes; |
|
47
|
|
|
|
|
48
|
|
|
/** @var null|Route */ |
|
49
|
|
|
private $route; |
|
50
|
|
|
|
|
51
|
|
|
/** @var RouteResolver */ |
|
52
|
|
|
private $resolver; |
|
53
|
|
|
|
|
54
|
|
|
/** @var mixed[] */ |
|
55
|
|
|
private $options = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the profiled routes |
|
59
|
|
|
* |
|
60
|
|
|
* @return null|DebugRoute |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getProfile(): ?DebugRoute |
|
63
|
|
|
{ |
|
64
|
1 |
|
if ($this->options['debug']) { |
|
65
|
|
|
$routes = $this->getCollection()->getRoutes(); |
|
|
|
|
|
|
66
|
1 |
|
|
|
67
|
1 |
|
foreach ($routes as $route) { |
|
68
|
1 |
|
$this->debug->addProfile(new DebugRoute($route->getName(), $route)); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->debug; |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Load routes from annotation. |
|
79
|
|
|
* |
|
80
|
|
|
* @param LoaderInterface $loader |
|
81
|
|
|
*/ |
|
82
|
|
|
public function loadAnnotation(LoaderInterface $loader): void |
|
83
|
|
|
{ |
|
84
|
6 |
|
// Guess you off debug mode and would not need to add new routes. |
|
85
|
|
|
if ($this->isFrozen()) { |
|
|
|
|
|
|
86
|
6 |
|
return; |
|
87
|
2 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
$annotations = $loader->load(); |
|
90
|
4 |
|
|
|
91
|
|
|
foreach ($annotations as $annotation) { |
|
92
|
|
|
if ($annotation instanceof RouteCollection) { |
|
93
|
|
|
$this->addRoute(...$annotation->getRoutes()); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
/** |
|
99
|
|
|
* Get merged default parameters. |
|
100
|
1 |
|
* |
|
101
|
|
|
* @param Route $route |
|
102
|
|
|
*/ |
|
103
|
|
|
private function mergeDefaults(Route $route): void |
|
104
|
|
|
{ |
|
105
|
|
|
$defaults = $route->getDefaults(); |
|
106
|
1 |
|
$param = $route->getArguments(); |
|
107
|
|
|
$excludes = ['_arguments' => true, '_domain' => true]; |
|
108
|
1 |
|
|
|
109
|
1 |
|
foreach ($defaults as $key => $value) { |
|
110
|
|
|
if (isset($excludes[$key])) { |
|
111
|
|
|
continue; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (!isset($param[$key]) || (!\is_int($key) && null !== $value)) { |
|
115
|
|
|
$route->argument($key, $value); |
|
116
|
4 |
|
} |
|
117
|
|
|
} |
|
118
|
4 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|