|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HnrAzevedo\Router; |
|
6
|
|
|
|
|
7
|
|
|
use ReflectionObject, ReflectionMethod, Exception; |
|
8
|
|
|
|
|
9
|
|
|
trait AttributeTrait |
|
10
|
|
|
{ |
|
11
|
|
|
use Helper, DefinitionsTrait; |
|
12
|
|
|
|
|
13
|
|
|
protected array $pipeline = []; |
|
14
|
|
|
|
|
15
|
|
|
protected function getPipeline(): array |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->pipeline; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function setPipeline(array $pipe): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->pipeline = $pipe; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function pipeline(array $pline): void |
|
26
|
|
|
{ |
|
27
|
|
|
self::getInstance()->setPipeline($pline); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function loadPipeline(): void |
|
31
|
|
|
{ |
|
32
|
|
|
foreach(self::getInstance()->getPipeline() as $line){ |
|
33
|
|
|
self::getInstance()->loadLine(new ReflectionObject(new $line())); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function loadLine(ReflectionObject $reflection): void |
|
38
|
|
|
{ |
|
39
|
|
|
foreach($reflection->getMethods() as $method){ |
|
40
|
|
|
$this->loadMethod($method); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function loadMethod(ReflectionMethod $method): void |
|
45
|
|
|
{ |
|
46
|
|
|
try{ |
|
47
|
|
|
foreach ($method->getAttributes() as $attr) { |
|
48
|
|
|
if($attr->getName() != 'HnrAzevedo\Router\RouteAttribute') continue; |
|
49
|
|
|
|
|
50
|
|
|
$args = $attr->getArguments(); |
|
51
|
|
|
|
|
52
|
|
|
$this->checkArgs($attr->getArguments()); |
|
53
|
|
|
|
|
54
|
|
|
self::set( |
|
55
|
|
|
strtolower(implode('|', $args['methods'])), |
|
56
|
|
|
$args['uri'], |
|
57
|
|
|
$method->class.'@'.$method->name |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->attrName($args) |
|
61
|
|
|
->attrBefore($args) |
|
62
|
|
|
->attrAfter($args) |
|
63
|
|
|
->attrAttributes($args) |
|
64
|
|
|
->attrWhere($args) |
|
65
|
|
|
->attrMiddleware($args); |
|
66
|
|
|
} |
|
67
|
|
|
}catch(Exception $er){ |
|
68
|
|
|
throw new Exception('Failed to add route via attribute: '.$er->getMessage()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function checkArgs(array $args): self |
|
73
|
|
|
{ |
|
74
|
|
|
if(!array_key_exists('uri', $args) || !array_key_exists('methods', $args)) { |
|
75
|
|
|
throw new Exception('Misconfigured route attribute'); |
|
76
|
|
|
} |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function attrName(array $attr): self |
|
81
|
|
|
{ |
|
82
|
|
|
if(array_key_exists('name', $attr)) { |
|
83
|
|
|
self::getInstance()->name($attr['name']); |
|
84
|
|
|
} |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function attrBefore(array $attr): self |
|
89
|
|
|
{ |
|
90
|
|
|
if(array_key_exists('before', $attr)) { |
|
91
|
|
|
self::getInstance()->before($attr['before']); |
|
92
|
|
|
} |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
private function attrAfter(array $attr): self |
|
97
|
|
|
{ |
|
98
|
|
|
if(array_key_exists('after', $attr)) { |
|
99
|
|
|
self::getInstance()->after($attr['after']); |
|
100
|
|
|
} |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function attrAttributes(array $attr): self |
|
105
|
|
|
{ |
|
106
|
|
|
if(array_key_exists('attributes', $attr)) { |
|
107
|
|
|
foreach($attr['attributes'] as $attribute){ |
|
108
|
|
|
self::getInstance()->attribute($attribute[0], $attribute[1]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function attrWhere(array $attr): self |
|
115
|
|
|
{ |
|
116
|
|
|
if(array_key_exists('where', $attr)) { |
|
117
|
|
|
self::getInstance()->where($attr['where']); |
|
118
|
|
|
} |
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private function attrMiddleware(array $attr): self |
|
123
|
|
|
{ |
|
124
|
|
|
if(array_key_exists('middleware', $attr)) { |
|
125
|
|
|
self::getInstance()->middleware($attr['middleware']); |
|
126
|
|
|
} |
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|