-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathSetupBrokerCommand.php
70 lines (57 loc) · 1.99 KB
/
SetupBrokerCommand.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
<?php
namespace Enqueue\Symfony\Client;
use Enqueue\Client\DriverInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand('enqueue:setup-broker')]
class SetupBrokerCommand extends Command
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @var string
*/
private $defaultClient;
/**
* @var string
*/
private $driverIdPattern;
public function __construct(ContainerInterface $container, string $defaultClient, string $driverIdPattern = 'enqueue.client.%s.driver')
{
$this->container = $container;
$this->defaultClient = $defaultClient;
$this->driverIdPattern = $driverIdPattern;
parent::__construct();
}
protected function configure(): void
{
$this
->setAliases(['enq:sb'])
->setDescription('Setup broker. Configure the broker, creates queues, topics and so on.')
->addOption('client', 'c', InputOption::VALUE_OPTIONAL, 'The client to consume messages from.', $this->defaultClient)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$client = $input->getOption('client');
try {
$this->getDriver($client)->setupBroker(new ConsoleLogger($output));
} catch (NotFoundExceptionInterface $e) {
throw new \LogicException(sprintf('Client "%s" is not supported.', $client), 0, $e);
}
$output->writeln('Broker set up');
return 0;
}
private function getDriver(string $client): DriverInterface
{
return $this->container->get(sprintf($this->driverIdPattern, $client));
}
}