1 | <?php |
||
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 = '') { |
|
107 | } |
||
108 |