1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Zalas\Toolbox\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
10
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
11
|
|
|
use Zalas\Toolbox\Tool\Filter; |
12
|
|
|
use Zalas\Toolbox\Tool\Tool; |
13
|
|
|
use Zalas\Toolbox\UseCase\ListTools; |
14
|
|
|
|
15
|
|
|
final class ListCommand extends Command |
16
|
|
|
{ |
17
|
|
|
use DefaultTag; |
18
|
|
|
|
19
|
|
|
public const NAME = 'list-tools'; |
20
|
|
|
|
21
|
|
|
private ListTools $listTools; |
22
|
|
|
|
23
|
9 |
|
public function __construct(ListTools $listTools) |
24
|
|
|
{ |
25
|
9 |
|
parent::__construct(self::NAME); |
26
|
|
|
|
27
|
9 |
|
$this->listTools = $listTools; |
28
|
|
|
} |
29
|
|
|
|
30
|
9 |
|
protected function configure(): void |
31
|
|
|
{ |
32
|
9 |
|
$this->setDescription('Lists available tools'); |
33
|
9 |
|
$this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude', $this->defaultExcludeTag()); |
34
|
9 |
|
$this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by', $this->defaultTag()); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
38
|
|
|
{ |
39
|
3 |
|
$tools = \call_user_func($this->listTools, new Filter($input->getOption('exclude-tag'), $input->getOption('tag'))); |
40
|
|
|
|
41
|
3 |
|
$style = $this->createStyle($input, $output); |
42
|
3 |
|
$style->title('Available tools'); |
43
|
3 |
|
$style->table( |
44
|
3 |
|
['Name', 'Summary'], |
45
|
3 |
|
$tools->map(function (Tool $tool) { |
46
|
3 |
|
return [\sprintf('<info>%s</info>', $tool->name()), $tool->summary().PHP_EOL.$tool->website().PHP_EOL]; |
47
|
3 |
|
})->toArray() |
48
|
3 |
|
); |
49
|
|
|
|
50
|
3 |
|
return 0; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
private function createStyle(InputInterface $input, OutputInterface $output): StyleInterface |
54
|
|
|
{ |
55
|
3 |
|
return new SymfonyStyle($input, $output); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|