Skip to content

Commit e3d1851

Browse files
committed
feature #32912 [Mailer] Add support for the profiler (fabpot)
This PR was merged into the 4.4 branch. Discussion ---------- [Mailer] Add support for the profiler | 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 | closes #31592 | License | MIT | Doc PR | n/a Web profiler for the Mailer. Commits ------- f152314e28 [Mailer] added support for the profiler
2 parents cc3c6a0 + d13689e commit e3d1851

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

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+
* Added `MessageDataCollector`
78
* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
89
* [BC BREAK] `TransportInterface` has a new `getName()` method
910
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\DataCollector;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Component\Mailer\Event\MessageEvents;
18+
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
19+
20+
/**
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class MessageDataCollector extends DataCollector
24+
{
25+
private $events;
26+
27+
public function __construct(MessageLoggerListener $logger)
28+
{
29+
$this->events = $logger->getEvents();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function collect(Request $request, Response $response, \Exception $exception = null)
36+
{
37+
$this->data['events'] = $this->events;
38+
}
39+
40+
public function getEvents(): MessageEvents
41+
{
42+
return $this->data['events'];
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function reset()
49+
{
50+
$this->data = [];
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function getName()
57+
{
58+
return 'mailer';
59+
}
60+
}

Event/MessageEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Mailer\Event;
1313

14+
use Symfony\Component\Mime\RawMessage;
15+
1416
/**
1517
* @author Fabien Potencier <[email protected]>
1618
*/
@@ -48,4 +50,18 @@ public function getEvents(string $name = null): array
4850

4951
return $events;
5052
}
53+
54+
/**
55+
* @return RawMessage[]
56+
*/
57+
public function getMessages(string $name = null): array
58+
{
59+
$events = $this->getEvents($name);
60+
$messages = [];
61+
foreach ($events as $event) {
62+
$messages[] = $event->getMessage();
63+
}
64+
65+
return $messages;
66+
}
5167
}

MessageDataCollector.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\DataCollector;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
17+
use Symfony\Component\Mailer\Event\MessageEvents;
18+
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
19+
20+
/**
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
class MessageDataCollector extends DataCollector
24+
{
25+
private $events;
26+
27+
public function __construct(MessageLoggerListener $logger)
28+
{
29+
$this->events = $logger->getEvents();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function collect(Request $request, Response $response, \Exception $exception = null)
36+
{
37+
$this->data['events'] = $this->events;
38+
}
39+
40+
public function getEvents(): MessageEvents
41+
{
42+
return $this->data['events'];
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function reset()
49+
{
50+
$this->data = [];
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function getName()
57+
{
58+
return 'mailer';
59+
}
60+
}

MessageEvents.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Mailer\Event;
1313

14+
use Symfony\Component\Mime\RawMessage;
15+
1416
/**
1517
* @author Fabien Potencier <[email protected]>
1618
*/
@@ -48,4 +50,18 @@ public function getEvents(string $name = null): array
4850

4951
return $events;
5052
}
53+
54+
/**
55+
* @return RawMessage[]
56+
*/
57+
public function getMessages(string $name = null): array
58+
{
59+
$events = $this->getEvents($name);
60+
$messages = [];
61+
foreach ($events as $event) {
62+
$messages[] = $event->getMessage();
63+
}
64+
65+
return $messages;
66+
}
5167
}

0 commit comments

Comments
 (0)