forked from symfony/swiftmailer-bundle
/
DebugCommand.php
133 lines (111 loc) · 4.84 KB
/
DebugCommand.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SwiftmailerBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
/**
* A console command for retrieving information about mailers.
*
* @author Jérémy Romey <jeremy@free-agent.fr>
*/
class DebugCommand extends AbstractSwiftMailerCommand
{
protected static $defaultName = 'debug:swiftmailer';
/** @var SymfonyStyle */
private $io;
/**
* @see Command
*/
protected function configure()
{
$this
->setName(static::$defaultName) // BC with 2.7
->setDefinition([
new InputArgument('name', InputArgument::OPTIONAL, 'A mailer name'),
])
->setDescription('Displays current mailers for an application')
->setHelp(
<<<EOF
The <info>%command.name%</info> displays the configured mailers:
<info>php %command.full_name% mailer-name</info>
EOF
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$name = $input->getArgument('name');
if ($name) {
$this->outputMailer($name);
} else {
$this->outputMailers();
}
}
protected function outputMailers($routes = null)
{
$this->io->title('Configured SwiftMailer Mailers');
$tableHeaders = ['Name', 'Transport', 'Spool', 'Delivery', 'Single Address'];
$tableRows = [];
$mailers = $this->getContainer()->getParameter('swiftmailer.mailers');
foreach ($mailers as $name => $mailer) {
$mailer = $this->getContainer()->get($mailer);
$transport = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name));
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
if ($this->isDefaultMailer($name)) {
$name = sprintf('%s (default mailer)', $name);
}
$tableRows[] = [$name, $transport, $spool, $delivery, $singleAddress];
}
$this->io->table($tableHeaders, $tableRows);
}
/**
* @throws \InvalidArgumentException When route does not exist
*/
protected function outputMailer($name)
{
try {
$service = sprintf('swiftmailer.mailer.%s', $name);
$mailer = $this->getContainer()->get($service);
} catch (ServiceNotFoundException $e) {
throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $name));
}
$tableHeaders = ['Property', 'Value'];
$tableRows = [];
$transport = $mailer->getTransport();
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
$this->io->title(sprintf('Configuration of the Mailer "%s"', $name));
if ($this->isDefaultMailer($name)) {
$this->io->comment('This is the default mailer');
}
$tableRows[] = ['Name', $name];
$tableRows[] = ['Service', $service];
$tableRows[] = ['Class', get_class($mailer)];
$tableRows[] = ['Transport', sprintf('%s (%s)', sprintf('swiftmailer.mailer.%s.transport.name', $name), get_class($transport))];
$tableRows[] = ['Spool', $spool];
if ($this->getContainer()->hasParameter(sprintf('swiftmailer.spool.%s.file.path', $name))) {
$tableRows[] = ['Spool file', $this->getContainer()->getParameter(sprintf('swiftmailer.spool.%s.file.path', $name))];
}
$tableRows[] = ['Delivery', $delivery];
$tableRows[] = ['Single Address', $singleAddress];
$this->io->table($tableHeaders, $tableRows);
}
private function isDefaultMailer($name)
{
return $this->getContainer()->getParameter('swiftmailer.default_mailer') === $name || 'default' === $name;
}
}