Skip to content

Commit 7c91358

Browse files
committed
Migrated from laminas-console to laminas-cli.
Signed-off-by: Illia Hapak <illyagapak@gmail.com>
1 parent b33528c commit 7c91358

34 files changed

Lines changed: 866 additions & 921 deletions

config/application.config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'modules' => [
5+
'RabbitMqModule',
6+
],
7+
'module_listener_options' => [],
8+
];

config/module.config.php

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
namespace RabbitMqModule;
44

5+
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
6+
use RabbitMqModule\Command;
7+
58
return [
9+
ConfigAbstractFactory::class => [
10+
Command\ListConsumersCommand::class => [
11+
'config',
12+
],
13+
],
614
'rabbitmq' => [
715
'connection' => [
816
'default' => []
@@ -19,69 +27,6 @@
1927
'rpc_server' => 'RabbitMqModule\\Service\\RpcServerFactory',
2028
'rpc_client' => 'RabbitMqModule\\Service\\RpcClientFactory'
2129
],
22-
'console' => [
23-
'router' => [
24-
'routes' => [
25-
'rabbitmq_module-setup-fabric' => [
26-
'options' => [
27-
'route' => 'rabbitmq setup-fabric',
28-
'defaults' => [
29-
'controller' => 'RabbitMqModule\\Controller\\SetupFabricController',
30-
'action' => 'index'
31-
]
32-
]
33-
],
34-
'rabbitmq_module-list_consumers' => [
35-
'options' => [
36-
'route' => 'rabbitmq list consumers',
37-
'defaults' => [
38-
'controller' => 'RabbitMqModule\\Controller\\ConsumerController',
39-
'action' => 'list'
40-
]
41-
]
42-
],
43-
'rabbitmq_module-consumer' => [
44-
'options' => [
45-
'route' => 'rabbitmq consumer <name> [--without-signals|-w]',
46-
'defaults' => [
47-
'controller' => 'RabbitMqModule\\Controller\\ConsumerController',
48-
'action' => 'index'
49-
]
50-
]
51-
],
52-
'rabbitmq_module-rpc_server' => [
53-
'options' => [
54-
'route' => 'rabbitmq rpc_server <name> [--without-signals|-w]',
55-
'defaults' => [
56-
'controller' => 'RabbitMqModule\\Controller\\RpcServerController',
57-
'action' => 'index'
58-
]
59-
]
60-
],
61-
'rabbitmq_module-stdin-producer' => [
62-
'options' => [
63-
'route' => 'rabbitmq stdin-producer <name> [--route=] <msg>',
64-
'defaults' => [
65-
'controller' => 'RabbitMqModule\\Controller\\StdInProducerController',
66-
'action' => 'index'
67-
]
68-
]
69-
]
70-
]
71-
]
72-
],
73-
'controllers' => [
74-
'factories' => [
75-
'RabbitMqModule\\Controller\\SetupFabricController' =>
76-
'RabbitMqModule\\Controller\\Factory\\SetupFabricControllerFactory',
77-
'RabbitMqModule\\Controller\\ConsumerController' =>
78-
'RabbitMqModule\\Controller\\Factory\\ConsumerControllerFactory',
79-
'RabbitMqModule\\Controller\\RpcServerController' =>
80-
'RabbitMqModule\\Controller\\Factory\\RpcServerControllerFactory',
81-
'RabbitMqModule\\Controller\\StdInProducerController' =>
82-
'RabbitMqModule\\Controller\\Factory\\StdInProducerControllerFactory'
83-
],
84-
],
8530
'service_manager' => [
8631
'invokables' => [
8732
'RabbitMqModule\\Service\\RabbitMqService' => 'RabbitMqModule\\Service\\RabbitMqService',
@@ -96,6 +41,22 @@
9641
],
9742
'abstract_factories' => [
9843
'RabbitMqModule\\Service\\AbstractServiceFactory' => 'RabbitMqModule\\Service\\AbstractServiceFactory'
44+
],
45+
'factories' => [
46+
Command\ListConsumersCommand::class => ConfigAbstractFactory::class,
47+
Command\StartConsumerCommand::class => Command\Factory\ContainerAwareCommandFactory::class,
48+
Command\StartRpcServerCommand::class => Command\Factory\ContainerAwareCommandFactory::class,
49+
Command\PublishMessageCommand::class => Command\Factory\ContainerAwareCommandFactory::class,
50+
Command\SetupFabricCommand::class => Command\Factory\ContainerAwareCommandFactory::class,
51+
],
52+
],
53+
'laminas-cli' => [
54+
'commands' => [
55+
Command\ListConsumersCommand::getDefaultName() => Command\ListConsumersCommand::class,
56+
Command\StartConsumerCommand::getDefaultName() => Command\StartConsumerCommand::class,
57+
Command\StartRpcServerCommand::getDefaultName() => Command\StartRpcServerCommand::class,
58+
Command\PublishMessageCommand::getDefaultName() => Command\PublishMessageCommand::class,
59+
Command\SetupFabricCommand::getDefaultName() => Command\SetupFabricCommand::class,
9960
]
10061
]
10162
];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RabbitMqModule\Command;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
abstract class ContainerAwareCommand extends Command
9+
{
10+
/** @var ContainerInterface */
11+
protected $container;
12+
13+
final public function __construct(ContainerInterface $container)
14+
{
15+
$this->container = $container;
16+
parent::__construct();
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace RabbitMqModule\Command\Factory;
4+
5+
use Interop\Container\ContainerInterface;
6+
use InvalidArgumentException;
7+
use Laminas\ServiceManager\Factory\FactoryInterface;
8+
use RabbitMqModule\Command\ContainerAwareCommand;
9+
10+
class ContainerAwareCommandFactory implements FactoryInterface
11+
{
12+
/**
13+
* @inheritDoc
14+
*
15+
* @param array<mixed>|null $options
16+
*/
17+
public function __invoke(
18+
ContainerInterface $container,
19+
$requestedName,
20+
?array $options = null
21+
): ContainerAwareCommand {
22+
if (! is_subclass_of($requestedName, ContainerAwareCommand::class)) {
23+
throw new InvalidArgumentException(
24+
sprintf(
25+
"The '%s' must be an extension of %s",
26+
$requestedName,
27+
ContainerAwareCommand::class
28+
)
29+
);
30+
}
31+
32+
return new $requestedName($container);
33+
}
34+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace RabbitMqModule\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class ListConsumersCommand extends Command
10+
{
11+
/** @var string */
12+
protected static $defaultName = 'rabbitmq:consumers:list';
13+
14+
/** @var string */
15+
protected static $defaultDescription = 'List available consumers';
16+
17+
/** @var array<string, array<string, mixed>> */
18+
protected $config;
19+
20+
/**
21+
* @param array<string, array<string, mixed>> $config
22+
*/
23+
public function __construct(array $config)
24+
{
25+
$this->config = $config;
26+
parent::__construct();
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output): int
30+
{
31+
if (! array_key_exists('rabbitmq', $this->config) || ! array_key_exists('consumer', $this->config['rabbitmq'])) {
32+
$output->writeln('<error>No "rabbitmq.consumer" configuration key found!</error>');
33+
34+
return Command::FAILURE;
35+
}
36+
37+
$consumers = $this->config['rabbitmq']['consumer'];
38+
39+
if (! is_array($consumers) || count($consumers) === 0) {
40+
$output->writeln('<error>No consumers defined!</error>');
41+
42+
return Command::SUCCESS;
43+
}
44+
45+
foreach ($consumers as $name => $configuration) {
46+
$description = array_key_exists('description', $configuration) ? (string) $configuration['description'] : '';
47+
$output->writeln("- <info>$name</info>: <comment>$description</comment>");
48+
}
49+
50+
return Command::SUCCESS;
51+
}
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace RabbitMqModule\Command;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
use Psr\Container\NotFoundExceptionInterface;
7+
use RabbitMqModule\Producer;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
class PublishMessageCommand extends ContainerAwareCommand
15+
{
16+
/** @var string */
17+
protected static $defaultName = 'rabbitmq:producer:publish';
18+
19+
/** @var string */
20+
protected static $defaultDescription = 'Send a message with a producer';
21+
22+
protected function configure(): void
23+
{
24+
$this
25+
->addArgument('name', InputArgument::REQUIRED, 'The producer name')
26+
->addArgument('msg', InputArgument::REQUIRED, 'Message to publish')
27+
->addOption(
28+
'route',
29+
'r',
30+
InputOption::VALUE_REQUIRED,
31+
'The routing key',
32+
''
33+
);
34+
}
35+
36+
/**
37+
* @throws ContainerExceptionInterface
38+
* @throws NotFoundExceptionInterface
39+
*/
40+
protected function execute(InputInterface $input, OutputInterface $output): int
41+
{
42+
/** @var string $producerName */
43+
$producerName = $input->getArgument('name');
44+
/** @var string $msg */
45+
$msg = $input->getArgument('msg');
46+
/** @var string $route */
47+
$route = $input->getOption('route');
48+
49+
$serviceName = "rabbitmq.producer.$producerName";
50+
51+
if (! $this->container->has($serviceName)) {
52+
$output->writeln("<error>No producer with name \"$producerName\" found</error>");
53+
54+
return Command::FAILURE;
55+
}
56+
57+
/** @var Producer $producer */
58+
$producer = $this->container->get($serviceName);
59+
$producer->publish($msg, $route);
60+
61+
return Command::SUCCESS;
62+
}
63+
}

src/Command/SetupFabricCommand.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace RabbitMqModule\Command;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
use Psr\Container\NotFoundExceptionInterface;
7+
use RabbitMqModule\Service\SetupFabricAwareInterface;
8+
use RuntimeException;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Throwable;
13+
14+
class SetupFabricCommand extends ContainerAwareCommand
15+
{
16+
/** @var string */
17+
protected static $defaultName = 'rabbitmq:fabric:setup';
18+
19+
/** @var string */
20+
protected static $defaultDescription = 'Sets up the Rabbit MQ fabric';
21+
22+
protected function execute(InputInterface $input, OutputInterface $output): int
23+
{
24+
$output->writeln('<info>Setting up the AMQP fabric</info>');
25+
26+
try {
27+
$services = $this->getServiceParts();
28+
29+
foreach ($services as $service) {
30+
if (! $service instanceof SetupFabricAwareInterface) {
31+
continue;
32+
}
33+
$service->setupFabric();
34+
}
35+
} catch (Throwable $e) {
36+
$output->writeln("<error>Exception: {$e->getMessage()}</error>");
37+
38+
return Command::FAILURE;
39+
}
40+
41+
return Command::SUCCESS;
42+
}
43+
44+
/**
45+
* @throws ContainerExceptionInterface
46+
* @throws NotFoundExceptionInterface
47+
*
48+
* @return array<int, mixed>
49+
*/
50+
protected function getServiceParts(): array
51+
{
52+
/** @var array<string, array<string, array<mixed>>> $config */
53+
$config = $this->container->get('config');
54+
$serviceKeys = [
55+
'consumer',
56+
'producer',
57+
'rpc_client',
58+
'rpc_server',
59+
];
60+
$parts = [];
61+
foreach ($serviceKeys as $serviceKey) {
62+
if (! isset($config['rabbitmq'][$serviceKey])) {
63+
throw new RuntimeException(
64+
sprintf('No service "rabbitmq.%s" found in configuration', $serviceKey)
65+
);
66+
}
67+
68+
$keys = array_keys($config['rabbitmq'][$serviceKey]);
69+
foreach ($keys as $key) {
70+
$parts[] = $this->container->get(sprintf('rabbitmq.%s.%s', $serviceKey, $key));
71+
}
72+
}
73+
74+
return $parts;
75+
}
76+
}

0 commit comments

Comments
 (0)