1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Preetender\Routing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Route |
10
|
|
|
* @package Preetender\Routing |
11
|
|
|
*/ |
12
|
|
|
class Route |
13
|
|
|
{ |
14
|
|
|
use RouteReflection; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $url; |
18
|
|
|
|
19
|
|
|
/** @var */ |
20
|
|
|
protected $callable; |
21
|
|
|
|
22
|
|
|
/** @var */ |
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
/** @var array */ |
26
|
|
|
private static $parameters = []; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private static $attributes = []; |
30
|
|
|
|
31
|
|
|
/** @var Request */ |
32
|
|
|
protected $request; |
33
|
|
|
|
34
|
|
|
/** @var Response */ |
35
|
|
|
protected $response; |
36
|
|
|
|
37
|
|
|
/** @var */ |
38
|
|
|
protected $running; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Route constructor. |
42
|
|
|
* @param $url |
43
|
|
|
* @param $callable |
44
|
|
|
* @param null $name |
45
|
|
|
*/ |
46
|
|
|
public function __construct($url, $callable, $name = null) |
47
|
|
|
{ |
48
|
|
|
$this->url = trim($url, '/'); |
49
|
|
|
$this->callable = $callable; |
50
|
|
|
$this->request = Kernel::getContainer()->get(Request::class); |
51
|
|
|
$this->response = Kernel::getContainer()->get(Response::class); |
52
|
|
|
$this->name = $name; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check the path, if there are parameters to create attributes for the scope of the request |
57
|
|
|
* |
58
|
|
|
* @param $url |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function extractParameters($url) |
62
|
|
|
{ |
63
|
|
|
$url = trim($url, '/'); |
64
|
|
|
$path = preg_replace_callback('#:([\w]+)#', [$this, 'attributesMatch'], $this->getUrl()); |
65
|
|
|
if(!preg_match("#^$path$#i", $url, $matches)) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
array_shift($matches); |
69
|
|
|
self::$parameters = $matches; |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* ... |
75
|
|
|
* |
76
|
|
|
* @param $match |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
private function attributesMatch($match) { |
80
|
|
|
if(isset(self::$attributes[$match[1]])) { |
81
|
|
|
return '(' . self::$attributes[$match[1]] . ')'; |
82
|
|
|
} |
83
|
|
|
return '([^/]+)'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get url. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getUrl(): string |
92
|
|
|
{ |
93
|
|
|
return $this->url; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get name route |
98
|
|
|
* |
99
|
|
|
* @return null|string |
100
|
|
|
*/ |
101
|
|
|
public function getName() |
102
|
|
|
{ |
103
|
|
|
return $this->name; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get callback. |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
public function getCallable() |
112
|
|
|
{ |
113
|
|
|
return $this->callable; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get params. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public static function getParameters(): array |
122
|
|
|
{ |
123
|
|
|
return self::$parameters; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get properties on route |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function getRoute() |
132
|
|
|
{ |
133
|
|
|
return [ |
134
|
|
|
'name' => $this->getName(), |
135
|
|
|
'path' => $this->getUrl(), |
136
|
|
|
'callable' => $this->getCallable(), |
137
|
|
|
'parameters' => self::resolveParameters() |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get request |
143
|
|
|
* |
144
|
|
|
* @return Request |
145
|
|
|
*/ |
146
|
|
|
public function getRequest(): Request |
147
|
|
|
{ |
148
|
|
|
return $this->request; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get reponse |
153
|
|
|
* |
154
|
|
|
* @return Response |
155
|
|
|
*/ |
156
|
|
|
public function getResponse(): Response |
157
|
|
|
{ |
158
|
|
|
return $this->response; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Executing route |
163
|
|
|
* |
164
|
|
|
* @param array $parameters |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function requestUrlCall($parameters = []) |
168
|
|
|
{ |
169
|
|
|
$path = $this->getUrl(); |
170
|
|
|
foreach ($parameters as $key => $value) { |
171
|
|
|
$path = str_replace(":$key", $value, $path); |
172
|
|
|
} |
173
|
|
|
$this->extractParameters($path); |
174
|
|
|
return $this->run(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Exporta para o controlador uma coleção de dados; |
179
|
|
|
* |
180
|
|
|
* @param string $attribute |
181
|
|
|
* @param string $regex |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function with(string $attribute, string $regex) |
185
|
|
|
{ |
186
|
|
|
self::$attributes[$attribute] = str_replace('(', '(?:', $regex); |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Check the type of request and define which treatment it should receive |
192
|
|
|
* |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
public function run() |
196
|
|
|
{ |
197
|
|
|
if(is_string($this->getCallable())) { |
198
|
|
|
return $this->handleRequestController(); |
199
|
|
|
} |
200
|
|
|
return $this->handleRequestCallable(); |
201
|
|
|
} |
202
|
|
|
} |