1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Routes; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
abstract class Resource |
9
|
|
|
{ |
10
|
|
|
use TraitRoute; |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected array $route; |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected string $method; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected string $request; |
23
|
|
|
/** |
24
|
|
|
* @var array|null |
25
|
|
|
*/ |
26
|
|
|
protected ?array $query = null; |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected string $namespace; |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected array $namespaceArray; |
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected array $patterns; |
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected bool $notFound = true; |
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected array $callback; |
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected array $controller; |
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected array $middleware; |
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected array $type; |
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected array $verb; |
63
|
|
|
protected function callback($callback, $controller, $verb, $middleware, $type): void |
64
|
|
|
{ |
65
|
|
|
if ($this->setRequest($callback)) { |
66
|
|
|
$this->setRoute($callback); |
67
|
|
|
$this->setPatterns(); |
68
|
|
|
$this->controller[] = $controller; |
69
|
|
|
$this->middleware[] = $middleware; |
70
|
|
|
$this->type[] = $type; |
71
|
|
|
$this->verb[] = $verb; |
72
|
|
|
$this->namespaceArray[] = $this->getNamespace(); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
/** |
77
|
|
|
* @param string $namespace |
78
|
|
|
*/ |
79
|
|
|
protected function setNamespace(string $namespace): void |
80
|
|
|
{ |
81
|
|
|
$this->namespace = $namespace; |
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* @param string $route |
85
|
|
|
*/ |
86
|
|
|
protected function setRoute(string $route): void |
87
|
|
|
{ |
88
|
|
|
$this->route[] = $route; |
89
|
|
|
} |
90
|
|
|
/** |
91
|
|
|
* create pattern |
92
|
|
|
*/ |
93
|
|
|
protected function setPatterns(): void |
94
|
|
|
{ |
95
|
|
|
$this->patterns = preg_replace('~{([^}]*)}~', "([^/]+)", $this->getRoute()); |
96
|
|
|
} |
97
|
|
|
/** |
98
|
|
|
* define method global server |
99
|
|
|
*/ |
100
|
|
|
protected function setMethod(): void |
101
|
|
|
{ |
102
|
|
|
$this->method = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_DEFAULT); |
103
|
|
|
} |
104
|
|
|
/** |
105
|
|
|
* @param string $route |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function setRequest(string $route): bool |
109
|
|
|
{ |
110
|
|
|
$uri = parse_url(filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_DEFAULT)); |
111
|
|
|
$this->request = $uri['path']; |
112
|
|
|
|
113
|
|
|
$callbackCount = count(explode("/", $route)); |
114
|
|
|
$requestCount = count(explode("/", $this->request)); |
115
|
|
|
|
116
|
|
|
if (str_ends_with($this->request, '/')) { |
117
|
|
|
$this->request = substr($this->request, 0, -1); |
118
|
|
|
} |
119
|
|
|
if (isset($uri['query'])) { |
120
|
|
|
parse_str($uri['query'], $query); |
121
|
|
|
$this->setQuery($query); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $callbackCount === $requestCount || $this->request === '/'; |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* @param array|null $query |
129
|
|
|
*/ |
130
|
|
|
protected function setQuery(?array $query): void |
131
|
|
|
{ |
132
|
|
|
$this->query = $query; |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* @param bool $notFound |
136
|
|
|
*/ |
137
|
|
|
protected function setNotFound(bool $notFound): void |
138
|
|
|
{ |
139
|
|
|
$this->notFound = $notFound; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |