|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\SwiftmailerBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | +use Symfony\Component\Routing\RouterInterface; |
| 19 | +use Symfony\Component\DependencyInjection\Alias; |
| 20 | +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 21 | + |
| 22 | +/** |
| 23 | + * A console command for retrieving information about mailers |
| 24 | + * |
| 25 | + * @author Jérémy Romey <[email protected]> |
| 26 | + */ |
| 27 | +class DebugCommand extends ContainerAwareCommand |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @see Command |
| 31 | + */ |
| 32 | + protected function configure() |
| 33 | + { |
| 34 | + $this |
| 35 | + ->setName('swiftmailer:debug') |
| 36 | + ->setDefinition(array( |
| 37 | + new InputArgument('name', InputArgument::OPTIONAL, 'A mailer name'), |
| 38 | + )) |
| 39 | + ->setDescription('Displays current mailers for an application') |
| 40 | + ->setHelp(<<<EOF |
| 41 | +The <info>%command.name%</info> displays the configured mailers: |
| 42 | +
|
| 43 | + <info>php %command.full_name% mailer-name</info> |
| 44 | +EOF |
| 45 | + ) |
| 46 | + ; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @see Command |
| 51 | + */ |
| 52 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 53 | + { |
| 54 | + $name = $input->getArgument('name'); |
| 55 | + |
| 56 | + if ($name) { |
| 57 | + $this->outputMailer($output, $name); |
| 58 | + } else { |
| 59 | + $this->outputMailers($output); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected function outputMailers(OutputInterface $output, $routes = null) |
| 64 | + { |
| 65 | + $output->writeln($this->getHelper('formatter')->formatSection('swiftmailer', 'Current mailers')); |
| 66 | + |
| 67 | + $maxName = strlen('name'); |
| 68 | + $maxTransport = strlen('transport'); |
| 69 | + $maxSpool = strlen('spool'); |
| 70 | + $maxDelivery = strlen('delivery'); |
| 71 | + $maxSingleAddress = strlen('single address'); |
| 72 | + |
| 73 | + $mailers = $this->getContainer()->getParameter('swiftmailer.mailers'); |
| 74 | + foreach ($mailers as $name => $mailer) { |
| 75 | + $mailer = $this->getContainer()->get($mailer); |
| 76 | + $transport = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name)); |
| 77 | + $spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO'; |
| 78 | + $delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO'; |
| 79 | + $singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name)); |
| 80 | + |
| 81 | + if ($this->isDefaultMailer($name)) { |
| 82 | + $name = sprintf('%s (default mailer)', $name); |
| 83 | + } |
| 84 | + $maxName = max($maxName, strlen($name)); |
| 85 | + $maxTransport = max($maxTransport, strlen($transport)); |
| 86 | + $maxSpool = max($maxSpool, strlen($spool)); |
| 87 | + $maxDelivery = max($maxDelivery, strlen($delivery)); |
| 88 | + $maxSingleAddress = max($maxSingleAddress, strlen($singleAddress)); |
| 89 | + } |
| 90 | + $format = '%-'.$maxName.'s %-'.$maxTransport.'s %-'.$maxSpool.'s %-'.$maxDelivery.'s %-'.$maxSingleAddress.'s'; |
| 91 | + |
| 92 | + $format1 = '%-'.($maxName + 19).'s %-'.($maxTransport + 19).'s %-'.($maxSpool + 19).'s %-'.($maxDelivery + 19).'s %-'.($maxSingleAddress + 19).'s'; |
| 93 | + $output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Transport</comment>', '<comment>Spool</comment>', '<comment>Delivery</comment>', '<comment>Single Address</comment>')); |
| 94 | + foreach ($mailers as $name => $mailer) { |
| 95 | + $mailer = $this->getContainer()->get($mailer); |
| 96 | + $transport = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name)); |
| 97 | + $spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO'; |
| 98 | + $delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO'; |
| 99 | + $singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name)); |
| 100 | + if ($this->isDefaultMailer($name)) { |
| 101 | + $name = sprintf('%s (default mailer)', $name); |
| 102 | + } |
| 103 | + $output->writeln(sprintf($format, $name, $transport, $spool, $delivery, $singleAddress)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @throws \InvalidArgumentException When route does not exist |
| 109 | + */ |
| 110 | + protected function outputMailer(OutputInterface $output, $name) |
| 111 | + { |
| 112 | + try { |
| 113 | + $service = sprintf('swiftmailer.mailer.%s', $name); |
| 114 | + $mailer = $this->getContainer()->get($service); |
| 115 | + } catch (ServiceNotFoundException $e) { |
| 116 | + throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $name)); |
| 117 | + } |
| 118 | + |
| 119 | + $transport = $mailer->getTransport(); |
| 120 | + $spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO'; |
| 121 | + $delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO'; |
| 122 | + $singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name)); |
| 123 | + |
| 124 | + $output->writeln($this->getHelper('formatter')->formatSection('swiftmailer', sprintf('Mailer "%s"', $name))); |
| 125 | + if ($this->isDefaultMailer($name)) { |
| 126 | + $output->writeln('This is the default mailer'); |
| 127 | + } |
| 128 | + |
| 129 | + $output->writeln(sprintf('<comment>Name</comment> %s', $name)); |
| 130 | + $output->writeln(sprintf('<comment>Service</comment> %s', $service)); |
| 131 | + $output->writeln(sprintf('<comment>Class</comment> %s', get_class($mailer))); |
| 132 | + $output->writeln(sprintf('<comment>Transport</comment> %s (%s)', sprintf('swiftmailer.mailer.%s.transport.name', $name), get_class($transport))); |
| 133 | + $output->writeln(sprintf('<comment>Spool</comment> %s', $spool)); |
| 134 | + if ($this->getContainer()->hasParameter(sprintf('swiftmailer.spool.%s.file.path', $name))) { |
| 135 | + $output->writeln(sprintf('<comment>Spool file</comment> %s', $this->getContainer()->getParameter(sprintf('swiftmailer.spool.%s.file.path', $name)))); |
| 136 | + } |
| 137 | + $output->writeln(sprintf('<comment>Delivery</comment> %s', $delivery)); |
| 138 | + $output->writeln(sprintf('<comment>Single Address</comment> %s', $singleAddress)); |
| 139 | + } |
| 140 | + |
| 141 | + private function isDefaultMailer($name) |
| 142 | + { |
| 143 | + return ($this->getContainer()->getParameter('swiftmailer.default_mailer') == $name || 'default' == $name) ? true : false; |
| 144 | + } |
| 145 | +} |
0 commit comments