Skip to content

Commit b3000d0

Browse files
committed
Make "mailer" service optional
1 parent 6d02c6c commit b3000d0

File tree

7 files changed

+156
-103
lines changed

7 files changed

+156
-103
lines changed

DependencyInjection/Compiler/CheckForMailerPass.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Checks to see if the mailer service exists.
2020
*
2121
* @author Ryan Weaver <[email protected]>
22+
*
23+
* @deprecated since 2.1, "mailer" service should be optional
2224
*/
2325
class CheckForMailerPass implements CompilerPassInterface
2426
{

FOSUserBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
1515
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
1616
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
17-
use FOS\UserBundle\DependencyInjection\Compiler\CheckForMailerPass;
1817
use FOS\UserBundle\DependencyInjection\Compiler\CheckForSessionPass;
1918
use FOS\UserBundle\DependencyInjection\Compiler\InjectRememberMeServicesPass;
2019
use FOS\UserBundle\DependencyInjection\Compiler\InjectUserCheckerPass;
@@ -38,7 +37,6 @@ public function build(ContainerBuilder $container)
3837
$container->addCompilerPass(new InjectUserCheckerPass());
3938
$container->addCompilerPass(new InjectRememberMeServicesPass());
4039
$container->addCompilerPass(new CheckForSessionPass());
41-
$container->addCompilerPass(new CheckForMailerPass());
4240

4341
$this->addRegisterMappingsPass($container);
4442
}

Mailer/Mailer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public function sendResettingEmailMessage(UserInterface $user)
9191
*/
9292
protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail)
9393
{
94+
if (null === $this->mailer) {
95+
throw new \RuntimeException(
96+
'Sending email requires the "mailer" service to be available. '.
97+
'Run "composer require symfony/swiftmailer-bundle" to install Swiftmailer.'
98+
);
99+
}
100+
94101
// Render the email, use the first line as the subject, and the rest as the body
95102
$renderedLines = explode("\n", trim($renderedTemplate));
96103
$subject = array_shift($renderedLines);

Mailer/TwigMailer.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\UserBundle\Mailer;
13+
14+
use FOS\UserBundle\Model\UserInterface;
15+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16+
17+
/**
18+
* @author Christophe Coevoet <[email protected]>
19+
*/
20+
class TwigMailer implements MailerInterface
21+
{
22+
/**
23+
* @var \Swift_Mailer
24+
*/
25+
protected $mailer;
26+
27+
/**
28+
* @var UrlGeneratorInterface
29+
*/
30+
protected $router;
31+
32+
/**
33+
* @var \Twig_Environment
34+
*/
35+
protected $twig;
36+
37+
/**
38+
* @var array
39+
*/
40+
protected $parameters;
41+
42+
/**
43+
* TwigMailer constructor.
44+
*
45+
* @param \Swift_Mailer $mailer
46+
* @param UrlGeneratorInterface $router
47+
* @param \Twig_Environment $twig
48+
* @param array $parameters
49+
*/
50+
public function __construct($mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
51+
{
52+
$this->mailer = $mailer;
53+
$this->router = $router;
54+
$this->twig = $twig;
55+
$this->parameters = $parameters;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function sendConfirmationEmailMessage(UserInterface $user)
62+
{
63+
$template = $this->parameters['template']['confirmation'];
64+
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
65+
66+
$context = array(
67+
'user' => $user,
68+
'confirmationUrl' => $url,
69+
);
70+
71+
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail());
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function sendResettingEmailMessage(UserInterface $user)
78+
{
79+
$template = $this->parameters['template']['resetting'];
80+
$url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
81+
82+
$context = array(
83+
'user' => $user,
84+
'confirmationUrl' => $url,
85+
);
86+
87+
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail());
88+
}
89+
90+
/**
91+
* @param string $templateName
92+
* @param array $context
93+
* @param array $fromEmail
94+
* @param string $toEmail
95+
*/
96+
protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
97+
{
98+
if (null === $this->mailer) {
99+
throw new \RuntimeException(
100+
'Sending email requires the "mailer" service to be available. '.
101+
'Run "composer require symfony/swiftmailer-bundle" to install Swiftmailer.'
102+
);
103+
}
104+
105+
$template = $this->twig->load($templateName);
106+
$subject = $template->renderBlock('subject', $context);
107+
$textBody = $template->renderBlock('body_text', $context);
108+
109+
$htmlBody = '';
110+
111+
if ($template->hasBlock('body_html', $context)) {
112+
$htmlBody = $template->renderBlock('body_html', $context);
113+
}
114+
115+
$message = (new \Swift_Message())
116+
->setSubject($subject)
117+
->setFrom($fromEmail)
118+
->setTo($toEmail);
119+
120+
if (!empty($htmlBody)) {
121+
$message->setBody($htmlBody, 'text/html')
122+
->addPart($textBody, 'text/plain');
123+
} else {
124+
$message->setBody($textBody);
125+
}
126+
127+
$this->mailer->send($message);
128+
}
129+
}

Mailer/TwigSwiftMailer.php

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,15 @@
1111

1212
namespace FOS\UserBundle\Mailer;
1313

14-
use FOS\UserBundle\Model\UserInterface;
1514
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1615

1716
/**
1817
* @author Christophe Coevoet <[email protected]>
18+
*
19+
* @deprecated TwigSwiftMailer class is deprecated since 2.1, use TwigMailer instead.
1920
*/
20-
class TwigSwiftMailer implements MailerInterface
21+
class TwigSwiftMailer extends TwigMailer
2122
{
22-
/**
23-
* @var \Swift_Mailer
24-
*/
25-
protected $mailer;
26-
27-
/**
28-
* @var UrlGeneratorInterface
29-
*/
30-
protected $router;
31-
32-
/**
33-
* @var \Twig_Environment
34-
*/
35-
protected $twig;
36-
37-
/**
38-
* @var array
39-
*/
40-
protected $parameters;
41-
4223
/**
4324
* TwigSwiftMailer constructor.
4425
*
@@ -49,74 +30,10 @@ class TwigSwiftMailer implements MailerInterface
4930
*/
5031
public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters)
5132
{
52-
$this->mailer = $mailer;
53-
$this->router = $router;
54-
$this->twig = $twig;
55-
$this->parameters = $parameters;
56-
}
57-
58-
/**
59-
* {@inheritdoc}
60-
*/
61-
public function sendConfirmationEmailMessage(UserInterface $user)
62-
{
63-
$template = $this->parameters['template']['confirmation'];
64-
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
65-
66-
$context = array(
67-
'user' => $user,
68-
'confirmationUrl' => $url,
69-
);
70-
71-
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail());
72-
}
73-
74-
/**
75-
* {@inheritdoc}
76-
*/
77-
public function sendResettingEmailMessage(UserInterface $user)
78-
{
79-
$template = $this->parameters['template']['resetting'];
80-
$url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL);
81-
82-
$context = array(
83-
'user' => $user,
84-
'confirmationUrl' => $url,
85-
);
86-
87-
$this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail());
88-
}
89-
90-
/**
91-
* @param string $templateName
92-
* @param array $context
93-
* @param array $fromEmail
94-
* @param string $toEmail
95-
*/
96-
protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
97-
{
98-
$template = $this->twig->load($templateName);
99-
$subject = $template->renderBlock('subject', $context);
100-
$textBody = $template->renderBlock('body_text', $context);
101-
102-
$htmlBody = '';
103-
104-
if ($template->hasBlock('body_html', $context)) {
105-
$htmlBody = $template->renderBlock('body_html', $context);
106-
}
107-
108-
$message = (new \Swift_Message())
109-
->setSubject($subject)
110-
->setFrom($fromEmail)
111-
->setTo($toEmail);
112-
113-
if (!empty($htmlBody)) {
114-
$message->setBody($htmlBody, 'text/html')
115-
->addPart($textBody, 'text/plain');
116-
} else {
117-
$message->setBody($textBody);
118-
}
33+
@trigger_error(sprintf(
34+
'%s is deprecated, use %s instead.', __CLASS__, TwigMailer::class
35+
), E_USER_DEPRECATED);
11936

120-
$this->mailer->send($message);
37+
parent::__construct($mailer, $router, $twig, $parameters);
12138
}
12239
}

Resources/config/mailer.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<services>
1919
<service id="fos_user.mailer.default" class="FOS\UserBundle\Mailer\Mailer" public="false">
20-
<argument type="service" id="mailer" />
20+
<argument type="service" id="mailer" on-invalid="null" />
2121
<argument type="service" id="router" />
2222
<argument type="service" id="templating" />
2323
<argument type="collection">
@@ -31,8 +31,8 @@
3131
<tag name="fos_user.requires_swift" />
3232
</service>
3333

34-
<service id="fos_user.mailer.twig_swift" class="FOS\UserBundle\Mailer\TwigSwiftMailer" public="false">
35-
<argument type="service" id="mailer" />
34+
<service id="fos_user.mailer.twig_swift" class="FOS\UserBundle\Mailer\TwigMailer" public="false">
35+
<argument type="service" id="mailer" on-invalid="null" />
3636
<argument type="service" id="router" />
3737
<argument type="service" id="twig" />
3838
<argument type="collection">

Tests/Mailer/TwigSwiftMailerTest.php renamed to Tests/Mailer/TwigMailerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111

1212
namespace FOS\UserBundle\Tests\Mailer;
1313

14-
use FOS\UserBundle\Mailer\TwigSwiftMailer;
14+
use FOS\UserBundle\Mailer\TwigMailer;
1515
use PHPUnit\Framework\TestCase;
1616
use Swift_Mailer;
1717
use Swift_Transport_NullTransport;
1818

19-
class TwigSwiftMailerTest extends TestCase
19+
class TwigMailerTest extends TestCase
2020
{
2121
/**
2222
* @dataProvider goodEmailProvider
2323
*/
2424
public function testSendConfirmationEmailMessageWithGoodEmails($emailAddress)
2525
{
26-
$mailer = $this->getTwigSwiftMailer();
26+
$mailer = $this->getTwigMailer();
2727
$mailer->sendConfirmationEmailMessage($this->getUser($emailAddress));
2828

2929
$this->assertTrue(true);
@@ -35,7 +35,7 @@ public function testSendConfirmationEmailMessageWithGoodEmails($emailAddress)
3535
*/
3636
public function testSendConfirmationEmailMessageWithBadEmails($emailAddress)
3737
{
38-
$mailer = $this->getTwigSwiftMailer();
38+
$mailer = $this->getTwigMailer();
3939
$mailer->sendConfirmationEmailMessage($this->getUser($emailAddress));
4040
}
4141

@@ -44,7 +44,7 @@ public function testSendConfirmationEmailMessageWithBadEmails($emailAddress)
4444
*/
4545
public function testSendResettingEmailMessageWithGoodEmails($emailAddress)
4646
{
47-
$mailer = $this->getTwigSwiftMailer();
47+
$mailer = $this->getTwigMailer();
4848
$mailer->sendResettingEmailMessage($this->getUser($emailAddress));
4949

5050
$this->assertTrue(true);
@@ -56,7 +56,7 @@ public function testSendResettingEmailMessageWithGoodEmails($emailAddress)
5656
*/
5757
public function testSendResettingEmailMessageWithBadEmails($emailAddress)
5858
{
59-
$mailer = $this->getTwigSwiftMailer();
59+
$mailer = $this->getTwigMailer();
6060
$mailer->sendResettingEmailMessage($this->getUser($emailAddress));
6161
}
6262

@@ -78,9 +78,9 @@ public function badEmailProvider()
7878
);
7979
}
8080

81-
private function getTwigSwiftMailer()
81+
private function getTwigMailer()
8282
{
83-
return new TwigSwiftMailer(
83+
return new TwigMailer(
8484
new Swift_Mailer(
8585
new Swift_Transport_NullTransport(
8686
$this->getMockBuilder('Swift_Events_EventDispatcher')->getMock()

0 commit comments

Comments
 (0)