Total Complexity | 7 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | abstract class BaseCliApplication |
||
8 | { |
||
9 | /** @var string */ |
||
10 | private $command; |
||
11 | |||
12 | /** @var string[] */ |
||
13 | private $arguments; |
||
14 | |||
15 | abstract public function printHelp(): void; |
||
16 | |||
17 | abstract public function execute(): int; |
||
18 | |||
19 | final public function __construct(string $command, string ...$arguments) |
||
20 | { |
||
21 | $this->command = $command; |
||
22 | $this->arguments = $arguments; |
||
23 | } |
||
24 | |||
25 | public function __invoke(): int |
||
26 | { |
||
27 | if ([] !== array_intersect(['-h', '--help'], $this->arguments)) { |
||
28 | $this->printHelp(); |
||
29 | return 0; |
||
30 | } |
||
31 | |||
32 | try { |
||
33 | return $this->execute(); |
||
34 | } catch (\Throwable $exception) { |
||
35 | file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL, FILE_APPEND); |
||
36 | // file_put_contents('php://stderr', get_class($exception) . ': ' . $exception->getMessage() . PHP_EOL, FILE_APPEND); |
||
37 | // file_put_contents('php://stderr', $exception->getTraceAsString() . PHP_EOL, FILE_APPEND); |
||
38 | return min(1, $exception->getCode()); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | public function getCommand(): string |
||
43 | { |
||
44 | return $this->command; |
||
45 | } |
||
46 | |||
47 | /** @return string[] */ |
||
48 | public function getArguments(): array |
||
49 | { |
||
50 | return $this->arguments; |
||
51 | } |
||
52 | |||
53 | public function getArgument(int $index): string |
||
56 | } |
||
57 | } |
||
58 |