FIX Use TransportFactory to prevent infinite config loop

This commit is contained in:
Steve Boyd 2023-08-01 10:06:47 +12:00
parent 067a3e25b8
commit c58bc0a7ff
2 changed files with 22 additions and 12 deletions

View File

@ -11,18 +11,7 @@ SilverStripe\Core\Injector\Injector:
calls: calls:
- [addSubscriber, ['%$SilverStripe\Control\Email\MailerSubscriber']] - [addSubscriber, ['%$SilverStripe\Control\Email\MailerSubscriber']]
Symfony\Component\Mailer\Transport\TransportInterface: Symfony\Component\Mailer\Transport\TransportInterface:
factory: Symfony\Component\Mailer\Transport factory: SilverStripe\Control\Email\TransportFactory
factory_method: fromDsn
constructor: constructor:
dsn: 'sendmail://default' dsn: 'sendmail://default'
dispatcher: '%$Symfony\Component\EventDispatcher\EventDispatcherInterface.mailer' dispatcher: '%$Symfony\Component\EventDispatcher\EventDispatcherInterface.mailer'
---
Name: mailer-dsn-env
After: '*'
Only:
envvarset: MAILER_DSN
---
SilverStripe\Core\Injector\Injector:
Symfony\Component\Mailer\Transport\TransportInterface:
constructor:
dsn: '`MAILER_DSN`'

View File

@ -0,0 +1,21 @@
<?php
namespace SilverStripe\Control\Email;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Factory;
use Symfony\Component\Mailer\Transport;
/**
* Creates an email transport from a DSN string
* A DSN defined in an environment variable has priority over a DSN defined in yml config file
*/
class TransportFactory implements Factory
{
public function create($service, array $params = [])
{
$dsn = Environment::getEnv('MAILER_DSN') ?: $params['dsn'];
$dispatcher = $params['dispatcher'];
return Transport::fromDsn($dsn, $dispatcher);
}
}