1
|
|
|
<?php |
2
|
|
|
namespace PhpDDD\PhpDDDBundle\Command; |
3
|
|
|
|
4
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
5
|
|
|
use Symfony\Component\Console\Helper\Table; |
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\DependencyInjection\Exception\ServiceNotFoundException; |
10
|
|
|
|
11
|
|
|
abstract class AbstractBusDebugCommand extends ContainerAwareCommand |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string[] |
16
|
|
|
*/ |
17
|
|
|
private $tableLabels; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $commandName |
21
|
|
|
* @param string $defaultName |
22
|
|
|
* @param string[] $tableHeaderLabel |
23
|
|
|
*/ |
24
|
|
|
protected function configureCommand($commandName, $defaultName, array $tableHeaderLabel) |
25
|
|
|
{ |
26
|
|
|
$this |
27
|
|
|
->setName($commandName) |
28
|
|
|
->addArgument('name', InputArgument::OPTIONAL, 'Name of the bus service', $defaultName) |
29
|
|
|
->setDescription('List every listeners associated to the bus'); |
30
|
|
|
$this->tableLabels = $tableHeaderLabel; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param InputInterface $input |
35
|
|
|
* @param OutputInterface $output |
36
|
|
|
* |
37
|
|
|
* @return int|null|void |
38
|
|
|
*/ |
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$busName = $input->getArgument('name'); |
42
|
|
|
|
43
|
|
|
$table = new Table($output); |
44
|
|
|
$table->setHeaders($this->tableLabels); |
45
|
|
|
|
46
|
|
|
$this->addRowsToTable($table, $this->getBus($busName)); |
47
|
|
|
|
48
|
|
|
$table->render(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $busName |
53
|
|
|
* |
54
|
|
|
* @return object |
55
|
|
|
*/ |
56
|
|
|
protected function getBus($busName) |
57
|
|
|
{ |
58
|
|
|
if (!$this->getContainer()->has($busName)) { |
59
|
|
|
throw new ServiceNotFoundException($busName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->getContainer()->get($busName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Table $table |
67
|
|
|
* @param object $bus |
68
|
|
|
*/ |
69
|
|
|
abstract protected function addRowsToTable(Table $table, $bus); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.