|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Forrest\CliCommand\Search; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Forrest\Command\Command; |
|
6
|
|
|
use Startwind\Forrest\Command\Parameters\FileParameter; |
|
7
|
|
|
use Startwind\Forrest\Output\OutputHelper; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
14
|
|
|
|
|
15
|
|
|
class FileCommand extends SearchCommand |
|
16
|
|
|
{ |
|
17
|
|
|
public const COMMAND_NAME = 'search:file'; |
|
18
|
|
|
|
|
19
|
|
|
protected static $defaultName = self::COMMAND_NAME; |
|
20
|
|
|
protected static $defaultDescription = 'Search for commands that fit the given file.'; |
|
21
|
|
|
|
|
22
|
|
|
protected function configure(): void |
|
23
|
|
|
{ |
|
24
|
|
|
parent::configure(); |
|
25
|
|
|
|
|
26
|
|
|
$this->addArgument('filename', InputArgument::REQUIRED, 'The filename you want to get commands for.'); |
|
27
|
|
|
$this->addArgument('pattern', InputArgument::OPTIONAL, 'Filter the results for a given pattern.'); |
|
28
|
|
|
|
|
29
|
|
|
$this->addOption('force', null, InputOption::VALUE_NONE, 'Run the command without asking for permission.'); |
|
30
|
|
|
|
|
31
|
|
|
$this->setAliases(['file']); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function doExecute(InputInterface $input, OutputInterface $output): int |
|
35
|
|
|
{ |
|
36
|
|
|
OutputHelper::renderHeader($output); |
|
37
|
|
|
|
|
38
|
|
|
$this->enrichRepositories(); |
|
39
|
|
|
|
|
40
|
|
|
$filename = $input->getArgument('filename'); |
|
41
|
|
|
$pattern = $input->getArgument('pattern'); |
|
42
|
|
|
|
|
43
|
|
|
/** @var QuestionHelper $questionHelper */ |
|
44
|
|
|
$questionHelper = $this->getHelper('question'); |
|
45
|
|
|
|
|
46
|
|
|
if (!file_exists($filename)) { |
|
47
|
|
|
$this->renderErrorBox('File not found.'); |
|
48
|
|
|
return SymfonyCommand::FAILURE; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$filenames = [ |
|
52
|
|
|
basename($filename), |
|
53
|
|
|
pathinfo($filename, PATHINFO_EXTENSION) |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
if (is_dir($filename)) { |
|
57
|
|
|
$filenames[] = FileParameter::DIRECTORY; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$fileCommands = $this->getRepositoryCollection()->searchByFile($filenames); |
|
61
|
|
|
|
|
62
|
|
|
if ($pattern) { |
|
63
|
|
|
foreach ($fileCommands as $key => $fileCommand) { |
|
64
|
|
|
if (str_contains(strtolower($fileCommand->getName()), strtolower($pattern))) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
if (str_contains(strtolower($fileCommand->getDescription()), strtolower($pattern))) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
unset($fileCommands[$key]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->renderInfoBox('This is a list of commands that are applicable to the given file or file type.'); |
|
75
|
|
|
|
|
76
|
|
|
if (empty($fileCommands)) { |
|
77
|
|
|
$this->renderErrorBox('No commands found that match this file type.'); |
|
78
|
|
|
return SymfonyCommand::FAILURE; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$command = OutputHelper::renderCommands( |
|
82
|
|
|
$output, |
|
83
|
|
|
$input, |
|
84
|
|
|
$questionHelper, |
|
85
|
|
|
$fileCommands, |
|
86
|
|
|
null, |
|
87
|
|
|
-1, |
|
88
|
|
|
true |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
if ($command === false) { |
|
|
|
|
|
|
92
|
|
|
return SymfonyCommand::FAILURE; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$output->writeln(''); |
|
96
|
|
|
|
|
97
|
|
|
$values = [$this->getParameterIdentifier($command, $filenames) => $filename]; |
|
98
|
|
|
|
|
99
|
|
|
return $this->runCommand($command, $values); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Return the identifier of the parameter that fits the filename. |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getParameterIdentifier(Command $command, array $filenames): string |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($command->getParameters() as $identifier => $parameter) { |
|
108
|
|
|
if ($parameter instanceof FileParameter) { |
|
109
|
|
|
if ($parameter->isCompatibleWithFiles($filenames)) { |
|
110
|
|
|
return $identifier; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
throw new \RuntimeException('No parameter found that excepts the given file name.'); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|