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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property string[] $controller_path Path that will be used by controller to render page |
12
|
|
|
*/ |
13
|
|
|
trait Files { |
14
|
|
|
/** |
15
|
|
|
* Include files necessary for module page rendering |
16
|
|
|
* |
17
|
|
|
* @param \cs\Request $Request |
18
|
|
|
* |
19
|
|
|
* @throws \cs\ExitException |
20
|
|
|
*/ |
21
|
2 |
|
protected function files_router ($Request) { |
22
|
2 |
|
foreach ($this->controller_path as $index => $path) { |
23
|
2 |
|
$dir = $this->working_directory; |
24
|
|
|
/** |
25
|
|
|
* Starting from index 2 (first is always `index`) we need to maintain slash-separated string that includes all paths from index 1 and till pre-last |
26
|
|
|
* (last is base filename, while pre-last goes into directory path) |
27
|
|
|
*/ |
28
|
2 |
|
if ($index > 1) { |
29
|
2 |
|
$dir .= '/'.implode('/', array_slice($this->controller_path, 1, $index - 1)); |
30
|
|
|
} |
31
|
2 |
|
$next_exists = isset($this->controller_path[$index + 1]); |
32
|
2 |
|
$this->files_router_handler($Request, $dir, $path, !$next_exists); |
33
|
|
|
} |
34
|
2 |
|
} |
35
|
|
|
/** |
36
|
|
|
* Include files that corresponds for specific paths in URL |
37
|
|
|
* |
38
|
|
|
* @param \cs\Request $Request |
39
|
|
|
* @param string $dir |
40
|
|
|
* @param string $basename |
41
|
|
|
* @param bool $required |
42
|
|
|
* |
43
|
|
|
* @throws \cs\ExitException |
44
|
|
|
*/ |
45
|
2 |
|
protected function files_router_handler ($Request, $dir, $basename, $required = true) { |
46
|
2 |
|
$this->files_router_handler_internal($Request, $dir, $basename, $required); |
47
|
2 |
|
} |
48
|
|
|
/** |
49
|
|
|
* @param \cs\Request $Request |
50
|
|
|
* @param string $dir |
51
|
|
|
* @param string $basename |
52
|
|
|
* @param bool $required |
53
|
|
|
* |
54
|
|
|
* @throws \cs\ExitException |
55
|
|
|
*/ |
56
|
2 |
|
protected function files_router_handler_internal ($Request, $dir, $basename, $required) { |
57
|
2 |
|
$included = _include("$dir/$basename.php", false, false) !== false; |
58
|
2 |
|
if (!$Request->cli_path && !$Request->api_path) { |
59
|
2 |
|
return; |
60
|
|
|
} |
61
|
2 |
|
$request_method = strtolower($Request->method); |
62
|
2 |
|
$included = _include("$dir/$basename.$request_method.php", false, false) !== false || $included; |
63
|
2 |
|
if ($included || !$required) { |
64
|
2 |
|
return; |
65
|
|
|
} |
66
|
2 |
|
$this->handler_not_found( |
67
|
2 |
|
$this->files_router_available_methods($dir, $basename), |
68
|
|
|
$request_method, |
69
|
|
|
$Request |
70
|
|
|
); |
71
|
2 |
|
} |
72
|
|
|
/** |
73
|
|
|
* @param string $dir |
74
|
|
|
* @param string $basename |
75
|
|
|
* |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
2 |
|
protected function files_router_available_methods ($dir, $basename) { |
79
|
2 |
|
$methods = get_files_list($dir, "/^$basename\\.[a-z]+\\.php$/"); |
80
|
2 |
|
$methods = _strtoupper(_substr($methods, strlen($basename) + 1, -4)); |
81
|
2 |
|
natcasesort($methods); |
82
|
2 |
|
return array_values($methods); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|