Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37265de

Browse files
committed
feature #32916 [Mailer] Add a name to the transports (fabpot)
This PR was merged into the 4.4 branch. Discussion ---------- [Mailer] Add a name to the transports | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | n/a Having a name for Transports helps identify them (useful for instance in the profiler when one uses several mailers). Commits ------- 2412dfe [Mailer] added a name to the transport
2 parents 1ce83da + 2412dfe commit 37265de

25 files changed

+191
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInt
4242
$this->onDoSend = $onDoSend;
4343
}
4444

45+
public function getName(): string
46+
{
47+
return 'dummy://local';
48+
}
49+
4550
protected function doSend(SentMessage $message): void
4651
{
4752
$onDoSend = $this->onDoSend;

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"symfony/dom-crawler": "<4.3",
7575
"symfony/form": "<4.3",
7676
"symfony/lock": "<4.4",
77+
"symfony/mailer": "<4.4",
7778
"symfony/messenger": "<4.3",
7879
"symfony/property-info": "<3.4",
7980
"symfony/serializer": "<4.2",

src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function __construct(string $accessKey, string $secretKey, string $region
4343
parent::__construct($client, $dispatcher, $logger);
4444
}
4545

46+
public function getName(): string
47+
{
48+
return sprintf('api://%s@ses?region=%s', $this->accessKey, $this->region);
49+
}
50+
4651
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4752
{
4853
$date = gmdate('D, d M Y H:i:s e');

src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function __construct(string $accessKey, string $secretKey, string $region
4242
parent::__construct($client, $dispatcher, $logger);
4343
}
4444

45+
public function getName(): string
46+
{
47+
return sprintf('http://%s@ses?region=%s', $this->accessKey, $this->region);
48+
}
49+
4550
protected function doSendHttp(SentMessage $message): ResponseInterface
4651
{
4752
$date = gmdate('D, d M Y H:i:s e');

src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3636
parent::__construct($client, $dispatcher, $logger);
3737
}
3838

39+
public function getName(): string
40+
{
41+
return sprintf('api://mandrill');
42+
}
43+
3944
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4045
{
4146
$response = $this->client->request('POST', self::ENDPOINT, [

src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3434
parent::__construct($client, $dispatcher, $logger);
3535
}
3636

37+
public function getName(): string
38+
{
39+
return sprintf('http://mandrill');
40+
}
41+
3742
protected function doSendHttp(SentMessage $message): ResponseInterface
3843
{
3944
$envelope = $message->getEnvelope();

src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function __construct(string $key, string $domain, string $region = null,
4141
parent::__construct($client, $dispatcher, $logger);
4242
}
4343

44+
public function getName(): string
45+
{
46+
return sprintf('api://%s@mailgun?region=%s', $this->domain, $this->region);
47+
}
48+
4449
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4550
{
4651
$body = new FormDataPart($this->getPayload($email, $envelope));

src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function __construct(string $key, string $domain, string $region = null,
4040
parent::__construct($client, $dispatcher, $logger);
4141
}
4242

43+
public function getName(): string
44+
{
45+
return sprintf('http://%s@mailgun?region=%s', $this->domain, $this->region);
46+
}
47+
4348
protected function doSendHttp(SentMessage $message): ResponseInterface
4449
{
4550
$body = new FormDataPart([

src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3636
parent::__construct($client, $dispatcher, $logger);
3737
}
3838

39+
public function getName(): string
40+
{
41+
return sprintf('api://postmark');
42+
}
43+
3944
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4045
{
4146
$response = $this->client->request('POST', self::ENDPOINT, [

src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
3737
parent::__construct($client, $dispatcher, $logger);
3838
}
3939

40+
public function getName(): string
41+
{
42+
return sprintf('api://sendgrid');
43+
}
44+
4045
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
4146
{
4247
$response = $this->client->request('POST', self::ENDPOINT, [

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* [BC BREAK] `TransportInterface` has a new `getName()` method
78
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.
89
* [BC BREAK] Transports depend on `Symfony\Contracts\EventDispatcher\EventDispatcherInterface`
910
instead of `Symfony\Component\EventDispatcher\EventDispatcherInterface`.

src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function testCreate(Dsn $dsn, TransportInterface $transport): void
6969
$factory = $this->getFactory();
7070

7171
$this->assertEquals($transport, $factory->create($dsn));
72+
if ('smtp' !== $dsn->getScheme()) {
73+
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', $transport->getName());
74+
}
7275
}
7376

7477
/**

src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public function testSendNoTransports()
2929
new FailoverTransport([]);
3030
}
3131

32+
public function testGetName()
33+
{
34+
$t1 = $this->createMock(TransportInterface::class);
35+
$t1->expects($this->once())->method('getName')->willReturn('t1://local');
36+
$t2 = $this->createMock(TransportInterface::class);
37+
$t2->expects($this->once())->method('getName')->willReturn('t2://local');
38+
$t = new FailoverTransport([$t1, $t2]);
39+
$this->assertEquals('t1://local || t2://local', $t->getName());
40+
}
41+
3242
public function testSendFirstWork()
3343
{
3444
$t1 = $this->createMock(TransportInterface::class);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Transport;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Transport\NullTransport;
16+
17+
class NullTransportTest extends TestCase
18+
{
19+
public function testName()
20+
{
21+
$t = new NullTransport();
22+
$this->assertEquals('smtp://null', $t->getName());
23+
}
24+
}

src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public function testSendNoTransports()
2828
new RoundRobinTransport([]);
2929
}
3030

31+
public function testGetName()
32+
{
33+
$t1 = $this->createMock(TransportInterface::class);
34+
$t1->expects($this->once())->method('getName')->willReturn('t1://local');
35+
$t2 = $this->createMock(TransportInterface::class);
36+
$t2->expects($this->once())->method('getName')->willReturn('t2://local');
37+
$t = new RoundRobinTransport([$t1, $t2]);
38+
$this->assertEquals('t1://local && t2://local', $t->getName());
39+
}
40+
3141
public function testSendAlternate()
3242
{
3343
$t1 = $this->createMock(TransportInterface::class);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Transport;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Transport\SendmailTransport;
16+
17+
class SendmailTransportTest extends TestCase
18+
{
19+
public function testName()
20+
{
21+
$t = new SendmailTransport();
22+
$this->assertEquals('smtp://sendmail', $t->getName());
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Transport\Smtp;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
16+
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
17+
18+
class SmtpTransportTest extends TestCase
19+
{
20+
public function testName()
21+
{
22+
$t = new SmtpTransport();
23+
$this->assertEquals('smtp://localhost:25', $t->getName());
24+
25+
$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525));
26+
$this->assertEquals('smtp://127.0.0.1:2525', $t->getName());
27+
}
28+
}

src/Symfony/Component/Mailer/Tests/TransportTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
6868
{
6969
throw new \BadMethodCallException('This method newer should be called.');
7070
}
71+
72+
public function getName(): string
73+
{
74+
return sprintf('dummy://local');
75+
}
7176
}
7277

7378
class DummyTransportFactory implements Transport\TransportFactoryInterface

src/Symfony/Component/Mailer/Transport/AbstractTransport.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function __construct(EventDispatcherInterface $dispatcher = null, LoggerI
3939
$this->logger = $logger ?: new NullLogger();
4040
}
4141

42+
abstract public function getName(): string;
43+
4244
/**
4345
* Sets the maximum number of messages to send per second (0 to disable).
4446
*/

src/Symfony/Component/Mailer/Transport/FailoverTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ protected function getNextTransport(): ?TransportInterface
2828

2929
return $this->currentTransport;
3030
}
31+
32+
protected function getNameSymbol(): string
33+
{
34+
return '||';
35+
}
3136
}

src/Symfony/Component/Mailer/Transport/NullTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ final class NullTransport extends AbstractTransport
2323
protected function doSend(SentMessage $message): void
2424
{
2525
}
26+
27+
public function getName(): string
28+
{
29+
return 'smtp://null';
30+
}
2631
}

src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
5656
throw new TransportException('All transports failed.');
5757
}
5858

59+
public function getName(): string
60+
{
61+
return implode(' '.$this->getNameSymbol().' ', array_map(function (TransportInterface $transport) {
62+
return $transport->getName();
63+
}, $this->transports));
64+
}
65+
5966
/**
6067
* Rotates the transport list around and returns the first instance.
6168
*/
@@ -90,6 +97,11 @@ protected function isTransportDead(TransportInterface $transport): bool
9097
return $this->deadTransports->contains($transport);
9198
}
9299

100+
protected function getNameSymbol(): string
101+
{
102+
return '&&';
103+
}
104+
93105
private function moveCursor(int $cursor): int
94106
{
95107
return ++$cursor >= \count($this->transports) ? 0 : $cursor;

src/Symfony/Component/Mailer/Transport/SendmailTransport.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
7373
return parent::send($message, $envelope);
7474
}
7575

76+
public function getName(): string
77+
{
78+
return $this->transport->getName();
79+
}
80+
7681
protected function doSend(SentMessage $message): void
7782
{
7883
$this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
126126
return $message;
127127
}
128128

129+
public function getName(): string
130+
{
131+
if ($this->stream instanceof SocketStream) {
132+
return sprintf('smtp://%s:%d', $this->stream->getHost(), $this->stream->getPort());
133+
}
134+
135+
return sprintf('smtp://sendmail');
136+
}
137+
129138
/**
130139
* Runs a command against the stream, expecting the given response codes.
131140
*

src/Symfony/Component/Mailer/Transport/TransportInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ interface TransportInterface
3030
* @throws TransportExceptionInterface
3131
*/
3232
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage;
33+
34+
public function getName(): string;
3335
}

0 commit comments

Comments
 (0)