|
6 | 6 | $base = dirname(__FILE__);
|
7 | 7 | require_once "{$base}/src/Used.php";
|
8 | 8 | $getCnf = require_once "{$base}/GetUsed.config.php";
|
9 |
| -if (is_file($usrCnf = "{$base}/user.config.php")) |
10 |
| - $getCnf = array_replace_recursive($getCnf, require_once $usrCnf); |
11 | 9 |
|
12 | 10 | /** @var mixed parsed arguments */
|
13 | 11 | parse_str(implode('&', $argv), $argv);
|
14 | 12 |
|
15 |
| -/** |
16 |
| - * @var array check if shell options are called and execute if ['-h', ...] |
17 |
| - */ |
18 |
| -foreach($getCnf['config']['options'] as $key => $shortOpt) |
19 |
| - if (isset($argv[$shortOpt]) AND isset($getCnf[$key])) |
20 |
| - exit(json_encode([$key => $getCnf[$key]], JSON_PRETTY_PRINT) . PHP_EOL); |
21 |
| - |
22 | 13 | /**
|
23 | 14 | * @var mixed If "file=" is left out in command
|
24 | 15 | */
|
25 | 16 | if (!isset($argv['file']) AND $pFilename = ($_SERVER['argv'][1] ?? false))
|
26 |
| - if (file_exists($pFilename)) |
| 17 | + if (is_file($pFilename)) |
27 | 18 | $argv['file'] = $pFilename;
|
28 | 19 |
|
29 | 20 | /**
|
30 |
| - * @var mixed sets & args |
| 21 | + * @var mixed set args |
31 | 22 | */
|
32 |
| -$autoloadFile = '/vendor/autoload.php'; |
33 |
| -$projectDir = $argv['project_dir'] ?? null; |
34 |
| -$argv['include'] = $argv['include'] ?? null; |
35 |
| -$argv['file'] = $argv['file'] ?? null ? "{$projectDir}{$argv['file']}" : null; |
| 23 | +$argv['file'] = $argv['file'] ?? null; |
36 | 24 | $argv['comment_out'] = ('false' === ($argv['comment_out'] ?? false)) ? false : true;
|
37 |
| -$classes = get_declared_classes(); |
38 |
| -$isIncluded = []; |
39 |
| - |
40 |
| -/** |
41 |
| - * @var array Set config |
42 |
| - */ |
43 |
| -Used::setConfig(['comment_out_existing' => $argv['comment_out'] ? true : false]); |
44 |
| - |
45 |
| -/** |
46 |
| - * @var mixed Search path to "/vendor/autoload.php" in the file environment iteratively. Sets "/vendor/autoload.php" as first file to include |
47 |
| - */ |
48 |
| -if ($argv['file'] AND $getAutoloader = Used::getAutoloadPath($argv['file'])) |
49 |
| - $argv['include'] = "{$getAutoloader},{$argv['include']},{$argv['file']}"; |
50 | 25 |
|
51 | 26 | /**
|
52 |
| - * @var mixed Set all files to include |
| 27 | + * @var array check if shell options are called and execute if ['-c', ...] |
53 | 28 | */
|
54 |
| -if ($argv['include'] ?? null) |
55 |
| - $argv['include'] = trim($argv['include'], ',') . ",{$argv['file']}"; |
56 |
| -else $argv['include'] = $argv['file']; |
57 |
| - |
58 |
| -/** |
59 |
| - * @var mixed include all |
60 |
| - */ |
61 |
| -if ($includeAll = ($argv['include'] ?? null)) |
62 |
| - foreach(explode(',', $includeAll) as $inc) |
63 |
| - if ($inc AND !in_array($inc, $isIncluded) AND is_file($inc) AND $isIncluded[] = $inc) |
64 |
| - include_once $inc; |
65 |
| - |
66 |
| -/** |
67 |
| - * @var mixed check included files |
68 |
| - */ |
69 |
| -if ($isIncluded AND $diff = array_diff(get_declared_classes(), $classes)) |
70 |
| - $class = end($diff); |
71 |
| -else $diff = false; |
72 |
| - |
73 |
| -/** |
74 |
| - * @var string get Used-Statements for a specific class through the "class"-parameter |
75 |
| - */ |
76 |
| -$class = $argv['class'] ?? $class ?? null; |
77 |
| - |
78 |
| -/** |
79 |
| - * @var mixed Set the right class to load, if nothing is specified |
80 |
| - */ |
81 |
| -if ($diff AND !($argv['class'] ?? null)) { |
82 |
| - foreach($diff as $id => $namespace) { |
83 |
| - $namespace = str_replace('\\', '/', $namespace); |
84 |
| - if (str_contains($argv['file'], $namespace)) { |
85 |
| - $class = $diff[$id]; |
86 |
| - break; |
87 |
| - } |
88 |
| - } |
89 |
| -} |
| 29 | +foreach($getCnf['config']['options'] as $key => $shortOpt) |
| 30 | + if (isset($argv[$shortOpt]) AND isset($getCnf[$key])) |
| 31 | + exit(json_encode([$key => $getCnf[$key]], JSON_PRETTY_PRINT) . PHP_EOL); |
90 | 32 |
|
91 | 33 | /**
|
92 |
| - * @var mixed Fix class if the given class still don't match with available |
| 34 | + * @var string get use Keywords |
93 | 35 | */
|
94 |
| -if ($diff AND !in_array($class, $diff)) { |
95 |
| - foreach($diff as $id => $namespace) { |
96 |
| - $namespace = str_replace('\\', '/', $namespace); |
97 |
| - if ($checkIterate = Used::searchPathLeftToRight($argv['file'], $namespace, '/')) { |
98 |
| - $class = str_replace('/', '\\', $checkIterate); |
99 |
| - break; |
100 |
| - } |
101 |
| - } |
102 |
| -} |
| 36 | +$getUsed = (new Used)->get($argv['file'], $argv); |
103 | 37 |
|
104 | 38 | /**
|
105 |
| - * @var string print Used-Statements or Error |
| 39 | + * @var string response |
106 | 40 | */
|
107 |
| -if ($class) { |
108 |
| - if ($getUsed = ((new Used)->getClassUseList($class))) { |
109 |
| - $r = [ |
110 |
| - 'file' => $getUsed['filename'] ?? $argv['file'] ?? null, |
111 |
| - 'use_for_class' => $class, |
112 |
| - 'start' => $_SERVER['REQUEST_TIME_FLOAT'] ?? null, |
113 |
| - 'end' => microtime(true), |
114 |
| - 'print' => $getUsed['print'] ?? null, |
115 |
| - ]; |
116 |
| - if ('json' === ($argv['return'] ?? null)) { |
117 |
| - $r = json_encode(array_merge($r, ['included' => $diff, 'response' => $getUsed,]), JSON_PRETTY_PRINT); |
118 |
| - } else { |
119 |
| - $rPrint = []; |
120 |
| - foreach($r as $k => $v) |
| 41 | +if ($argv['file'] AND $getUsed) { |
| 42 | + $r = [ |
| 43 | + 'file' => trim($argv['file']), |
| 44 | + 'start' => $_SERVER['REQUEST_TIME_FLOAT'] ?? null, |
| 45 | + 'end' => microtime(true), |
| 46 | + 'print' => $getUsed['print'] ?? null, |
| 47 | + ]; |
| 48 | + if ('json' === ($argv['return'] ?? null)) { |
| 49 | + $r = json_encode($r, JSON_PRETTY_PRINT); |
| 50 | + } else { |
| 51 | + $rPrint = []; |
| 52 | + foreach($r as $k => $v) |
| 53 | + if (!in_array($k, ['class', 'function', 'constant', 'file_content'])) |
121 | 54 | $rPrint[] = 'print' === $k ? "\n{$v}" : "// {$k} = {$v}";
|
122 |
| - $r = PHP_EOL . implode(PHP_EOL, $rPrint) . PHP_EOL . PHP_EOL; |
123 |
| - } |
124 |
| - exit($r); |
125 |
| - } else exit("Error processing the Class: {$class}"); |
126 |
| -} |
127 |
| - |
128 |
| -// if script somehow ends up here, exit anyway |
129 |
| -exit("No file defined, set ?file=/path/to/file.php\n"); |
| 55 | + $r = sprintf('%1$s%2$s%1$s%1$s', PHP_EOL, implode(PHP_EOL, $rPrint)); |
| 56 | + } |
| 57 | + exit($r); |
| 58 | +} else exit(sprintf('Error processing the file: %s', $argv['file'])); |
0 commit comments