|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2015-2017, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs\App\Router; |
|
9
|
|
|
use |
|
10
|
|
|
cli\Table, |
|
11
|
|
|
cs\Config, |
|
12
|
|
|
cs\Config\Module_Properties, |
|
13
|
|
|
cs\Page; |
|
14
|
|
|
|
|
15
|
|
|
trait CLI { |
|
16
|
2 |
|
protected function print_cli_structure ($path) { |
|
17
|
2 |
|
$result = []; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array $modules |
|
20
|
|
|
*/ |
|
21
|
2 |
|
$modules = Config::instance()->components['modules']; |
|
22
|
2 |
|
foreach ($modules as $module_name => $data) { |
|
23
|
2 |
|
if ($data['active'] == Module_Properties::ENABLED) { |
|
24
|
2 |
|
$working_dir = MODULES."/$module_name/cli"; |
|
25
|
2 |
|
$structure = file_exists("$working_dir/index.json") ? file_get_json("$working_dir/index.json") : []; |
|
26
|
2 |
|
$this->print_cli_structure_internal($working_dir, $module_name, '', $structure, $result[$module_name]); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
2 |
|
$result = $this->print_cli_structure_normalize_result($result); |
|
30
|
2 |
|
$Page = Page::instance(); |
|
31
|
|
|
// Cut `/cli/` prefix |
|
32
|
2 |
|
$path = substr($path, 5); |
|
33
|
2 |
|
if ($path) { |
|
34
|
2 |
|
$Page->content("<y>Paths and methods for \"$path\":</y>\n"); |
|
35
|
2 |
|
$result = array_filter( |
|
36
|
2 |
|
$result, |
|
37
|
2 |
|
function ($item) use ($path) { |
|
38
|
2 |
|
return strpos($item[0], $path) === 0; |
|
39
|
2 |
|
} |
|
40
|
|
|
); |
|
41
|
|
|
} else { |
|
42
|
2 |
|
$Page->content("<y>All paths and methods:</y>\n"); |
|
43
|
|
|
} |
|
44
|
2 |
|
$Page->content( |
|
45
|
2 |
|
implode("\n", (new Table(['Path', 'Methods available'], $result))->getDisplayLines())."\n" |
|
46
|
|
|
); |
|
47
|
2 |
|
} |
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $dir |
|
50
|
|
|
* @param string $module_name |
|
51
|
|
|
* @param string $basename |
|
52
|
|
|
* @param array $structure |
|
53
|
|
|
* @param array $result |
|
54
|
|
|
*/ |
|
55
|
2 |
|
protected function print_cli_structure_internal ($dir, $module_name, $basename, $structure, &$result) { |
|
56
|
|
|
/** @noinspection NestedTernaryOperatorInspection */ |
|
57
|
2 |
|
foreach ($structure ?: (!$basename ? ['index'] : []) as $path => $nested_structure) { |
|
58
|
2 |
|
if (!is_array($nested_structure)) { |
|
59
|
2 |
|
$path = $nested_structure; |
|
60
|
2 |
|
$nested_structure = []; |
|
61
|
|
|
} |
|
62
|
2 |
|
$key = $path == '_' ? 0 : $path; |
|
63
|
2 |
|
if (file_exists("$dir/Controller.php")) { |
|
64
|
2 |
|
$result[$key] = $this->controller_router_available_methods( |
|
65
|
2 |
|
$dir, |
|
66
|
2 |
|
"\\cs\\modules\\$module_name\\cli\\Controller", |
|
67
|
2 |
|
$basename ? $basename.'_'.$path : $path |
|
68
|
|
|
); |
|
69
|
2 |
|
$new_dir = $dir; |
|
70
|
2 |
|
$new_basename = $basename ? $basename.'_'.$path : $path; |
|
71
|
|
|
} else { |
|
72
|
2 |
|
$result[$key] = $this->files_router_available_methods($dir, $path); |
|
73
|
2 |
|
$new_dir = "$dir/$path"; |
|
74
|
2 |
|
$new_basename = $basename; |
|
75
|
|
|
} |
|
76
|
2 |
|
if ($structure && $nested_structure) { |
|
77
|
2 |
|
$this->print_cli_structure_internal($new_dir, $module_name, $new_basename, $nested_structure, $result[$key]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
2 |
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $result |
|
83
|
|
|
* @param string $prefix |
|
84
|
|
|
* |
|
85
|
|
|
* @return string[] |
|
86
|
|
|
*/ |
|
87
|
2 |
|
protected function print_cli_structure_normalize_result ($result, $prefix = '') { |
|
88
|
2 |
|
$normalized = []; |
|
89
|
2 |
|
foreach ($result as $key => $value) { |
|
90
|
2 |
|
if (is_array_assoc($value)) { |
|
91
|
2 |
|
if (!$prefix && isset($value['index'])) { |
|
92
|
2 |
|
$value[0] = $value['index']; |
|
93
|
2 |
|
unset($value['index']); |
|
94
|
|
|
} |
|
95
|
2 |
|
if (is_array(@$value[0]) && $value[0]) { |
|
96
|
2 |
|
$normalized[] = [$prefix.$key, strtolower(implode(', ', $value[0]))]; |
|
97
|
|
|
} |
|
98
|
2 |
|
unset($value[0]); |
|
99
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
|
100
|
2 |
|
$normalized = array_merge($normalized, $this->print_cli_structure_normalize_result($value, $prefix.$key.'/')); |
|
101
|
2 |
|
} elseif (is_array($value) && $value) { |
|
102
|
2 |
|
$normalized[] = [$prefix.$key, strtolower(implode(', ', $value))]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
2 |
|
return $normalized; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|