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