1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace wapmorgan\UnifiedArchive\Commands; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
10
|
|
|
|
11
|
|
|
class BaseFileCommand extends BaseArchiveCommand |
12
|
|
|
{ |
13
|
|
|
protected function configure() |
14
|
|
|
{ |
15
|
|
|
parent::configure(); |
16
|
|
|
$this |
17
|
|
|
->addArgument('file', InputArgument::OPTIONAL, 'Archive entry') |
18
|
|
|
; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param InputInterface $input |
23
|
|
|
* @param OutputInterface $output |
24
|
|
|
* @return array |
25
|
|
|
* @throws \Exception |
26
|
|
|
*/ |
27
|
|
|
protected function getArchiveAndFile(InputInterface $input, OutputInterface $output) |
28
|
|
|
{ |
29
|
|
|
$archive = $this->getArchive($input, $output); |
30
|
|
|
$file = $input->getArgument('file'); |
31
|
|
|
$files = $archive->getFiles(); |
32
|
|
|
|
33
|
|
|
if (empty($file)) { |
34
|
|
|
/** @var QuestionHelper $helper */ |
35
|
|
|
$helper = $this->getHelper('question'); |
36
|
|
|
|
37
|
|
|
$question = new ChoiceQuestion('Which file', $files); |
38
|
|
|
$file = $helper->ask($input, $output, $question); |
39
|
|
|
} else if (!in_array($file, $files, true)) { |
40
|
|
|
throw new \InvalidArgumentException('File "' . $file . '" not found in archive'); |
41
|
|
|
} |
42
|
|
|
$output->writeln('<comment>Selecting file ' . $file . '</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE); |
43
|
|
|
|
44
|
|
|
return [$archive, $file]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|