1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Libraries; |
3
|
|
|
|
4
|
|
|
final class Router extends \WebServCo\Framework\AbstractLibrary |
5
|
|
|
{ |
6
|
|
|
public function getRoute($requestCustom, $routes, $extraArgs = []) |
7
|
|
|
{ |
8
|
|
|
$routeString = $this->parseCustomRoutes($requestCustom, $routes); |
9
|
|
|
if (empty($routeString)) { |
10
|
|
|
return $this->setting('default_route', [null, null, null]); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
$controller = ''; |
14
|
|
|
$action = ''; |
15
|
|
|
$args = []; |
16
|
|
|
|
17
|
|
|
$parts = explode('/', $routeString, 3); |
18
|
|
|
if (!empty($parts['0'])) { |
19
|
|
|
$controller = $parts[0]; |
20
|
|
|
} |
21
|
|
|
if (!empty($parts['1'])) { |
22
|
|
|
$action = $parts[1]; |
23
|
|
|
} |
24
|
|
|
if (!empty($parts['2'])) { |
25
|
|
|
$args = explode('/', $parts[2]); |
26
|
|
|
} |
27
|
|
|
if (!empty($extraArgs)) { |
28
|
|
|
foreach ($extraArgs as $k => $v) { |
29
|
|
|
$args[] = $v; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
return [$controller, $action, $args]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function parseCustomRoutes($requestCustom, $routes) |
36
|
|
|
{ |
37
|
|
|
if (is_array($routes)) { |
38
|
|
|
foreach ($routes as $k => $v) { |
39
|
|
|
$k = str_replace(['{num}','{any}'], ['[0-9]+','.+'], $k); |
40
|
|
|
/** |
41
|
|
|
* Check for a custom route match. |
42
|
|
|
*/ |
43
|
|
|
if (preg_match("#^{$k}$#", $requestCustom) || |
44
|
|
|
preg_match("#^{$k}/$#", $requestCustom) |
45
|
|
|
) { |
46
|
|
|
/** |
47
|
|
|
* Check for back references. |
48
|
|
|
*/ |
49
|
|
|
if (false !== strpos($v, '$') && false !== strpos($k, '(')) { |
50
|
|
|
/** |
51
|
|
|
* Parse request. |
52
|
|
|
*/ |
53
|
|
|
$v = preg_replace("#^{$k}$#", $v, $requestCustom); |
54
|
|
|
} |
55
|
|
|
return $v; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return $requestCustom; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|