1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\SatCatalogosPopulate\Commands; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
class CliApplication |
10
|
|
|
{ |
11
|
|
|
/** @var array<string, array{class: class-string, description: string}> */ |
|
|
|
|
12
|
|
|
private array $commands; |
13
|
|
|
|
14
|
|
|
private string $executableName = ''; |
15
|
|
|
|
16
|
|
|
public function getExecutableName(): string |
17
|
|
|
{ |
18
|
|
|
return $this->executableName; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setExecutableName(string $executableName): void |
22
|
|
|
{ |
23
|
|
|
$this->executableName = $executableName; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->commands = [ |
29
|
|
|
'dump-origins' => [ |
30
|
|
|
'class' => DumpOrigins::class, |
31
|
|
|
'description' => 'Hace un volcado del archivo de orígenes esperado', |
32
|
|
|
], |
33
|
|
|
'update-origins' => [ |
34
|
|
|
'class' => UpdateOrigins::class, |
35
|
|
|
'description' => 'Actualiza el archivo de orígenes desde un archivo o directorio', |
36
|
|
|
], |
37
|
|
|
'update-database' => [ |
38
|
|
|
'class' => UpdateDatabase::class, |
39
|
|
|
'description' => 'Actualiza la base de datos de catálogos desde un directorio', |
40
|
|
|
], |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function run(string ...$arguments): int |
45
|
|
|
{ |
46
|
|
|
$this->setExecutableName((string) array_shift($arguments)); |
47
|
|
|
|
48
|
|
|
$command = (string) array_shift($arguments); |
49
|
|
|
|
50
|
|
|
if ('' === $command || 'list' === $command) { |
51
|
|
|
$this->listCommands(); |
52
|
|
|
return 0; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (in_array($command, ['help', '-h', '--help'], true)) { |
56
|
|
|
$this->showHelp($arguments[0] ?? ''); |
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ([] !== array_intersect(['-h', '--help'], $arguments)) { |
61
|
|
|
$this->showHelp($command); |
62
|
|
|
return 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->runCommand($command, ...$arguments); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @return class-string */ |
|
|
|
|
69
|
|
|
public function getCommandClass(string $commandName): string |
70
|
|
|
{ |
71
|
|
|
if (! isset($this->commands[$commandName])) { |
72
|
|
|
throw new RuntimeException("Command $commandName is not registered"); |
73
|
|
|
} |
74
|
|
|
return $this->commands[$commandName]['class']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function runCommand(string $commandName, string ...$arguments): int |
78
|
|
|
{ |
79
|
|
|
$commandClass = $this->getCommandClass($commandName); |
80
|
|
|
/** @phpstan-var callable $staticCallable phpstan work around*/ |
81
|
|
|
$staticCallable = [$commandClass, 'createFromArguments']; |
82
|
|
|
/** @var CommandInterface $command */ |
83
|
|
|
$command = call_user_func($staticCallable, $arguments); |
84
|
|
|
return $command->run(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function listCommands(): void |
88
|
|
|
{ |
89
|
|
|
$commandList = []; |
90
|
|
|
foreach ($this->commands as $name => $commandInfo) { |
91
|
|
|
$commandList[$name] = $commandInfo['description']; |
92
|
|
|
} |
93
|
|
|
$internalCommandList = [ |
94
|
|
|
'list' => 'show the list of commands', |
95
|
|
|
'help' => 'show general help of command help', |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$commandList = array_merge($internalCommandList, array_diff($commandList, $internalCommandList)); |
99
|
|
|
|
100
|
|
|
echo $this->getExecutableName() . ' command options' . PHP_EOL . PHP_EOL; |
101
|
|
|
foreach ($commandList as $name => $description) { |
102
|
|
|
echo ' ' . $name . ': ' . $description . PHP_EOL; |
103
|
|
|
} |
104
|
|
|
echo PHP_EOL; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function showHelp(string $commandName): void |
108
|
|
|
{ |
109
|
|
|
if ('' === $commandName) { |
110
|
|
|
$this->listCommands(); |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$commandClassName = $this->getCommandClass($commandName); |
115
|
|
|
$description = $this->commands[$commandName]['description']; |
116
|
|
|
echo $commandName, ': ', $description, PHP_EOL; |
117
|
|
|
/** @var callable $staticCallable phpstan work around */ |
118
|
|
|
$staticCallable = $commandClassName . '::help'; |
119
|
|
|
/** @var string $helpOutput */ |
120
|
|
|
$helpOutput = call_user_func($staticCallable, $commandName); |
121
|
|
|
echo $helpOutput, PHP_EOL, PHP_EOL; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|