1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Command; |
4
|
|
|
|
5
|
|
|
use DI\Container; |
6
|
|
|
use Padawan\Framework\Application\Socket; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Padawan\Framework\Application\Socket\SocketOutput; |
11
|
|
|
use Symfony\Component\Console\Exception\ExceptionInterface; |
12
|
|
|
|
13
|
|
|
abstract class AsyncCommand extends Command |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return \Generator |
18
|
|
|
*/ |
19
|
|
|
public function run(InputInterface $input, OutputInterface $output) |
20
|
|
|
{ |
21
|
|
|
// force the creation of the synopsis before the merge with the app definition |
22
|
|
|
$this->getSynopsis(true); |
23
|
|
|
$this->getSynopsis(false); |
24
|
|
|
|
25
|
|
|
// add the application arguments and options |
26
|
|
|
$this->mergeApplicationDefinition(); |
27
|
|
|
|
28
|
|
|
// bind the input against the command specific arguments/options |
29
|
|
|
try { |
30
|
|
|
$input->bind($this->getDefinition()); |
31
|
|
|
} catch (ExceptionInterface $e) { |
32
|
|
|
if (!$this->ignoreValidationErrors()) { |
33
|
|
|
throw $e; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->initialize($input, $output); |
38
|
|
|
|
39
|
|
|
// The command name argument is often omitted when a command is executed directly with its run() method. |
40
|
|
|
// It would fail the validation if we didn't make sure the command argument is present, |
41
|
|
|
// since it's required by the application. |
42
|
|
|
if ($input->hasArgument('command') && null === $input->getArgument('command')) { |
43
|
|
|
$input->setArgument('command', $this->getName()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$input->validate(); |
47
|
|
|
|
48
|
|
|
return $this->execute($input, $output); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
|
|
if ($output instanceof SocketOutput) { |
54
|
|
|
return $this->executeAsync($input, $output); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return \Generator |
60
|
|
|
*/ |
61
|
|
|
abstract protected function executeAsync(InputInterface $input, SocketOutput $output); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Container |
65
|
|
|
*/ |
66
|
|
|
public function getContainer() |
67
|
|
|
{ |
68
|
|
|
return $this->getApplication()->getContainer(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Socket |
73
|
|
|
*/ |
74
|
|
|
public function getApplication() |
75
|
|
|
{ |
76
|
|
|
return parent::getApplication(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|