1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Router; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Router |
6
|
|
|
* |
7
|
|
|
* @package Mezon |
8
|
|
|
* @subpackage Router |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
* @version v.1.0 (2019/08/15) |
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Router class |
16
|
|
|
*/ |
17
|
|
|
class Router |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
use RoutesSet, UrlParser, RouteTypes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Method wich handles invalid route error |
24
|
|
|
* |
25
|
|
|
* @var callable |
26
|
|
|
*/ |
27
|
|
|
private $invalidRouteErrorHandler; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Method returns request method |
31
|
|
|
* |
32
|
|
|
* @return string Request method |
33
|
|
|
*/ |
34
|
|
|
protected function getRequestMethod(): string |
35
|
|
|
{ |
36
|
|
|
return $_SERVER['REQUEST_METHOD'] ?? 'GET'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor |
41
|
|
|
*/ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$_SERVER['REQUEST_METHOD'] = $this->getRequestMethod(); |
45
|
|
|
|
46
|
|
|
$this->invalidRouteErrorHandler = [ |
47
|
|
|
$this, |
48
|
|
|
'noProcessorFoundErrorHandler' |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$this->initDefaultTypes(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Method fetches actions from the objects and creates GetRoutes for them |
56
|
|
|
* |
57
|
|
|
* @param object $object |
58
|
|
|
* Object to be processed |
59
|
|
|
*/ |
60
|
|
|
public function fetchActions(object $object): void |
61
|
|
|
{ |
62
|
|
|
$methods = get_class_methods($object); |
63
|
|
|
|
64
|
|
|
foreach ($methods as $method) { |
65
|
|
|
if (strpos($method, 'action') === 0) { |
66
|
|
|
$route = Utils::convertMethodNameToRoute($method); |
67
|
|
|
$this->addGetRoute($route, $object, $method); |
68
|
|
|
$this->addPostRoute($route, $object, $method); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Method processes no processor found error |
75
|
|
|
* |
76
|
|
|
* @param string $route |
77
|
|
|
* Route |
78
|
|
|
*/ |
79
|
|
|
public function noProcessorFoundErrorHandler(string $route) |
80
|
|
|
{ |
81
|
|
|
throw (new \Exception( |
82
|
|
|
'The processor was not found for the route ' . $route . ' in ' . $this->getAllRoutesTrace())); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Method sets InvalidRouteErrorHandler function |
87
|
|
|
* |
88
|
|
|
* @param callable $function |
89
|
|
|
* Error handler |
90
|
|
|
*/ |
91
|
|
|
public function setNoProcessorFoundErrorHandler(callable $function) |
92
|
|
|
{ |
93
|
|
|
$oldErrorHandler = $this->invalidRouteErrorHandler; |
94
|
|
|
|
95
|
|
|
$this->invalidRouteErrorHandler = $function; |
96
|
|
|
|
97
|
|
|
return $oldErrorHandler; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Processing specified router |
102
|
|
|
* |
103
|
|
|
* @param mixed $route |
104
|
|
|
* Route |
105
|
|
|
*/ |
106
|
|
|
public function callRoute($route) |
107
|
|
|
{ |
108
|
|
|
$this->compileRegexpForBunches(); |
109
|
|
|
|
110
|
|
|
$route = Utils::prepareRoute($route); |
111
|
|
|
$requestMethod = $this->getRequestMethod(); |
112
|
|
|
$this->validateRequestMethod($requestMethod); |
113
|
|
|
|
114
|
|
|
if (($result = $this->findStaticRouteProcessor($route)) !== false) { |
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (($result = $this->findDynamicRouteProcessor($route)) !== false) { |
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
call_user_func($this->invalidRouteErrorHandler, $route); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Method returns call back by it's router |
127
|
|
|
* |
128
|
|
|
* @param array|string $route |
129
|
|
|
* route |
130
|
|
|
* @return array|callable|bool route callback |
131
|
|
|
*/ |
132
|
|
|
public function getCallback($route) |
133
|
|
|
{ |
134
|
|
|
$this->compileRegexpForBunches(); |
135
|
|
|
|
136
|
|
|
$route = Utils::prepareRoute($route); |
137
|
|
|
|
138
|
|
|
if (($result = $this->getStaticRouteProcessor($route)) !== false) { |
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (($result = $this->getDynamicRouteProcessor($route)) !== false) { |
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
call_user_func($this->invalidRouteErrorHandler, $route); // @codeCoverageIgnoreStart |
147
|
|
|
} // @codeCoverageIgnoreEnd |
148
|
|
|
} |
149
|
|
|
|