-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
207 lines (172 loc) · 5.71 KB
/
Router.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Created by zhou.
* User: zhou
* Date: 2015/11/28
* Time: 14:52
*/
namespace shiwolang\router;
class Router
{
/**
* @var Router[]
*/
protected static $_instance = [];
/**
* @var Log[]
*/
protected $log = [];
/**
* @var Route[]
*/
protected $routes = [];
/**
* @var \shiwolang\router\CallBack[][]
*/
protected $contexts = [];
protected $handlers = [];
protected $debug = true;
public static function init($name = "default", $reinit = false)
{
if (!isset(self::$_instance[$name]) || $reinit) {
self::$_instance[$name] = new self();
} else {
throw new RouterException("The Router name of (" . $name . ") does exist");
}
return self::$_instance[$name];
}
public static function instance($name = "default")
{
if (isset(self::$_instance[$name])) {
return self::$_instance[$name];
} else {
throw new RouterException("The Router name of (" . $name . ") does not exist");
}
}
public function execute($routeString, $debug = true)
{
$this->debug = $debug;
$matchedRoute = $this->routeExecuter($routeString);
$matchedRouteName = $matchedRoute[0];
/** @var Route $matchedRoute */
$matchedRoute = $matchedRoute[1];
$namesExplode = explode(".", $matchedRouteName);
$myselfhander = function () use ($matchedRouteName, $matchedRoute, $namesExplode) {
$this->contextExecuter($namesExplode);
$result = $matchedRoute->invoke();
$this->contextExecuter($namesExplode, false);
return $result;
};
$_matchedRouteName = $namesExplode;
$_matchedRouteNameCount = count($_matchedRouteName);
$handers[] = CallBack::init([
"name" => '__$$$$$__',
"callback" => $myselfhander
]);;
for ($i = 1; $i <= $_matchedRouteNameCount; $i++) {
$name = implode(".", $_matchedRouteName);
array_pop($_matchedRouteName);
if (isset($this->handlers[$name])) {
$handers[] = $this->handlers[$name][1];
}
}
$this->handlerExecuter($handers, count($handers), 1);
}
public function routeExecuter($routeString)
{
/** @var route[] $matchedRoutes */
$matchedRoutes = [];
$context = null;
foreach ($this->routes as $name => $route) {
if ($route->match($routeString)) {
$matchedRoutes[$name] = $route;
if (!$this->debug) {
break;
}
}
}
$matchedRoutesCount = count($matchedRoutes);
if ($matchedRoutesCount > 1) {
throw new RouterException("Duplicate route pattern match for( " . implode(" );( ", array_keys($matchedRoutes)) . " )");
}
if ($matchedRoutesCount == 0) {
throw new RouterException("Can not match route of " . $routeString . "!", 404);
}
return [current(array_keys($matchedRoutes)), current($matchedRoutes)];
}
public function contextExecuter($matchedRouteName, $invoke = true)
{
$name = "";
foreach ($matchedRouteName as $key => $namePart) {
$name .= $key == 0 ? $namePart : "." . $namePart;
if (isset($this->contexts[$name])) {
$callbacks = $this->contexts[$name];
foreach ($callbacks as $callback) {
$invoke ? $callback->invoke() : $callback->invokeHandled();
}
}
}
}
public function handlerExecuter($handers, $max, $offset = 1)
{
/** @var \shiwolang\router\CallBack[] $handers */
$nextOffset = $offset + 1;
$nextOffset = $nextOffset >= $max ? null : $nextOffset;
if ($nextOffset === null) {
if ($max == 1) {
return $handers[0]->invoke([]);
}
return $handers[$offset]->invoke([$handers[$offset - 1]]);
} else {
$handers[$offset]->parent = $handers[$offset - 1];
return $this->handlerExecuter($handers, $max, $nextOffset);
}
}
public function addRoute(Route $route, $name = null, $reSet = false)
{
if ($name === null) {
$this->routes[] = $route;
return;
}
if ($reSet || !isset($this->routes[$name])) {
$route->name = $name;
$this->routes[$name] = $route;
} else {
throw new RouterException("Route of name:(" . $name . ") does exist");
}
}
public function addContext(CallBack $callBack, $name, $append = true)
{
if (!isset($this->routes[$name])) {
throw new RouterException("Route of name:(" . $name . ") does not exist");
}
$callBack->name = $name;
$append ?
$this->contexts[$name][] = $callBack :
array_unshift($this->contexts[$name], $callBack);
}
public function addHandler(CallBack $callBack, $name, $reSet = false)
{
$executeRoute = true;
if ($reSet) {
$this->handlers[$name] = [$executeRoute, $callBack];
return;
}
if (isset($this->handlers[$name])) {
throw new RouterException("Handler of name:(" . $name . ") does exist");
}
$callBack->name = $name;
$this->handlers[$name] = [$executeRoute, $callBack];
}
public function getRoutes()
{
return $this->routes;
}
public static function urlFor($name)
{
}
/////////////////////////////////////////////
private function __construct()
{
}
}