|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Console\Symfony; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Wandu\Console\Command; |
|
10
|
|
|
use Wandu\Console\Exception\ConsoleException; |
|
11
|
|
|
|
|
12
|
|
|
class CommandAdapter extends SymfonyCommand |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \Wandu\Console\Command */ |
|
15
|
|
|
protected $command; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param \Wandu\Console\Command $command |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(Command $command) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::__construct('_'); |
|
23
|
|
|
$this->command = $command; |
|
24
|
|
|
|
|
25
|
|
|
$this->setDescription($command->getDescription()); |
|
26
|
|
|
$this->setHelp($command->getHelp()); |
|
27
|
|
|
|
|
28
|
|
|
// arguments |
|
29
|
|
|
foreach ($command->getArguments() as $argument => $description) { |
|
30
|
|
|
if (substr($argument, -1) === '?') { |
|
31
|
|
|
$this->addArgument(substr($argument, 0, -1), InputArgument::OPTIONAL, $description); |
|
32
|
|
|
} elseif (substr($argument, -2) === '[]') { |
|
33
|
|
|
$this->addArgument(substr($argument, 0, -2), InputArgument::IS_ARRAY, $description); |
|
34
|
|
|
} else { |
|
35
|
|
|
$this->addArgument($argument, InputArgument::REQUIRED, $description); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
foreach ($command->getOptions() as $option => $description) { |
|
40
|
|
|
if (substr($option, -1) === '?') { |
|
41
|
|
|
$this->addOption(substr($option, 0, -1), null, InputOption::VALUE_OPTIONAL, $description); |
|
42
|
|
|
} elseif (substr($option, -2) === '[]') { |
|
43
|
|
|
$this->addOption(substr($option, 0, -2), null, InputOption::VALUE_IS_ARRAY, $description); |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->addOption($option, null, InputOption::VALUE_REQUIRED, $description); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
52
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
53
|
|
|
* @return int |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
|
|
return $this->command->withIO($input, $output)->execute(); |
|
59
|
|
|
} catch (ConsoleException $e) { |
|
60
|
|
|
$output->writeln($e->getMessage()); |
|
61
|
|
|
return -1; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|