1 | <?php |
||
12 | class Router |
||
13 | { |
||
14 | const _ext = '?ext'; |
||
15 | const _sep = "\x1E"; |
||
16 | |||
17 | /** @var \Teto\Routing\Action[] */ |
||
18 | public $variable_actions = []; |
||
19 | |||
20 | /** @var \Teto\Routing\Action[][] */ |
||
21 | public $fixed_actions = []; |
||
22 | |||
23 | /** @var \Teto\Routing\Action[] */ |
||
24 | public $named_actions = []; |
||
25 | |||
26 | /** @var array */ |
||
27 | public $error_action = []; |
||
28 | |||
29 | public function __set($name, $value) |
||
33 | |||
34 | /** |
||
35 | * @param array $route_map |
||
36 | * @param string $method |
||
37 | * @param string $path |
||
38 | * @return \Teto\Routing\Action |
||
39 | */ |
||
40 | 24 | public static function dispatch(array $route_map, $method, $path) |
|
44 | |||
45 | /** |
||
46 | * @param array $route_map |
||
47 | */ |
||
48 | 24 | public function __construct(array $route_map) |
|
56 | |||
57 | /** |
||
58 | * @param string $method |
||
59 | * @param string $path |
||
60 | * @return \Teto\Routing\Action |
||
61 | */ |
||
62 | 48 | public function match($method, $path) |
|
63 | { |
||
64 | 48 | if ($method === 'HEAD') { $method = 'GET'; } |
|
65 | 48 | if (strpos($path, '//') !== false || strpos($path, self::_sep) !== false) { |
|
66 | return $this->getNotFoundAction($method, $path); |
||
67 | } |
||
68 | |||
69 | 48 | $split_path = array_values(array_filter(explode('/', $path), 'strlen')); |
|
70 | 48 | $count = count($split_path); |
|
71 | |||
72 | 48 | $ext = ''; |
|
73 | |||
74 | 48 | if ($count > 0) { |
|
75 | 40 | $file = explode('.', $split_path[$count - 1], 2); |
|
76 | 40 | if (isset($file[1]) && strlen($file[1]) > 0) { |
|
77 | 8 | if (strlen($file[1]) > 0) { |
|
78 | 8 | list($split_path[$count - 1], $ext) = $file; |
|
79 | } else { |
||
80 | $split_path[$count - 1] .= '.'; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | 48 | $fixed_key = implode(self::_sep, $split_path); |
|
86 | 48 | if (isset($this->fixed_actions[$fixed_key][$method])) { |
|
87 | 14 | $action = $this->fixed_actions[$fixed_key][$method]; |
|
88 | 14 | if ($matched = $action->match($method, $split_path, $ext)) { |
|
89 | 14 | return $matched; |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 34 | if (isset($this->variable_actions[$count])) { |
|
94 | 30 | foreach ($this->variable_actions[$count] as $action) { |
|
95 | 30 | if ($matched = $action->match($method, $split_path, $ext)) { |
|
96 | 30 | return $matched; |
|
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | 24 | return $this->getNotFoundAction($method, $path); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $method |
||
106 | * @param string $path |
||
107 | * @return \Teto\Routing\Action |
||
108 | */ |
||
109 | 24 | public function getNotFoundAction($method, $path) |
|
121 | |||
122 | /** |
||
123 | * @param int|string $key |
||
124 | * @param array $action_tuple |
||
125 | */ |
||
126 | 24 | public function setAction($key, array $action_tuple) |
|
158 | |||
159 | /** |
||
160 | * @param string $name |
||
161 | * @param mixed $value |
||
162 | */ |
||
163 | 24 | public function setSpecialAction($name, $value) |
|
167 | |||
168 | /** |
||
169 | * @param string $name |
||
170 | * @param array $param |
||
171 | * @param boolean $strict |
||
172 | */ |
||
173 | 15 | public function makePath($name, array $param = [], $strict = false) |
|
188 | } |
||
189 |