SetupTransportsCommand.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\Command;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
  18. /**
  19. * @author Vincent Touzet <vincent.touzet@gmail.com>
  20. */
  21. class SetupTransportsCommand extends Command
  22. {
  23. protected static $defaultName = 'messenger:setup-transports';
  24. private $transportLocator;
  25. private $transportNames;
  26. public function __construct(ContainerInterface $transportLocator, array $transportNames = [])
  27. {
  28. $this->transportLocator = $transportLocator;
  29. $this->transportNames = $transportNames;
  30. parent::__construct();
  31. }
  32. protected function configure()
  33. {
  34. $this
  35. ->addArgument('transport', InputArgument::OPTIONAL, 'Name of the transport to setup', null)
  36. ->setDescription('Prepare the required infrastructure for the transport')
  37. ->setHelp(<<<EOF
  38. The <info>%command.name%</info> command setups the transports:
  39. <info>php %command.full_name%</info>
  40. Or a specific transport only:
  41. <info>php %command.full_name% <transport></info>
  42. EOF
  43. )
  44. ;
  45. }
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. $io = new SymfonyStyle($input, $output);
  49. $transportNames = $this->transportNames;
  50. // do we want to set up only one transport?
  51. if ($transport = $input->getArgument('transport')) {
  52. if (!$this->transportLocator->has($transport)) {
  53. throw new \RuntimeException(sprintf('The "%s" transport does not exist.', $transport));
  54. }
  55. $transportNames = [$transport];
  56. }
  57. foreach ($transportNames as $id => $transportName) {
  58. $transport = $this->transportLocator->get($transportName);
  59. if ($transport instanceof SetupableTransportInterface) {
  60. $transport->setup();
  61. $io->success(sprintf('The "%s" transport was set up successfully.', $transportName));
  62. } else {
  63. $io->note(sprintf('The "%s" transport does not support setup.', $transportName));
  64. }
  65. }
  66. return 0;
  67. }
  68. }