1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
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
|
|
|
protected function files_router ($Request) { |
22
|
|
|
foreach ($this->controller_path as $index => $path) { |
23
|
|
|
/** |
24
|
|
|
* Starting from index 2 we need to maintain slash-separated string that includes all paths from index 1 and till current |
25
|
|
|
*/ |
26
|
|
|
if ($index > 1) { |
27
|
|
|
$path = implode('/', array_slice($this->controller_path, 1, $index)); |
28
|
|
|
} |
29
|
|
|
$next_exists = isset($this->controller_path[$index + 1]); |
30
|
|
|
$this->files_router_handler($Request, $this->working_directory, $path, !$next_exists); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
/** |
34
|
|
|
* Include files that corresponds for specific paths in URL |
35
|
|
|
* |
36
|
|
|
* @param \cs\Request $Request |
37
|
|
|
* @param string $dir |
38
|
|
|
* @param string $basename |
39
|
|
|
* @param bool $required |
40
|
|
|
* |
41
|
|
|
* @throws \cs\ExitException |
42
|
|
|
*/ |
43
|
|
|
protected function files_router_handler ($Request, $dir, $basename, $required = true) { |
44
|
|
|
$this->files_router_handler_internal($Request, $dir, $basename, $required); |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* @param \cs\Request $Request |
48
|
|
|
* @param string $dir |
49
|
|
|
* @param string $basename |
50
|
|
|
* @param bool $required |
51
|
|
|
* |
52
|
|
|
* @throws \cs\ExitException |
53
|
|
|
*/ |
54
|
|
|
protected function files_router_handler_internal ($Request, $dir, $basename, $required) { |
55
|
|
|
$included = _include("$dir/$basename.php", false, false) !== false; |
56
|
|
|
if (!$Request->cli_path && !$Request->api_path) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
$request_method = strtolower($Request->method); |
60
|
|
|
$included = _include("$dir/$basename.$request_method.php", false, false) !== false || $included; |
61
|
|
|
if ($included || !$required) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
$this->handler_not_found( |
|
|
|
|
65
|
|
|
$this->files_router_available_methods($dir, $basename), |
66
|
|
|
$request_method, |
67
|
|
|
$Request |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
/** |
71
|
|
|
* @param string $dir |
72
|
|
|
* @param string $basename |
73
|
|
|
* |
74
|
|
|
* @return string[] |
75
|
|
|
*/ |
76
|
|
|
protected function files_router_available_methods ($dir, $basename) { |
77
|
|
|
$methods = get_files_list($dir, "/^$basename\\.[a-z]+\\.php$/"); |
78
|
|
|
$methods = _strtoupper(_substr($methods, strlen($basename) + 1, -4)); |
79
|
|
|
natcasesort($methods); |
80
|
|
|
return array_values($methods); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.