Skip to content
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

Use the DI container for creating transport factories #67

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Yii Framework 2 Symfony mailer extension Change Log
================================================

4.0.0 Jan 29, 2024
------------------
- Enh: Use DI container for creating factories (sammousa)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should go into 4.0.0 ↓


4.0.0
------

- Enh #45: Include logger proxy as a dependency
- Enh #45: Drop support for end-of-life php versions 7.4 and 8.0
- Enh #45: Include logger proxy as a dependency (sammousa)
- Enh #45: Drop support for end-of-life php versions 7.4 and 8.0 (sammousa)

3.1.0 under development
-----------------------
Expand All @@ -15,7 +19,6 @@ Yii Framework 2 Symfony mailer extension Change Log
- Enh #50: Forward transport logs to the Yii Logger (sammousa)
- Enh #49: Removed dependency on SymfonyMailer class (sammousa)


3.0.0 December 05, 2022
-----------------------

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ Yii::$app->mailer->compose('contact/html')
->send();
```

DI Container
------------
The `Mailer` component will automatically use the DI container when it is available.
This allows you to easily override the transport factory configurations or their dependencies.

Migrating from yiisoft/yii2-swiftmailer
---------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"require": {
"php": ">=8.1",
"psr/event-dispatcher": "1.0.0",
"symfony/mailer": "^6.4 || ^7.0",
"symfony/mime": "^6.4 || ^7.0",
"symfony/mailer": "^7.0",
"symfony/mime": "^7.0",
"yiisoft/yii2": ">=2.0.4"
},
"require-dev": {
Expand Down Expand Up @@ -79,4 +79,4 @@
"psalm": "psalm",
"test": "phpunit"
}
}
}
62 changes: 56 additions & 6 deletions src/Mailer.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
<?php

/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/

declare(strict_types=1);

namespace yii\symfonymailer;

use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\NativeTransportFactory;
use Symfony\Component\Mailer\Transport\NullTransportFactory;
use Symfony\Component\Mailer\Transport\SendmailTransportFactory;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesTransportFactory;
use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
use Symfony\Component\Mailer\Bridge\OhMySmtp\Transport\OhMySmtpTransportFactory;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\mail\BaseMailer;
use yii\psr\DynamicLogger;
use yii\mail\MessageInterface;

/**
* @psalm-suppress PropertyNotSetInConstructor
Expand Down Expand Up @@ -66,16 +82,46 @@ private function getTransport(): TransportInterface
return $this->_transport;
}

/**
* @psalm-suppress UndefinedClass
* @throws InvalidConfigException
* @throws \yii\di\NotInstantiableException
*/
private function getTransportFactory(): Transport
{
if (isset($this->transportFactory)) {
return $this->transportFactory;
}
/** @var LoggerInterface|null $logger */
$logger = class_exists(DynamicLogger::class) ? new DynamicLogger() : null;
$defaultFactories = Transport::getDefaultFactories(new EventDispatcherProxy($this), null, $logger);
// Use the Yii DI container, if available.
if (isset(\Yii::$container)) {
$factories = [];
foreach ([
NullTransportFactory::class,
SendmailTransportFactory::class,
EsmtpTransportFactory::class,
NativeTransportFactory::class,
SesTransportFactory::class,
GmailTransportFactory::class,
InfobipTransportFactory::class,
MandrillTransportFactory::class,
MailgunTransportFactory::class,
MailjetTransportFactory::class,
OhMySmtpTransportFactory::class,
PostmarkTransportFactory::class,
SendgridTransportFactory::class,
SendinblueTransportFactory::class,
] as $factoryClass) {
if (!class_exists($factoryClass)) {
continue;
}
$factories[] = \Yii::$container->get($factoryClass);
}
} else {
$factories = Transport::getDefaultFactories();
}

/** @psalm-suppress InvalidArgument Symfony's type annotation is wrong */
return new Transport($defaultFactories);
return new Transport($factories);
}

/**
Expand Down Expand Up @@ -107,6 +153,10 @@ private function createTransport(array $config = []): TransportInterface
return $transport;
}

/**
* @param MessageWrapperInterface&MessageInterface $message
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
*/
protected function sendMessage($message): bool
{
if (!($message instanceof MessageWrapperInterface)) {
Expand Down
24 changes: 24 additions & 0 deletions tests/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace yiiunit\extensions\symfonymailer;

use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\di\Container;
use yii\mail\MessageInterface;
use yii\symfonymailer\Mailer;
use yii\symfonymailer\Message;
Expand Down Expand Up @@ -164,4 +166,26 @@ public function testSendMessageThrowsOnBadMessageType(): void

$mailer->send($message);
}

public function testDiContainer(): void
{
\Yii::$container = new Container();

$dispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
$dispatcherCreator = $this
->getMockBuilder(\stdclass::class)
->addMethods(['__invoke'])
->getMock();

$dispatcherCreator
->expects(self::atLeastOnce())
->method('__invoke')->willReturn($dispatcherMock);
\Yii::$container->set(EventDispatcherInterface::class, $dispatcherCreator);


$mailer = new Mailer();
$mailer->setTransport([
'dsn' => 'null://null',
]);
}
}
Loading