|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs\App\Router; |
|
9
|
|
|
use |
|
10
|
|
|
cs\Page, |
|
11
|
|
|
cs\Response; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @property string[] $controller_path Path that will be used by controller to render page |
|
15
|
|
|
*/ |
|
16
|
|
|
trait Controller { |
|
17
|
|
|
/** |
|
18
|
|
|
* Call methods necessary for module page rendering |
|
19
|
|
|
* |
|
20
|
|
|
* @param \cs\Request $Request |
|
21
|
|
|
* |
|
22
|
|
|
* @throws \cs\ExitException |
|
23
|
|
|
*/ |
|
24
|
8 |
|
protected function controller_router ($Request) { |
|
25
|
8 |
|
$suffix = ''; |
|
26
|
8 |
|
if ($Request->cli_path) { |
|
27
|
2 |
|
$suffix = '\\cli'; |
|
28
|
8 |
|
} elseif ($Request->admin_path) { |
|
29
|
2 |
|
$suffix = '\\admin'; |
|
30
|
8 |
|
} elseif ($Request->api_path) { |
|
31
|
6 |
|
$suffix = '\\api'; |
|
32
|
|
|
} |
|
33
|
8 |
|
$controller_class = class_exists("cs\\custom\\modules\\$Request->current_module$suffix\\Controller") |
|
34
|
2 |
|
? "cs\\custom\\modules\\$Request->current_module$suffix\\Controller" |
|
35
|
8 |
|
: "cs\\modules\\$Request->current_module$suffix\\Controller"; |
|
36
|
8 |
|
foreach ($this->controller_path as $index => $path) { |
|
37
|
|
|
/** |
|
38
|
|
|
* Starting from index 2 (first is always `index`) we need to maintain underscore-separated string that includes all paths from index 1 and till |
|
39
|
|
|
* current |
|
40
|
|
|
*/ |
|
41
|
8 |
|
if ($index > 1) { |
|
42
|
2 |
|
$path = implode('_', array_slice($this->controller_path, 1, $index)); |
|
43
|
|
|
} |
|
44
|
8 |
|
$next_exists = isset($this->controller_path[$index + 1]); |
|
45
|
8 |
|
$this->controller_router_handler($Request, $controller_class, $path, !$next_exists); |
|
46
|
|
|
} |
|
47
|
8 |
|
} |
|
48
|
|
|
/** |
|
49
|
|
|
* Call methods that corresponds for specific paths in URL |
|
50
|
|
|
* |
|
51
|
|
|
* @param \cs\Request $Request |
|
52
|
|
|
* @param string $controller_class |
|
53
|
|
|
* @param string $method_name |
|
54
|
|
|
* @param bool $required |
|
55
|
|
|
* |
|
56
|
|
|
* @throws \cs\ExitException |
|
57
|
|
|
*/ |
|
58
|
8 |
|
protected function controller_router_handler ($Request, $controller_class, $method_name, $required = true) { |
|
59
|
8 |
|
$method_name = str_replace('.', '_', $method_name); |
|
60
|
8 |
|
$this->controller_router_handler_internal($Request, $controller_class, $method_name, $required); |
|
61
|
8 |
|
} |
|
62
|
|
|
/** |
|
63
|
|
|
* @param \cs\Request $Request |
|
64
|
|
|
* @param string $controller_class |
|
65
|
|
|
* @param string $method_name |
|
66
|
|
|
* @param bool $required |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \cs\ExitException |
|
69
|
|
|
*/ |
|
70
|
8 |
|
protected function controller_router_handler_internal ($Request, $controller_class, $method_name, $required) { |
|
71
|
8 |
|
$Response = Response::instance(); |
|
72
|
8 |
|
$found = $this->controller_router_handler_internal_execute($controller_class, $method_name, $Request, $Response); |
|
73
|
8 |
|
if (!$Request->cli_path && !$Request->api_path) { |
|
74
|
4 |
|
return; |
|
75
|
|
|
} |
|
76
|
6 |
|
$request_method = strtolower($Request->method); |
|
77
|
6 |
|
$found = $this->controller_router_handler_internal_execute($controller_class, $method_name.'_'.$request_method, $Request, $Response) || $found; |
|
78
|
6 |
|
if ($found || !$required) { |
|
79
|
6 |
|
return; |
|
80
|
|
|
} |
|
81
|
2 |
|
$this->handler_not_found( |
|
82
|
2 |
|
$this->controller_router_available_methods($this->working_directory, $controller_class, $method_name), |
|
83
|
|
|
$request_method, |
|
84
|
|
|
$Request |
|
85
|
|
|
); |
|
86
|
2 |
|
} |
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $working_directory |
|
89
|
|
|
* @param string $controller_class |
|
90
|
|
|
* @param string $method_name |
|
91
|
|
|
* |
|
92
|
|
|
* @return string[] |
|
93
|
|
|
*/ |
|
94
|
2 |
|
protected function controller_router_available_methods ($working_directory, $controller_class, $method_name) { |
|
95
|
2 |
|
$structure = file_exists("$working_directory/index.json") ? file_get_json("$working_directory/index.json") : ['index']; |
|
96
|
2 |
|
$structure = $this->controller_router_available_methods_to_flat_structure($structure); |
|
97
|
2 |
|
$methods = array_filter( |
|
98
|
2 |
|
get_class_methods($controller_class) ?: [], |
|
99
|
2 |
|
function ($found_method) use ($method_name, $structure) { |
|
100
|
2 |
|
if (!preg_match("/^{$method_name}_[a-z_]+$/", $found_method)) { |
|
101
|
2 |
|
return false; |
|
102
|
|
|
} |
|
103
|
2 |
|
foreach ($structure as $structure_method) { |
|
104
|
2 |
|
if (strpos($found_method, $structure_method) === 0 && strpos($method_name, $structure_method) !== 0) { |
|
105
|
2 |
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
2 |
|
return true; |
|
109
|
2 |
|
} |
|
110
|
|
|
); |
|
111
|
2 |
|
$methods = _strtoupper(_substr($methods, strlen($method_name) + 1)); |
|
112
|
2 |
|
natcasesort($methods); |
|
113
|
2 |
|
return array_values($methods); |
|
114
|
|
|
} |
|
115
|
|
|
/** |
|
116
|
|
|
* @param array $structure |
|
117
|
|
|
* @param string $prefix |
|
118
|
|
|
* |
|
119
|
|
|
* @return string[] |
|
120
|
|
|
*/ |
|
121
|
2 |
|
protected function controller_router_available_methods_to_flat_structure ($structure, $prefix = '') { |
|
122
|
|
|
// First key in order to avoid warning when `$flat_structure` is empty at `return` |
|
123
|
2 |
|
$flat_structure = [[]]; |
|
124
|
2 |
|
foreach ($structure as $path => $nested_structure) { |
|
125
|
2 |
|
if (!is_array($nested_structure)) { |
|
126
|
2 |
|
$path = $nested_structure; |
|
127
|
2 |
|
$nested_structure = []; |
|
128
|
|
|
} |
|
129
|
2 |
|
$flat_structure[] = [$prefix.$path]; |
|
130
|
2 |
|
$flat_structure[] = $this->controller_router_available_methods_to_flat_structure($nested_structure, $prefix.$path.'_'); |
|
131
|
|
|
} |
|
132
|
2 |
|
return array_merge(...$flat_structure); |
|
133
|
|
|
} |
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $controller_class |
|
136
|
|
|
* @param string $method_name |
|
137
|
|
|
* @param \cs\Request $Request |
|
138
|
|
|
* @param Response $Response |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
8 |
|
protected function controller_router_handler_internal_execute ($controller_class, $method_name, $Request, $Response) { |
|
143
|
8 |
|
if (!method_exists($controller_class, $method_name)) { |
|
144
|
8 |
|
return false; |
|
145
|
|
|
} |
|
146
|
6 |
|
$result = $controller_class::$method_name($Request, $Response); |
|
147
|
6 |
|
if ($result !== null) { |
|
148
|
2 |
|
Page::instance()->{$Request->api_path ? 'json' : 'content'}($result); |
|
149
|
|
|
} |
|
150
|
6 |
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|