From 7bd3ce8c1b7ae58fa78a7f2dd0e4ff876496542e Mon Sep 17 00:00:00 2001 From: Olivier Balais Date: Tue, 5 Dec 2017 13:08:52 +0100 Subject: [PATCH 1/5] Use new DI and explicitly declare Commands --- Command/AddMessagesCommand.php | 37 ++++------ Command/CreateQueuesCommand.php | 61 +++++++--------- Command/DeleteQueuesCommand.php | 59 +++++++-------- Command/GetMessagesCommand.php | 38 ++++------ Command/ListPrioritiesCommand.php | 39 ++++------ Command/PurgeQueuesCommand.php | 64 +++++++---------- Command/QueuesInfoCommand.php | 26 +++---- DependencyInjection/QueueClientExtension.php | 1 - QueueClientBundle.php | 1 + QueueClientFactory.php | 18 +++-- Resources/config/services.yml | 37 ++++++---- Utils/Output.php | 75 -------------------- composer.json | 12 ++-- 13 files changed, 166 insertions(+), 302 deletions(-) delete mode 100644 Utils/Output.php diff --git a/Command/AddMessagesCommand.php b/Command/AddMessagesCommand.php index fd1f9a8..687e808 100644 --- a/Command/AddMessagesCommand.php +++ b/Command/AddMessagesCommand.php @@ -2,22 +2,24 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; -use Psr\Log\LoggerInterface; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\QueueClient\QueueClientInterface; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; -class AddMessagesCommand extends ContainerAwareCommand +class AddMessagesCommand extends Command { - /** - * @var Output $output - */ - private $output; + /** @var QueueClientInterface */ + private $queueClient; + + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } protected function configure() { @@ -27,7 +29,8 @@ protected function configure() ->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Add in queue with specific priority') ->addArgument('queueName', InputArgument::REQUIRED, 'queue') ->addArgument('messages', InputArgument::IS_ARRAY, 'messages to add') - ->setHelp('This command add messages in queue.'); + ->setHelp('This command add messages in queue.') + ; } /** @@ -37,20 +40,10 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - $priority = null; if ($input->getOption('priority')) { $priority = $input->getOption('priority'); - if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) { + if (!in_array($priority, $this->queueClient->getPriorityHandler()->getAll())) { throw new \InvalidArgumentException('Priority "' . $priority . '" not found.'); } } @@ -58,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $queueName = $input->getArgument('queueName'); $messages = $input->getArgument('messages'); - $queueClient->addMessages($queueName, $messages, $priority); + $this->queueClient->addMessages($queueName, $messages, $priority); return 0; } diff --git a/Command/CreateQueuesCommand.php b/Command/CreateQueuesCommand.php index a6b1ae7..fa2d027 100644 --- a/Command/CreateQueuesCommand.php +++ b/Command/CreateQueuesCommand.php @@ -3,8 +3,6 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; use InvalidArgumentException; -use Psr\Log\LoggerInterface; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration; use ReputationVIP\QueueClient\QueueClientInterface; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; @@ -13,15 +11,19 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\Yaml\Yaml; class CreateQueuesCommand extends ContainerAwareCommand { - /** - * @var Output $output - */ - private $output; + /** @var QueueClientInterface */ + private $queueClient; + + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } protected function configure() { @@ -47,11 +49,13 @@ protected function configure() } /** + * @param OutputInterface $output * @param QueueClientInterface $queueClient * @param string $fileName + * * @return int */ - private function createFromFile($queueClient, $fileName) + private function createFromFile(OutputInterface $output, QueueClientInterface $queueClient, $fileName) { try { $processor = new Processor(); @@ -59,30 +63,30 @@ private function createFromFile($queueClient, $fileName) $processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName))); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::CRITICAL); + $output->writeln($e->getMessage()); return 1; } array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer()); - $this->output->write('Start create queue.', Output::INFO); + $output->writeln('Start create queue.'); foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) { $queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE]; try { $queueClient->createQueue($queueName); - $this->output->write('Queue ' . $queueName . ' created.', Output::INFO); + $output->writeln('Queue ' . $queueName . ' created.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->writeln($e->getMessage()); } foreach ($queue[QueuesConfiguration::QUEUE_ALIASES_NODE] as $alias) { try { $queueClient->addAlias($queueName, $alias); - $this->output->write('Queue alias ' . $alias . ' -> ' . $queueName . ' found.', Output::INFO); + $output->writeln('Queue alias ' . $alias . ' -> ' . $queueName . ' found.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->writeln($e->getMessage()); } } } - $this->output->write('End create queue.', Output::INFO); + $output->writeln('End create queue.'); return 0; } @@ -94,34 +98,19 @@ private function createFromFile($queueClient, $fileName) */ protected function execute(InputInterface $input, OutputInterface $output) { - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); - try { - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - } catch (ServiceNotFoundException $e) { - $this->output->write('No queue client service found.', Output::CRITICAL); - - return 1; - } if ($input->getOption('file')) { $fileName = $input->getOption('file'); - return $this->createFromFile($queueClient, $fileName); + return $this->createFromFile($output, $this->queueClient, $fileName); } else { $queues = $input->getArgument('queues'); if (count($queues)) { foreach ($queues as $queue) { try { - $queueClient->createQueue($queue); - $this->output->write('Queue ' . $queue . ' created.', Output::INFO); + $this->queueClient->createQueue($queue); + $output->writeln('Queue ' . $queue . ' created.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->writeln($e->getMessage()); } } @@ -130,9 +119,9 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $fileName = $this->getContainer()->getParameter('queue_client.queues_file'); - return $this->createFromFile($queueClient, $fileName); + return $this->createFromFile($output, $this->queueClient, $fileName); } catch (InvalidArgumentException $e) { - $this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL); + $output->writeln('No queue_client.queues_file parameter found.'); return 1; } diff --git a/Command/DeleteQueuesCommand.php b/Command/DeleteQueuesCommand.php index 98da8db..fe7d2ad 100644 --- a/Command/DeleteQueuesCommand.php +++ b/Command/DeleteQueuesCommand.php @@ -3,9 +3,7 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; use InvalidArgumentException; -use Psr\Log\LoggerInterface; use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\QueueClient\QueueClientInterface; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Config\Definition\Processor; @@ -14,15 +12,19 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\Yaml\Yaml; class DeleteQueuesCommand extends ContainerAwareCommand { - /** - * @var Output $output - */ - private $output; + /** @var QueueClientInterface */ + private $queueClient; + + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } protected function configure() { @@ -49,11 +51,12 @@ protected function configure() } /** - * @param QueueClientInterface $queueClient + * @param OutputInterface $output * @param string $fileName + * * @return int */ - private function deleteFromFile($queueClient, $fileName) + private function deleteFromFile(OutputInterface $output, $fileName) { try { $processor = new Processor(); @@ -61,22 +64,22 @@ private function deleteFromFile($queueClient, $fileName) $processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName))); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::CRITICAL); + $output->write($e->getMessage()); return 1; } array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer()); - $this->output->write('Start delete queue.', Output::INFO); + $output->write('Start delete queue.'); foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) { $queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE]; try { - $queueClient->deleteQueue($queueName); - $this->output->write('Queue ' . $queueName . ' deleted.', Output::INFO); + $this->queueClient->deleteQueue($queueName); + $output->write('Queue ' . $queueName . ' deleted.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->write($e->getMessage()); } } - $this->output->write('End delete queue.', Output::INFO); + $output->write('End delete queue.'); return 0; } @@ -90,21 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); $force = $input->getOption('force') ? true : false; - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); - try { - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - } catch (ServiceNotFoundException $e) { - $this->output->write('No queue client service found.', Output::CRITICAL); - return 1; - } if ($input->getOption('file')) { $fileName = $input->getOption('file'); if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) { @@ -112,7 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } - return $this->deleteFromFile($queueClient, $fileName); + return $this->deleteFromFile($output, $fileName); } else { $queues = $input->getArgument('queues'); if (count($queues)) { @@ -122,10 +111,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } foreach ($queues as $queue) { try { - $queueClient->deleteQueue($queue); - $this->output->write('Queue ' . $queue . ' deleted.', Output::INFO); + $this->queueClient->deleteQueue($queue); + $output->write('Queue ' . $queue . ' deleted.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->write($e->getMessage()); } } @@ -138,9 +127,9 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } - return $this->deleteFromFile($queueClient, $fileName); + return $this->deleteFromFile($output, $fileName); } catch (InvalidArgumentException $e) { - $this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL); + $output->write('No queue_client.queues_file parameter found.'); return 1; } diff --git a/Command/GetMessagesCommand.php b/Command/GetMessagesCommand.php index 7c8690b..0c9b156 100644 --- a/Command/GetMessagesCommand.php +++ b/Command/GetMessagesCommand.php @@ -2,22 +2,24 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; -use Psr\Log\LoggerInterface; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\QueueClient\QueueClientInterface; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; class GetMessagesCommand extends ContainerAwareCommand { - /** - * @var Output $output - */ - private $output; + /** @var QueueClientInterface */ + private $queueClient; + + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } protected function configure() { @@ -38,31 +40,16 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); - try { - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - } catch (ServiceNotFoundException $e) { - $this->output->write('No queue client service found.', Output::CRITICAL); - - return 1; - } $queueName = $input->getArgument('queueName'); $numberMessages = $input->getOption('number-messages') ?: 1; $priority = null; if ($input->getOption('priority')) { $priority = $input->getOption('priority'); - if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) { + if (!in_array($priority, $this->queueClient->getPriorityHandler()->getAll())) { throw new \InvalidArgumentException('Priority "' . $priority . '" not found.'); } } - $messages = $queueClient->getMessages($queueName, $numberMessages, $priority); + $messages = $this->queueClient->getMessages($queueName, $numberMessages, $priority); foreach ($messages as $message) { if (is_array($message['Body'])) { $output->writeln(json_encode($message['Body'])); @@ -70,8 +57,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln($message['Body']); } } + if ($input->getOption('pop')) { - $queueClient->deleteMessages($queueName, $messages); + $this->queueClient->deleteMessages($queueName, $messages); } return 0; diff --git a/Command/ListPrioritiesCommand.php b/Command/ListPrioritiesCommand.php index cd0aa00..0c7f3df 100644 --- a/Command/ListPrioritiesCommand.php +++ b/Command/ListPrioritiesCommand.php @@ -2,20 +2,22 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; -use Psr\Log\LoggerInterface; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\QueueClient\QueueClientInterface; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; -class ListPrioritiesCommand extends ContainerAwareCommand +class ListPrioritiesCommand extends Command { - /** - * @var Output $output - */ - private $output; + /** @var QueueClientInterface */ + private $queueClient; + + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } protected function configure() { @@ -32,23 +34,8 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); - try { - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - } catch (ServiceNotFoundException $e) { - $this->output->write('No queue client service found.', Output::CRITICAL); - - return 1; - } - foreach ($queueClient->getPriorityHandler()->getAll() as $priority) { - $output->writeln($priority); + foreach ($this->queueClient->getPriorityHandler()->getAll() as $priority) { + $output->writeln($priority->getName()); } return 0; diff --git a/Command/PurgeQueuesCommand.php b/Command/PurgeQueuesCommand.php index 9992e44..d76c385 100644 --- a/Command/PurgeQueuesCommand.php +++ b/Command/PurgeQueuesCommand.php @@ -3,9 +3,7 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; use InvalidArgumentException; -use Psr\Log\LoggerInterface; use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\QueueClient\QueueClientInterface; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Config\Definition\Processor; @@ -14,16 +12,19 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; use Symfony\Component\Yaml\Yaml; class PurgeQueuesCommand extends ContainerAwareCommand { + /** @var QueueClientInterface */ + private $queueClient; - /** - * @var Output $output - */ - private $output; + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } protected function configure() { @@ -51,12 +52,12 @@ protected function configure() } /** - * @param QueueClientInterface $queueClient - * @param string $fileName - * @param string|null $priority + * @param OutputInterface $output + * @param $fileName + * @param $priority * @return int */ - private function purgeFromFile($queueClient, $fileName, $priority) + private function purgeFromFile(OutputInterface $output, $fileName, $priority) { try { $processor = new Processor(); @@ -64,22 +65,22 @@ private function purgeFromFile($queueClient, $fileName, $priority) $processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName))); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::CRITICAL); + $output->writeln($e->getMessage()); return 1; } array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer()); - $this->output->write('Start purge queue.', Output::INFO); + $output->writeln('Start purge queue.'); foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) { $queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE]; try { - $queueClient->purgeQueue($queueName, $priority); - $this->output->write('Queue ' . $queueName . ' purged.', Output::INFO); + $this->queueClient->purgeQueue($queueName, $priority); + $output->writeln('Queue ' . $queueName . ' purged.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->writeln($e->getMessage()); } } - $this->output->write('End purge queue.', Output::INFO); + $output->writeln('End purge queue.'); return 0; } @@ -94,25 +95,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $helper = $this->getHelper('question'); $force = $input->getOption('force') ? true : false; - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); - try { - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - } catch (ServiceNotFoundException $e) { - $this->output->write('No queue client service found.', Output::CRITICAL); - - return 1; - } $priority = null; if ($input->getOption('priority')) { $priority = $input->getOption('priority'); - if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) { + if (!in_array($priority, $this->queueClient->getPriorityHandler()->getAll())) { throw new \InvalidArgumentException('Priority "' . $priority . '" not found.'); } } @@ -123,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } - return $this->purgeFromFile($queueClient, $fileName, $priority); + return $this->purgeFromFile($output, $fileName, $priority); } else { $queues = $input->getArgument('queues'); if (count($queues)) { @@ -133,10 +119,10 @@ protected function execute(InputInterface $input, OutputInterface $output) } foreach ($queues as $queue) { try { - $queueClient->purgeQueue($queue, $priority); - $this->output->write('Queue ' . $queue . ' purged.', Output::INFO); + $this->queueClient->purgeQueue($queue, $priority); + $output->writeln('Queue ' . $queue . ' purged.'); } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::WARNING); + $output->writeln($e->getMessage()); } } @@ -149,9 +135,9 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } - return $this->purgeFromFile($queueClient, $fileName, $priority); + return $this->purgeFromFile($output, $fileName, $priority); } catch (InvalidArgumentException $e) { - $this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL); + $output->writeln('No queue_client.queues_file parameter found.'); return 1; } diff --git a/Command/QueuesInfoCommand.php b/Command/QueuesInfoCommand.php index 26cf3ad..ecd6e72 100644 --- a/Command/QueuesInfoCommand.php +++ b/Command/QueuesInfoCommand.php @@ -2,8 +2,6 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; -use Psr\Log\LoggerInterface; -use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; use ReputationVIP\QueueClient\QueueClientInterface; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Helper\Table; @@ -16,11 +14,6 @@ class QueuesInfoCommand extends ContainerAwareCommand { - /** - * @var Output - */ - private $output; - protected function configure() { $this @@ -41,18 +34,11 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { - try { - /** @var LoggerInterface $logger */ - $logger = $this->getContainer()->get('logger'); - } catch (ServiceNotFoundException $e) { - $logger = null; - } - $this->output = new Output($logger, $output); try { /** @var QueueClientInterface $queueClient */ $queueClient = $this->getContainer()->get('queue_client'); } catch (ServiceNotFoundException $e) { - $this->output->write('No queue client service found.', Output::CRITICAL); + $output->writeln('No queue client service found.'); return 1; } @@ -62,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $queues = $queuesList; } catch (\Exception $e) { - $this->output->write($e->getMessage(), Output::ERROR); + $output->writeln($e->getMessage()); return 1; } @@ -76,9 +62,11 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($input->getOption('alias')) { $queuesAliases = $queueClient->getAliases(); } + if ($input->getOption('priority')) { $priorities = $queueClient->getPriorityHandler()->getAll(); } + foreach ($queues as $queue) { if (in_array($queue, $queuesList)) { $row = [$queue]; @@ -105,14 +93,16 @@ protected function execute(InputInterface $input, OutputInterface $output) } $arrayRows[] = $row; } else { - $this->output->write('Queue "' . $queue . '" does not exists.', Output::WARNING); + $output->writeln('Queue "' . $queue . '" does not exists.'); } } + if (empty($queues)) { - $this->output->write('No queue found.', Output::NOTICE); + $output->writeln('No queue found.'); return 0; } + $table->setRows($arrayRows); if (!$input->getOption('no-header')) { $headers = []; diff --git a/DependencyInjection/QueueClientExtension.php b/DependencyInjection/QueueClientExtension.php index c8d0f6f..f59483c 100644 --- a/DependencyInjection/QueueClientExtension.php +++ b/DependencyInjection/QueueClientExtension.php @@ -11,7 +11,6 @@ class QueueClientExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { - $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); diff --git a/QueueClientBundle.php b/QueueClientBundle.php index 980baae..42bed3b 100644 --- a/QueueClientBundle.php +++ b/QueueClientBundle.php @@ -2,6 +2,7 @@ namespace ReputationVIP\Bundle\QueueClientBundle; +use Symfony\Component\Console\Application; use Symfony\Component\HttpKernel\Bundle\Bundle; class QueueClientBundle extends Bundle diff --git a/QueueClientFactory.php b/QueueClientFactory.php index c21ab3f..0616d7f 100644 --- a/QueueClientFactory.php +++ b/QueueClientFactory.php @@ -14,6 +14,15 @@ class QueueClientFactory { + /** + * @var LoggerInterface + */ + private $logger; + + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } /** * @param $item @@ -39,16 +48,17 @@ public static function resolveParameters(&$item, $key, $container) * @param ContainerInterface $container * @param AdapterInterface $adapter * @param string $queuesFile + * * @return null|QueueClientInterface + * * @throws \ErrorException */ - public function get($container, $adapter, $queuesFile) + public function get(ContainerInterface $container, AdapterInterface $adapter, $queuesFile) { - /** @var LoggerInterface $logger */ - $logger = $container->get('logger'); $queueClient = new QueueClient($adapter); $processor = new Processor(); $configuration = new QueuesConfiguration(); + $processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($queuesFile))); array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $container); @@ -58,7 +68,7 @@ public function get($container, $adapter, $queuesFile) try { $queueClient->addAlias($queueName, $alias); } catch (QueueAccessException $e) { - $logger->warning($e->getMessage()); + $this->logger->warning($e->getMessage()); } catch (\ErrorException $e) { if ($e->getSeverity() === E_ERROR) { throw $e; diff --git a/Resources/config/services.yml b/Resources/config/services.yml index da797d3..5198a96 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,24 +1,31 @@ services: + _defaults: + autowire: true + autoconfigure: true + public: false + queue_client_adapter_priority_handler: class: "%queue_client.adapter.priority_handler.class%" - queue_client_adapter_factory: - class: ReputationVIP\Bundle\QueueClientBundle\QueueClientAdapterFactory + ReputationVIP\Bundle\QueueClientBundle\QueueClientAdapterFactory: ~ - queue_client_factory: - class: ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory + ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory: ~ - queue_client_adapter: - class: ReputationVIP\QueueClient\Adapter\AbstractAdapter - factory: ["@queue_client_adapter_factory", get] + ReputationVIP\QueueClient\Adapter\AbstractAdapter: + factory: ['@ReputationVIP\Bundle\QueueClientBundle\QueueClientAdapterFactory', get] arguments: - - "%queue_client.config%" - - "@queue_client_adapter_priority_handler" + $config: "%queue_client.config%" + $priorityHandler: "@queue_client_adapter_priority_handler" - queue_client: - class: ReputationVIP\QueueClient\QueueClient - factory: ["@queue_client_factory", get] + ReputationVIP\QueueClient\QueueClient: + factory: ['@ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory', get] arguments: - - "@service_container" - - "@queue_client_adapter" - - "%queue_client.queues_file%" + $container: '@service_container' + $adapter: '@ReputationVIP\QueueClient\Adapter\AbstractAdapter' + $queuesFile: '%queue_client.queues_file%' + + ReputationVIP\QueueClient\QueueClientInterface: '@ReputationVIP\QueueClient\QueueClient' + + ReputationVIP\Bundle\QueueClientBundle\Command\: + resource: '../../Command/*' + tags: ['console.command'] diff --git a/Utils/Output.php b/Utils/Output.php deleted file mode 100644 index 878af3d..0000000 --- a/Utils/Output.php +++ /dev/null @@ -1,75 +0,0 @@ -logger = $logger; - $this->output = $output; - } - - public function write($msg, $level) { - if (null !== $this->logger) { - switch ($level) { - case self::ERROR : - $this->logger->error($msg); - break; - case self::CRITICAL : - $this->logger->critical($msg); - break; - case self::INFO : - $this->logger->info($msg); - break; - case self::NOTICE : - $this->logger->notice($msg); - break; - case self::WARNING : - $this->logger->warning($msg); - break; - default : - $this->logger->debug($msg); - } - } else { - switch ($level) { - case self::ERROR : - $this->output->writeln('' . $msg . ''); - break; - case self::CRITICAL : - $this->output->writeln('' . $msg . ''); - break; - case self::INFO : - $this->output->writeln('' . $msg . ''); - break; - case self::NOTICE : - $this->output->writeln('' . $msg . ''); - break; - case self::WARNING : - $this->output->writeln('' . $msg . ''); - break; - default : - $this->output->writeln($msg); - } - } - } -} diff --git a/composer.json b/composer.json index 08f9e95..5f4ae8b 100644 --- a/composer.json +++ b/composer.json @@ -9,12 +9,12 @@ ], "require": { "reputation-vip/queue-client": "~1.0", - "symfony/http-kernel": ">=2.7", - "symfony/config": ">=2.7", - "symfony/dependency-injection": ">=2.7", - "symfony/framework-bundle": ">=2.7", - "symfony/console": ">=2.7", - "symfony/yaml": ">=2.7", + "symfony/http-kernel": ">=3.3", + "symfony/config": ">=3.3", + "symfony/dependency-injection": ">=3.3", + "symfony/framework-bundle": ">=3.3", + "symfony/console": ">=3.3", + "symfony/yaml": ">=3.3", "psr/log": "^1.0" }, "autoload": { From 9cb59f5ed384ea0bc391ba259b919bf88de7d615 Mon Sep 17 00:00:00 2001 From: Olivier Balais Date: Tue, 5 Dec 2017 13:09:12 +0100 Subject: [PATCH 2/5] Prepare 2.0 release --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d601a4..49161f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Queue Client Bundle Changelog +## v2.0.0 + +- Use autowire +- Explicitly declare Commands +- [BR] Symfony 3.3 is now required + ## v1.1.1 - Remove debug code From 40c9b139b0244b82068149768558e4350705fcc4 Mon Sep 17 00:00:00 2001 From: Olivier Balais Date: Wed, 6 Dec 2017 15:58:12 +0100 Subject: [PATCH 3/5] Remove container aware command when useless --- Command/QueuesInfoCommand.php | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Command/QueuesInfoCommand.php b/Command/QueuesInfoCommand.php index ecd6e72..9c6732a 100644 --- a/Command/QueuesInfoCommand.php +++ b/Command/QueuesInfoCommand.php @@ -3,17 +3,26 @@ namespace ReputationVIP\Bundle\QueueClientBundle\Command; use ReputationVIP\QueueClient\QueueClientInterface; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableCell; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; -class QueuesInfoCommand extends ContainerAwareCommand +class QueuesInfoCommand extends Command { + /** @var QueueClientInterface */ + private $queueClient; + + public function __construct(QueueClientInterface $queueClient) + { + parent::__construct(); + + $this->queueClient = $queueClient; + } + protected function configure() { $this @@ -30,20 +39,13 @@ protected function configure() /** * @param InputInterface $input * @param OutputInterface $output + * * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { - try { - /** @var QueueClientInterface $queueClient */ - $queueClient = $this->getContainer()->get('queue_client'); - } catch (ServiceNotFoundException $e) { - $output->writeln('No queue client service found.'); - - return 1; - } $queues = $input->getArgument('queues'); - $queuesList = $queueClient->listQueues(); + $queuesList = $this->queueClient->listQueues(); if (0 === count($queues)) { try { $queues = $queuesList; @@ -60,11 +62,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $priorities = []; if ($input->getOption('alias')) { - $queuesAliases = $queueClient->getAliases(); + $queuesAliases = $this->queueClient->getAliases(); } if ($input->getOption('priority')) { - $priorities = $queueClient->getPriorityHandler()->getAll(); + $priorities = $this->queueClient->getPriorityHandler()->getAll(); } foreach ($queues as $queue) { @@ -73,11 +75,11 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($input->getOption('count')) { if ($input->getOption('priority')) { foreach ($priorities as $priority) { - $count = $queueClient->getNumberMessages($queue, $priority); + $count = $this->queueClient->getNumberMessages($queue, $priority); $row[] = $count; } } else { - $count = $queueClient->getNumberMessages($queue); + $count = $this->queueClient->getNumberMessages($queue); $row[] = $count; } } From 2572a6405db2166ff9831ac19e27211a4af39fd0 Mon Sep 17 00:00:00 2001 From: Olivier Balais Date: Wed, 6 Dec 2017 16:02:56 +0100 Subject: [PATCH 4/5] Fix README --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 974c6c9..c099447 100644 --- a/README.md +++ b/README.md @@ -69,4 +69,3 @@ queue_client: - ```secret:``` this config value set the SQS secret. - ```region:``` this config value set the SQS region (default `eu-west-1`). - ```version:``` this config value set the SQS version (default `2012-11-05`). - From b989531f0604204d445d402281d006053373be48 Mon Sep 17 00:00:00 2001 From: Olivier Balais Date: Wed, 6 Dec 2017 16:47:28 +0100 Subject: [PATCH 5/5] Use queue-client 2.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5f4ae8b..e9eb64f 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ } ], "require": { - "reputation-vip/queue-client": "~1.0", + "reputation-vip/queue-client": "~2.0", "symfony/http-kernel": ">=3.3", "symfony/config": ">=3.3", "symfony/dependency-injection": ">=3.3",