Completed
Push — master ( 2956ed...a8d912 )
by Nazar
06:36
created

CLI::print_cli_structure()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.583

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 8
nop 1
dl 0
loc 30
ccs 15
cts 21
cp 0.7143
crap 5.583
rs 8.439
c 0
b 0
f 0
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 3
	protected function print_cli_structure ($path) {
17 3
		$result = [];
18
		/**
19
		 * @var array $modules
20
		 */
21 3
		$modules = Config::instance()->components['modules'];
22 3
		foreach ($modules as $module_name => $data) {
23 3
			if ($data['active'] == Module_Properties::ENABLED) {
24 3
				$working_dir = MODULES."/$module_name/cli";
25 3
				$structure   = file_exists("$working_dir/index.json") ? file_get_json("$working_dir/index.json") : [];
26 3
				$this->print_cli_structure_internal($working_dir, $module_name, '', $structure, $result[$module_name]);
27
			}
28
		}
29 3
		$result = $this->print_cli_structure_normalize_result($result);
30 3
		$Page   = Page::instance();
31
		// Cut `/cli/` prefix
32 3
		$path = substr($path, 5);
33 3
		if ($path) {
34
			$Page->content("<y>Paths and methods for \"$path\":</y>\n");
35
			$result = array_filter(
36
				$result,
37
				function ($item) use ($path) {
38
					return strpos($item[0], $path) === 0;
39
				}
40
			);
41
		} else {
42 3
			$Page->content("<y>All paths and methods:</y>\n");
43
		}
44 3
		$Page->content(
45 3
			implode("\n", (new Table(['Path', 'Methods available'], $result))->getDisplayLines())."\n"
46
		);
47
	}
48
	/**
49
	 * @param string $dir
50
	 * @param string $module_name
51
	 * @param string $basename
52
	 * @param array  $structure
53
	 * @param array  $result
54
	 */
55 3
	protected function print_cli_structure_internal ($dir, $module_name, $basename, $structure, &$result) {
56
		/** @noinspection NestedTernaryOperatorInspection */
57 3
		foreach ($structure ?: (!$basename ? ['index'] : []) as $path => $nested_structure) {
58 3
			if (!is_array($nested_structure)) {
59 3
				$path             = $nested_structure;
60 3
				$nested_structure = [];
61
			}
62 3
			$key = $path == '_' ? 0 : $path;
63 3
			if (file_exists("$dir/Controller.php")) {
64 3
				$result[$key] = $this->controller_router_available_methods(
0 ignored issues
show
Bug introduced by
It seems like controller_router_available_methods() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
				/** @scrutinizer ignore-call */ 
65
    $result[$key] = $this->controller_router_available_methods(
Loading history...
65 3
					$dir,
66 3
					"\\cs\\modules\\$module_name\\cli\\Controller",
67 3
					$basename ? $basename.'_'.$path : $path
68
				);
69 3
				$new_dir      = $dir;
70 3
				$new_basename = $basename ? $basename.'_'.$path : $path;
71
			} else {
72 3
				$result[$key] = $this->files_router_available_methods($dir, $path);
0 ignored issues
show
Bug introduced by
It seems like files_router_available_methods() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
				/** @scrutinizer ignore-call */ 
73
    $result[$key] = $this->files_router_available_methods($dir, $path);
Loading history...
73 3
				$new_dir      = "$dir/$path";
74 3
				$new_basename = $basename;
75
			}
76 3
			if ($structure && $nested_structure) {
77 3
				$this->print_cli_structure_internal($new_dir, $module_name, $new_basename, $nested_structure, $result[$key]);
78
			}
79
		}
80 3
	}
81
	/**
82
	 * @param array  $result
83
	 * @param string $prefix
84
	 *
85
	 * @return string[]
86
	 */
87 3
	protected function print_cli_structure_normalize_result ($result, $prefix = '') {
88 3
		$normalized = [];
89 3
		foreach ($result as $key => $value) {
90 3
			if (is_array_assoc($value)) {
91 3
				if (!$prefix && isset($value['index'])) {
92 3
					$value[0] = $value['index'];
93 3
					unset($value['index']);
94
				}
95 3
				if (is_array(@$value[0]) && $value[0]) {
96 3
					$normalized[] = [$prefix.$key, strtolower(implode(', ', $value[0]))];
97
				}
98 3
				unset($value[0]);
99
				/** @noinspection SlowArrayOperationsInLoopInspection */
100 3
				$normalized = array_merge($normalized, $this->print_cli_structure_normalize_result($value, $prefix.$key.'/'));
101 3
			} elseif (is_array($value) && $value) {
102 3
				$normalized[] = [$prefix.$key, strtolower(implode(', ', $value))];
103
			}
104
		}
105 3
		return $normalized;
106
	}
107
}
108