-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an adapter to support the Symfony Mailer component and deprecate …
…the Swiftmailer integration Co-authored-by: Michael Babker <[email protected]>
- Loading branch information
Showing
17 changed files
with
468 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
|
||
/composer.phar | ||
/composer.lock | ||
|
||
/.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Bundle/DependencyInjection/Compiler/RegisterRendererAdapterPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\MailerBundle\DependencyInjection\Compiler; | ||
|
||
use Sylius\Bundle\MailerBundle\Renderer\Adapter\EmailTwigAdapter; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class RegisterRendererAdapterPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if ($container->hasAlias('sylius.email_renderer.adapter')) { | ||
return; | ||
} | ||
|
||
if ($container->has('twig')) { | ||
$twigAdapter = new ChildDefinition('sylius.email_renderer.adapter.abstract'); | ||
$twigAdapter->setClass(EmailTwigAdapter::class); | ||
$twigAdapter->setArguments([new Reference('twig'), new Reference('event_dispatcher', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]); | ||
$twigAdapter->setPublic(true); | ||
|
||
$container->setDefinition('sylius.email_renderer.adapter.twig', $twigAdapter); | ||
$container->setAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.twig'); | ||
|
||
return; | ||
} | ||
|
||
$container->setAlias('sylius.email_renderer.adapter', 'sylius.email_renderer.adapter.default'); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Bundle/DependencyInjection/Compiler/RegisterSenderAdapterPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\MailerBundle\DependencyInjection\Compiler; | ||
|
||
use Sylius\Bundle\MailerBundle\Sender\Adapter\SwiftMailerAdapter; | ||
use Sylius\Bundle\MailerBundle\Sender\Adapter\SymfonyMailerAdapter; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class RegisterSenderAdapterPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if ($container->hasAlias('sylius.email_sender.adapter')) { | ||
return; | ||
} | ||
|
||
if ($container->has('swiftmailer.mailer.default')) { | ||
$swiftmailerAdapter = new ChildDefinition('sylius.email_sender.adapter.abstract'); | ||
/** @psalm-suppress DeprecatedClass */ | ||
$swiftmailerAdapter->setClass(SwiftMailerAdapter::class); | ||
$swiftmailerAdapter->setArguments([new Reference('swiftmailer.mailer.default'), new Reference('event_dispatcher', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]); | ||
$swiftmailerAdapter->setPublic(true); | ||
$swiftmailerAdapter->setDeprecated( | ||
'sylius/mailer-bundle', | ||
'1.8', | ||
'The "%service_id%" service is deprecated, use the Symfony Mailer integration instead.' | ||
); | ||
|
||
$container->setDefinition('sylius.email_sender.adapter.swiftmailer', $swiftmailerAdapter); | ||
$container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.swiftmailer'); | ||
|
||
return; | ||
} | ||
|
||
if ($container->has('mailer.mailer')) { | ||
$symfonyMailerAdapter = new ChildDefinition('sylius.email_sender.adapter.abstract'); | ||
$symfonyMailerAdapter->setClass(SymfonyMailerAdapter::class); | ||
$symfonyMailerAdapter->setArguments([new Reference('mailer.mailer')]); | ||
$symfonyMailerAdapter->setPublic(true); | ||
|
||
$container->setDefinition('sylius.email_sender.adapter.symfony_mailer', $symfonyMailerAdapter); | ||
$container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.symfony_mailer'); | ||
|
||
return; | ||
} | ||
|
||
$container->setAlias('sylius.email_sender.adapter', 'sylius.email_sender.adapter.default'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\MailerBundle\Sender\Adapter; | ||
|
||
use Sylius\Component\Mailer\Event\EmailSendEvent; | ||
use Sylius\Component\Mailer\Model\EmailInterface; | ||
use Sylius\Component\Mailer\Renderer\RenderedEmail; | ||
use Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter; | ||
use Sylius\Component\Mailer\SyliusMailerEvents; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
|
||
final class SymfonyMailerAdapter extends AbstractAdapter | ||
{ | ||
private MailerInterface $mailer; | ||
|
||
public function __construct(MailerInterface $mailer) | ||
{ | ||
$this->mailer = $mailer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function send( | ||
array $recipients, | ||
string $senderAddress, | ||
string $senderName, | ||
RenderedEmail $renderedEmail, | ||
EmailInterface $email, | ||
array $data, | ||
array $attachments = [], | ||
array $replyTo = [] | ||
): void { | ||
$message = (new Email()) | ||
->subject($renderedEmail->getSubject()) | ||
->from(new Address($senderAddress, $senderName)) | ||
->to(...$recipients) | ||
->replyTo(...$replyTo) | ||
->html($renderedEmail->getBody()); | ||
|
||
foreach ($attachments as $attachment) { | ||
$message->attachFromPath($attachment); | ||
} | ||
|
||
$emailSendEvent = new EmailSendEvent($message, $email, $data, $recipients, $replyTo); | ||
|
||
$this->dispatcher?->dispatch($emailSendEvent, SyliusMailerEvents::EMAIL_PRE_SEND); | ||
|
||
$this->mailer->send($message); | ||
|
||
$this->dispatcher?->dispatch($emailSendEvent, SyliusMailerEvents::EMAIL_POST_SEND); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
namespace spec\Sylius\Bundle\MailerBundle\Sender\Adapter; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\MailerBundle\Sender\Adapter\SwiftMailerAdapter; | ||
use Sylius\Bundle\MailerBundle\Sender\Adapter\SymfonyMailerAdapter; | ||
use Sylius\Component\Mailer\Model\EmailInterface; | ||
use Sylius\Component\Mailer\Renderer\RenderedEmail; | ||
use Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter; | ||
|
@@ -32,8 +32,8 @@ function it_throws_an_exception_about_not_configured_email_sender_adapter( | |
): void { | ||
$this | ||
->shouldThrow(new \RuntimeException(sprintf( | ||
'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/swiftmailer-bundle" library).', | ||
SwiftMailerAdapter::class | ||
'You need to configure an adapter to send the email. Take a look at %s (requires "symfony/mailer" library).', | ||
SymfonyMailerAdapter::class | ||
))) | ||
->during('send', [['[email protected]'], '[email protected]', 'arnaud', $renderedEmail, $email, []]) | ||
; | ||
|
Oops, something went wrong.