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 $path){ |
33
|
|
|
|
34
|
|
|
if(!is_dir($path)){ |
35
|
|
|
self::getInstance()->loadLine(new ReflectionObject(new $path())); |
36
|
|
|
continue; |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
foreach (scandir($path) as $routeFile) { |
40
|
|
|
if(pathinfo($path . DIRECTORY_SEPARATOR . $routeFile, PATHINFO_EXTENSION) === 'php'){ |
41
|
|
|
|
42
|
|
|
require_once($path . DIRECTORY_SEPARATOR . $routeFile); |
43
|
|
|
|
44
|
|
|
$classes = get_declared_classes(); |
45
|
|
|
$className = end($classes); |
46
|
|
|
self::getInstance()->loadLine(new ReflectionObject(new $className())); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function loadLine(ReflectionObject $reflection): void |
53
|
|
|
{ |
54
|
|
|
foreach($reflection->getMethods() as $method){ |
55
|
|
|
$this->loadMethod($method); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function loadMethod(ReflectionMethod $method): void |
60
|
|
|
{ |
61
|
|
|
try{ |
62
|
|
|
foreach ($method->getAttributes() as $attr) { |
63
|
|
|
if($attr->getName() != 'HnrAzevedo\Router\Route') continue; |
64
|
|
|
|
65
|
|
|
$args = $attr->getArguments(); |
66
|
|
|
|
67
|
|
|
$this->checkArgs($attr->getArguments()); |
68
|
|
|
|
69
|
|
|
self::set( |
70
|
|
|
(array_key_exists('methods', $args)) ? strtolower(implode('|', $args['methods'])) : 'get', |
71
|
|
|
(array_key_exists('uri', $args)) ? $args['uri'] : $args[0], |
72
|
|
|
$method->class.'@'.$method->name |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$this->attrName($args) |
76
|
|
|
->attrBefore($args) |
77
|
|
|
->attrAfter($args) |
78
|
|
|
->attrAttributes($args) |
79
|
|
|
->attrWhere($args) |
80
|
|
|
->attrMiddleware($args); |
81
|
|
|
} |
82
|
|
|
}catch(Exception $er){ |
83
|
|
|
throw new Exception('Failed to add route via attribute: '.$er->getMessage()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function checkArgs(array $args): self |
88
|
|
|
{ |
89
|
|
|
if(!array_key_exists('uri', $args) && !array_key_exists(0, $args)) { |
90
|
|
|
throw new Exception('Misconfigured route attribute'); |
91
|
|
|
} |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function attrName(array $attr): self |
96
|
|
|
{ |
97
|
|
|
if(array_key_exists('name', $attr)) { |
98
|
|
|
self::getInstance()->name($attr['name']); |
99
|
|
|
} |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function attrBefore(array $attr): self |
104
|
|
|
{ |
105
|
|
|
if(array_key_exists('before', $attr)) { |
106
|
|
|
self::getInstance()->before($attr['before']); |
107
|
|
|
} |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function attrAfter(array $attr): self |
112
|
|
|
{ |
113
|
|
|
if(array_key_exists('after', $attr)) { |
114
|
|
|
self::getInstance()->after($attr['after']); |
115
|
|
|
} |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function attrAttributes(array $attr): self |
120
|
|
|
{ |
121
|
|
|
if(array_key_exists('attributes', $attr)) { |
122
|
|
|
foreach($attr['attributes'] as $attribute => $attrValue){ |
123
|
|
|
self::getInstance()->attribute($attribute, $attrValue); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function attrWhere(array $attr): self |
130
|
|
|
{ |
131
|
|
|
if(array_key_exists('where', $attr)) { |
132
|
|
|
self::getInstance()->where($attr['where']); |
133
|
|
|
} |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function attrMiddleware(array $attr): self |
138
|
|
|
{ |
139
|
|
|
if(array_key_exists('middleware', $attr)) { |
140
|
|
|
self::getInstance()->middleware($attr['middleware']); |
141
|
|
|
} |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|