-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an adapter to support the Symfony Mailer component and deprecate the Swiftmailer integration #102
Add an adapter to support the Symfony Mailer component and deprecate the Swiftmailer integration #102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
|
||
/composer.phar | ||
/composer.lock | ||
|
||
/.phpunit.result.cache |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ | |
|
||
<service id="sylius.email_renderer.adapter.abstract" class="Sylius\Component\Mailer\Renderer\Adapter\AbstractAdapter" abstract="true"> | ||
<call method="setEventDispatcher"> | ||
<argument type="service" id="event_dispatcher" /> | ||
<argument type="service" id="event_dispatcher" on-invalid="ignore" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (and the change immediately below) ends up being a bug fix. The event dispatcher is an optional dependency in the classes that support it, yet compiling the container would fail hard if the dispatcher service isn't there. This ensures that the dispatcher stays optional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't on-invalid="null"? is this different? I don't find any documentation about this value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s the “ignoring missing dependencies” section on that page. The null behavior is valid if you’re working with a nullable argument. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yes, I just had not seen it was with a call method, excuxe me. |
||
</call> | ||
</service> | ||
<service | ||
|
@@ -49,10 +49,19 @@ | |
parent="sylius.email_renderer.adapter.abstract" | ||
public="true" | ||
/> | ||
<service | ||
id="sylius.email_renderer.adapter.twig" | ||
class="Sylius\Bundle\MailerBundle\Renderer\Adapter\EmailTwigAdapter" | ||
parent="sylius.email_renderer.adapter.abstract" | ||
public="true" | ||
> | ||
<argument type="service" id="twig" /> | ||
<argument type="service" id="event_dispatcher" on-invalid="null" /> | ||
</service> | ||
|
||
<service id="sylius.email_sender.adapter.abstract" class="Sylius\Component\Mailer\Sender\Adapter\AbstractAdapter" abstract="true"> | ||
<call method="setEventDispatcher"> | ||
<argument type="service" id="event_dispatcher" /> | ||
<argument type="service" id="event_dispatcher" on-invalid="ignore" /> | ||
</call> | ||
</service> | ||
<service | ||
|
@@ -61,5 +70,23 @@ | |
parent="sylius.email_sender.adapter.abstract" | ||
public="true" | ||
/> | ||
<service | ||
id="sylius.email_sender.adapter.swiftmailer" | ||
class="Sylius\Bundle\MailerBundle\Sender\Adapter\SwiftMailerAdapter" | ||
parent="sylius.email_sender.adapter.abstract" | ||
public="true" | ||
> | ||
<deprecated package="sylius/mailer-bundle" version="1.8">The "%service_id%" service is deprecated and will be removed in 2.0, use the Symfony Mailer integration instead.</deprecated> | ||
<argument type="service" id="swiftmailer.mailer.default" /> | ||
<argument type="service" id="event_dispatcher" on-invalid="null" /> | ||
</service> | ||
<service | ||
id="sylius.email_sender.adapter.symfony_mailer" | ||
class="Sylius\Bundle\MailerBundle\Sender\Adapter\SymfonyMailerAdapter" | ||
parent="sylius.email_sender.adapter.abstract" | ||
public="true" | ||
> | ||
<argument type="service" id="mailer.mailer" /> | ||
</service> | ||
</services> | ||
</container> |
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); | ||
} | ||
} |
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, []]) | ||
; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we keep this test suite this way for a moment, I think we have to add
composer require --dev symfony/mailer
at the endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not following what it is you’re suggesting to do. The order of operations for all the Composer commands is fine as is, there is no point in doing a full reset to the starting state at the end of each lint step (everything a step is doing is building off the state the previous step left things in).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think depending on a previous one is a little bit risky. On resource bundle, we do that : https://github.com/Sylius/SyliusResourceBundle/blob/1.10/.github/workflows/build.yml#L106
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By that same logic, you can’t rely on the previous steps to have set up the environment correctly either 😉 (if these were parallel or separately running jobs it’d be another story, but it’s a single threaded build).
The only difference in workflows is where certain commands are run, and running less Composer commands overall since each step builds on the previous.