-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathRoutesCommand.php
162 lines (130 loc) · 4.57 KB
/
RoutesCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Enqueue\Symfony\Client;
use Enqueue\Client\DriverInterface;
use Enqueue\Client\Route;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand('enqueue:routes')]
class RoutesCommand extends Command
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @var string
*/
private $defaultClient;
/**
* @var string
*/
private $driverIdPatter;
/**
* @var DriverInterface
*/
private $driver;
public function __construct(ContainerInterface $container, string $defaultClient, string $driverIdPatter = 'enqueue.client.%s.driver')
{
$this->container = $container;
$this->defaultClient = $defaultClient;
$this->driverIdPatter = $driverIdPatter;
parent::__construct();
}
protected function configure(): void
{
$this
->setAliases(['debug:enqueue:routes'])
->setDescription('A command lists all registered routes.')
->addOption('show-route-options', null, InputOption::VALUE_NONE, 'Adds ability to hide options.')
->addOption('client', 'c', InputOption::VALUE_OPTIONAL, 'The client to consume messages from.', $this->defaultClient)
;
$this->driver = null;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$this->driver = $this->getDriver($input->getOption('client'));
} catch (NotFoundExceptionInterface $e) {
throw new \LogicException(sprintf('Client "%s" is not supported.', $input->getOption('client')), 0, $e);
}
$routes = $this->driver->getRouteCollection()->all();
$output->writeln(sprintf('Found %s routes', count($routes)));
$output->writeln('');
if ($routes) {
$table = new Table($output);
$table->setHeaders(['Type', 'Source', 'Queue', 'Processor', 'Options']);
$firstRow = true;
foreach ($routes as $route) {
if (false == $firstRow) {
$table->addRow(new TableSeparator());
$firstRow = false;
}
if ($route->isCommand()) {
continue;
}
$table->addRow([
$this->formatSourceType($route),
$route->getSource(),
$this->formatQueue($route),
$this->formatProcessor($route),
$input->getOption('show-route-options') ? $this->formatOptions($route) : '(hidden)',
]);
}
foreach ($routes as $route) {
if ($route->isTopic()) {
continue;
}
$table->addRow([
$this->formatSourceType($route),
$route->getSource(),
$this->formatQueue($route),
$this->formatProcessor($route),
$input->getOption('show-route-options') ? $this->formatOptions($route) : '(hidden)',
]);
}
$table->render();
}
return 0;
}
private function formatSourceType(Route $route): string
{
if ($route->isCommand()) {
return 'command';
}
if ($route->isTopic()) {
return 'topic';
}
return 'unknown';
}
private function formatProcessor(Route $route): string
{
if ($route->isProcessorExternal()) {
return 'n\a (external)';
}
$processor = $route->getProcessor();
return $route->isProcessorExclusive() ? $processor.' (exclusive)' : $processor;
}
private function formatQueue(Route $route): string
{
$queue = $route->getQueue() ?: $this->driver->getConfig()->getDefaultQueue();
return $route->isPrefixQueue() ? $queue.' (prefixed)' : $queue.' (as is)';
}
private function formatOptions(Route $route): string
{
return var_export($route->getOptions(), true);
}
private function getDriver(string $client): DriverInterface
{
return $this->container->get(sprintf($this->driverIdPatter, $client));
}
}
function enqueue()
{
}