-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandRunner.php
More file actions
73 lines (58 loc) · 2.37 KB
/
Copy pathCommandRunner.php
File metadata and controls
73 lines (58 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
namespace TypePHP\Command;
final class CommandRunner
{
private const KNOWN_COMMANDS = [
'config:init',
'cache:clear',
'cache:warm',
'cache:rebuild',
'help',
];
/**
* Parses CLI arguments and routes execution to the corresponding command class.
*
* @param array<int, string> $args
* @param resource $outputStream
* @param resource $errorStream
*/
public static function run(array $args, $outputStream = STDOUT, $errorStream = STDERR): int
{
$c = [CliFormatter::class, 'color'];
$showHelp = \in_array('help', $args, true)
|| \in_array('typephp:help', $args, true)
|| \in_array('--help', $args, true)
|| \in_array('-h', $args, true)
|| $args === [];
if ($showHelp) {
return (new HelpCommand())->execute($args, $outputStream, $errorStream);
}
$firstArg = $args[0] ?? '';
if ($firstArg === 'config:init' || $firstArg === 'init') {
return (new ConfigInitCommand())->execute($args, $outputStream, $errorStream);
}
if ($firstArg === 'cache:rebuild') {
return (new CacheRebuildCommand())->execute($args, $outputStream, $errorStream);
}
if ($firstArg === 'cache:clear') {
return (new CacheClearCommand())->execute($args, $outputStream, $errorStream);
}
if ($firstArg === 'cache:warm') {
return (new CacheWarmCommand())->execute($args, $outputStream, $errorStream);
}
$hasFileExtension = str_contains(basename($firstArg), '.');
$isFileTarget = file_exists($firstArg) || $hasFileExtension;
if (! $isFileTarget) {
fwrite($errorStream, "\n " . $c(' TYPEPHP ', 'badge_red') . ' ' . $c('Error', 'bold') . "\n\n");
fwrite($errorStream, ' ' . $c('✗', 'red') . ' Command ' . $c('"' . $firstArg . '"', 'bold') . " is not defined.\n\n");
fwrite($errorStream, ' ' . $c('Did you mean one of these?', 'yellow') . "\n");
foreach (self::KNOWN_COMMANDS as $cmd) {
fwrite($errorStream, ' ' . $c('•', 'cyan') . ' ' . $cmd . "\n");
}
fwrite($errorStream, "\n");
return 1;
}
return (new RunCommand())->execute($args, $outputStream, $errorStream);
}
}