From b3000d0088536a1384b7057aa0888a9caa42a45e Mon Sep 17 00:00:00 2001 From: "Andrey F. Mindubaev" Date: Wed, 30 May 2018 13:43:33 +0300 Subject: [PATCH] Make "mailer" service optional --- .../Compiler/CheckForMailerPass.php | 2 + FOSUserBundle.php | 2 - Mailer/Mailer.php | 7 + Mailer/TwigMailer.php | 129 ++++++++++++++++++ Mailer/TwigSwiftMailer.php | 97 +------------ Resources/config/mailer.xml | 6 +- ...SwiftMailerTest.php => TwigMailerTest.php} | 16 +-- 7 files changed, 156 insertions(+), 103 deletions(-) create mode 100644 Mailer/TwigMailer.php rename Tests/Mailer/{TwigSwiftMailerTest.php => TwigMailerTest.php} (90%) diff --git a/DependencyInjection/Compiler/CheckForMailerPass.php b/DependencyInjection/Compiler/CheckForMailerPass.php index fa2ee0414f..1381089e36 100644 --- a/DependencyInjection/Compiler/CheckForMailerPass.php +++ b/DependencyInjection/Compiler/CheckForMailerPass.php @@ -19,6 +19,8 @@ * Checks to see if the mailer service exists. * * @author Ryan Weaver + * + * @deprecated since 2.1, "mailer" service should be optional */ class CheckForMailerPass implements CompilerPassInterface { diff --git a/FOSUserBundle.php b/FOSUserBundle.php index e00208552f..4b3388e721 100644 --- a/FOSUserBundle.php +++ b/FOSUserBundle.php @@ -14,7 +14,6 @@ use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass; use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass; -use FOS\UserBundle\DependencyInjection\Compiler\CheckForMailerPass; use FOS\UserBundle\DependencyInjection\Compiler\CheckForSessionPass; use FOS\UserBundle\DependencyInjection\Compiler\InjectRememberMeServicesPass; use FOS\UserBundle\DependencyInjection\Compiler\InjectUserCheckerPass; @@ -38,7 +37,6 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new InjectUserCheckerPass()); $container->addCompilerPass(new InjectRememberMeServicesPass()); $container->addCompilerPass(new CheckForSessionPass()); - $container->addCompilerPass(new CheckForMailerPass()); $this->addRegisterMappingsPass($container); } diff --git a/Mailer/Mailer.php b/Mailer/Mailer.php index 8f9e537a70..52db46b0b0 100644 --- a/Mailer/Mailer.php +++ b/Mailer/Mailer.php @@ -91,6 +91,13 @@ public function sendResettingEmailMessage(UserInterface $user) */ protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail) { + if (null === $this->mailer) { + throw new \RuntimeException( + 'Sending email requires the "mailer" service to be available. '. + 'Run "composer require symfony/swiftmailer-bundle" to install Swiftmailer.' + ); + } + // Render the email, use the first line as the subject, and the rest as the body $renderedLines = explode("\n", trim($renderedTemplate)); $subject = array_shift($renderedLines); diff --git a/Mailer/TwigMailer.php b/Mailer/TwigMailer.php new file mode 100644 index 0000000000..be02284a69 --- /dev/null +++ b/Mailer/TwigMailer.php @@ -0,0 +1,129 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\UserBundle\Mailer; + +use FOS\UserBundle\Model\UserInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + +/** + * @author Christophe Coevoet + */ +class TwigMailer implements MailerInterface +{ + /** + * @var \Swift_Mailer + */ + protected $mailer; + + /** + * @var UrlGeneratorInterface + */ + protected $router; + + /** + * @var \Twig_Environment + */ + protected $twig; + + /** + * @var array + */ + protected $parameters; + + /** + * TwigMailer constructor. + * + * @param \Swift_Mailer $mailer + * @param UrlGeneratorInterface $router + * @param \Twig_Environment $twig + * @param array $parameters + */ + public function __construct($mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters) + { + $this->mailer = $mailer; + $this->router = $router; + $this->twig = $twig; + $this->parameters = $parameters; + } + + /** + * {@inheritdoc} + */ + public function sendConfirmationEmailMessage(UserInterface $user) + { + $template = $this->parameters['template']['confirmation']; + $url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); + + $context = array( + 'user' => $user, + 'confirmationUrl' => $url, + ); + + $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail()); + } + + /** + * {@inheritdoc} + */ + public function sendResettingEmailMessage(UserInterface $user) + { + $template = $this->parameters['template']['resetting']; + $url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); + + $context = array( + 'user' => $user, + 'confirmationUrl' => $url, + ); + + $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail()); + } + + /** + * @param string $templateName + * @param array $context + * @param array $fromEmail + * @param string $toEmail + */ + protected function sendMessage($templateName, $context, $fromEmail, $toEmail) + { + if (null === $this->mailer) { + throw new \RuntimeException( + 'Sending email requires the "mailer" service to be available. '. + 'Run "composer require symfony/swiftmailer-bundle" to install Swiftmailer.' + ); + } + + $template = $this->twig->load($templateName); + $subject = $template->renderBlock('subject', $context); + $textBody = $template->renderBlock('body_text', $context); + + $htmlBody = ''; + + if ($template->hasBlock('body_html', $context)) { + $htmlBody = $template->renderBlock('body_html', $context); + } + + $message = (new \Swift_Message()) + ->setSubject($subject) + ->setFrom($fromEmail) + ->setTo($toEmail); + + if (!empty($htmlBody)) { + $message->setBody($htmlBody, 'text/html') + ->addPart($textBody, 'text/plain'); + } else { + $message->setBody($textBody); + } + + $this->mailer->send($message); + } +} diff --git a/Mailer/TwigSwiftMailer.php b/Mailer/TwigSwiftMailer.php index cad3696828..4c412bef22 100644 --- a/Mailer/TwigSwiftMailer.php +++ b/Mailer/TwigSwiftMailer.php @@ -11,34 +11,15 @@ namespace FOS\UserBundle\Mailer; -use FOS\UserBundle\Model\UserInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * @author Christophe Coevoet + * + * @deprecated TwigSwiftMailer class is deprecated since 2.1, use TwigMailer instead. */ -class TwigSwiftMailer implements MailerInterface +class TwigSwiftMailer extends TwigMailer { - /** - * @var \Swift_Mailer - */ - protected $mailer; - - /** - * @var UrlGeneratorInterface - */ - protected $router; - - /** - * @var \Twig_Environment - */ - protected $twig; - - /** - * @var array - */ - protected $parameters; - /** * TwigSwiftMailer constructor. * @@ -49,74 +30,10 @@ class TwigSwiftMailer implements MailerInterface */ public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $router, \Twig_Environment $twig, array $parameters) { - $this->mailer = $mailer; - $this->router = $router; - $this->twig = $twig; - $this->parameters = $parameters; - } - - /** - * {@inheritdoc} - */ - public function sendConfirmationEmailMessage(UserInterface $user) - { - $template = $this->parameters['template']['confirmation']; - $url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); - - $context = array( - 'user' => $user, - 'confirmationUrl' => $url, - ); - - $this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], (string) $user->getEmail()); - } - - /** - * {@inheritdoc} - */ - public function sendResettingEmailMessage(UserInterface $user) - { - $template = $this->parameters['template']['resetting']; - $url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL); - - $context = array( - 'user' => $user, - 'confirmationUrl' => $url, - ); - - $this->sendMessage($template, $context, $this->parameters['from_email']['resetting'], (string) $user->getEmail()); - } - - /** - * @param string $templateName - * @param array $context - * @param array $fromEmail - * @param string $toEmail - */ - protected function sendMessage($templateName, $context, $fromEmail, $toEmail) - { - $template = $this->twig->load($templateName); - $subject = $template->renderBlock('subject', $context); - $textBody = $template->renderBlock('body_text', $context); - - $htmlBody = ''; - - if ($template->hasBlock('body_html', $context)) { - $htmlBody = $template->renderBlock('body_html', $context); - } - - $message = (new \Swift_Message()) - ->setSubject($subject) - ->setFrom($fromEmail) - ->setTo($toEmail); - - if (!empty($htmlBody)) { - $message->setBody($htmlBody, 'text/html') - ->addPart($textBody, 'text/plain'); - } else { - $message->setBody($textBody); - } + @trigger_error(sprintf( + '%s is deprecated, use %s instead.', __CLASS__, TwigMailer::class + ), E_USER_DEPRECATED); - $this->mailer->send($message); + parent::__construct($mailer, $router, $twig, $parameters); } } diff --git a/Resources/config/mailer.xml b/Resources/config/mailer.xml index b3253ae119..4fc96a389c 100644 --- a/Resources/config/mailer.xml +++ b/Resources/config/mailer.xml @@ -17,7 +17,7 @@ - + @@ -31,8 +31,8 @@ - - + + diff --git a/Tests/Mailer/TwigSwiftMailerTest.php b/Tests/Mailer/TwigMailerTest.php similarity index 90% rename from Tests/Mailer/TwigSwiftMailerTest.php rename to Tests/Mailer/TwigMailerTest.php index f56e02292b..120d30ee6a 100644 --- a/Tests/Mailer/TwigSwiftMailerTest.php +++ b/Tests/Mailer/TwigMailerTest.php @@ -11,19 +11,19 @@ namespace FOS\UserBundle\Tests\Mailer; -use FOS\UserBundle\Mailer\TwigSwiftMailer; +use FOS\UserBundle\Mailer\TwigMailer; use PHPUnit\Framework\TestCase; use Swift_Mailer; use Swift_Transport_NullTransport; -class TwigSwiftMailerTest extends TestCase +class TwigMailerTest extends TestCase { /** * @dataProvider goodEmailProvider */ public function testSendConfirmationEmailMessageWithGoodEmails($emailAddress) { - $mailer = $this->getTwigSwiftMailer(); + $mailer = $this->getTwigMailer(); $mailer->sendConfirmationEmailMessage($this->getUser($emailAddress)); $this->assertTrue(true); @@ -35,7 +35,7 @@ public function testSendConfirmationEmailMessageWithGoodEmails($emailAddress) */ public function testSendConfirmationEmailMessageWithBadEmails($emailAddress) { - $mailer = $this->getTwigSwiftMailer(); + $mailer = $this->getTwigMailer(); $mailer->sendConfirmationEmailMessage($this->getUser($emailAddress)); } @@ -44,7 +44,7 @@ public function testSendConfirmationEmailMessageWithBadEmails($emailAddress) */ public function testSendResettingEmailMessageWithGoodEmails($emailAddress) { - $mailer = $this->getTwigSwiftMailer(); + $mailer = $this->getTwigMailer(); $mailer->sendResettingEmailMessage($this->getUser($emailAddress)); $this->assertTrue(true); @@ -56,7 +56,7 @@ public function testSendResettingEmailMessageWithGoodEmails($emailAddress) */ public function testSendResettingEmailMessageWithBadEmails($emailAddress) { - $mailer = $this->getTwigSwiftMailer(); + $mailer = $this->getTwigMailer(); $mailer->sendResettingEmailMessage($this->getUser($emailAddress)); } @@ -78,9 +78,9 @@ public function badEmailProvider() ); } - private function getTwigSwiftMailer() + private function getTwigMailer() { - return new TwigSwiftMailer( + return new TwigMailer( new Swift_Mailer( new Swift_Transport_NullTransport( $this->getMockBuilder('Swift_Events_EventDispatcher')->getMock()