Skip to content

Commit b736241

Browse files
authored
SymfonyMailCollector support (#1457)
1 parent 8a3bab7 commit b736241

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "^8.0",
14-
"maximebf/debugbar": "^1.18.2",
14+
"maximebf/debugbar": "^1.19",
1515
"illuminate/routing": "^9|^10",
1616
"illuminate/session": "^9|^10",
1717
"illuminate/support": "^9|^10",

config/debugbar.php

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
'slow_threshold' => false, // Only track queries that last longer than this time in ms
207207
],
208208
'mail' => [
209+
'timeline' => false, // Add mails to the timeline
209210
'full_log' => false,
210211
],
211212
'views' => [

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This package includes some custom collectors:
3030

3131
Bootstraps the following collectors for Laravel:
3232
- LogCollector: Show all Log messages
33-
- SwiftMailCollector and SwiftLogCollector for Mail
33+
- SymfonyMailCollector for Mail
3434

3535
And the default collectors:
3636
- PhpInfoCollector

src/LaravelDebugbar.php

+41-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
use Barryvdh\Debugbar\Storage\SocketStorage;
1919
use Barryvdh\Debugbar\Storage\FilesystemStorage;
2020
use DebugBar\Bridge\MonologCollector;
21-
use DebugBar\Bridge\SwiftMailer\SwiftLogCollector;
22-
use DebugBar\Bridge\SwiftMailer\SwiftMailCollector;
21+
use DebugBar\Bridge\Symfony\SymfonyMailCollector;
2322
use DebugBar\DataCollector\ConfigCollector;
2423
use DebugBar\DataCollector\DataCollectorInterface;
2524
use DebugBar\DataCollector\ExceptionsCollector;
@@ -36,10 +35,15 @@
3635
use Exception;
3736
use Throwable;
3837
use Illuminate\Contracts\Foundation\Application;
38+
use Illuminate\Mail\Events\MessageSent;
3939
use Illuminate\Session\SessionManager;
4040
use Illuminate\Support\Str;
4141
use Symfony\Component\HttpFoundation\Request;
4242
use Symfony\Component\HttpFoundation\Response;
43+
use Symfony\Component\Mailer\Envelope;
44+
use Symfony\Component\Mailer\SentMessage;
45+
use Symfony\Component\Mailer\Transport\AbstractTransport;
46+
use Symfony\Component\Mime\RawMessage;
4347

4448
/**
4549
* Debug bar subclass which adds all without Request and with LaravelCollector.
@@ -440,16 +444,42 @@ function ($event, $params) use ($queryCollector) {
440444
}
441445
}
442446

443-
if ($this->shouldCollect('mail', true) && class_exists('Illuminate\Mail\MailServiceProvider') && $this->checkVersion('9.0', '<')) {
447+
if ($this->shouldCollect('mail', true) && class_exists('Illuminate\Mail\MailServiceProvider') && isset($this->app['events'])) {
444448
try {
445-
$mailer = $this->app['mailer']->getSwiftMailer();
446-
$this->addCollector(new SwiftMailCollector($mailer));
447-
if (
448-
$this->app['config']->get('debugbar.options.mail.full_log') && $this->hasCollector(
449-
'messages'
450-
)
451-
) {
452-
$this['messages']->aggregate(new SwiftLogCollector($mailer));
449+
$mailCollector = new SymfonyMailCollector();
450+
$this->addCollector($mailCollector);
451+
$this->app['events']->listen(function (MessageSent $event) use ($mailCollector) {
452+
$mailCollector->addSymfonyMessage($event->sent->getSymfonySentMessage());
453+
});
454+
455+
if ($this->app['config']->get('debugbar.options.mail.full_log')) {
456+
$mailCollector->showMessageDetail();
457+
}
458+
459+
if ($debugbar->hasCollector('time') && $this->app['config']->get('debugbar.options.mail.timeline')) {
460+
$transport = $this->app['mailer']->getSymfonyTransport();
461+
$this->app['mailer']->setSymfonyTransport(new class($transport, $this) extends AbstractTransport{
462+
private $originalTransport;
463+
private $laravelDebugbar;
464+
465+
public function __construct($transport, $laravelDebugbar)
466+
{
467+
$this->originalTransport = $transport;
468+
$this->laravelDebugbar = $laravelDebugbar;
469+
}
470+
public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
471+
{
472+
return $this->laravelDebugbar['time']->measure(
473+
'mail: '. Str::limit($message->getSubject(), 100),
474+
function () use ($message, $envelope) {
475+
return $this->originalTransport->send($message, $envelope);
476+
},
477+
'mail'
478+
);
479+
}
480+
protected function doSend(SentMessage $message): void {}
481+
public function __toString(): string{ $this->originalTransport->__toString(); }
482+
});
453483
}
454484
} catch (\Exception $e) {
455485
$this->addThrowable(

src/Support/Clockwork/Converter.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ public function convert($data)
122122
}
123123
}
124124

125-
if (isset($data['swiftmailer_mails'])) {
126-
foreach ($data['swiftmailer_mails']['mails'] as $mail) {
125+
if (isset($data['symfonymailer_mails'])) {
126+
foreach ($data['symfonymailer_mails']['mails'] as $mail) {
127127
$output['emailsData'][] = [
128128
'data' => [
129-
'to' => $mail['to'],
129+
'to' => implode(', ', $mail['to']),
130130
'subject' => $mail['subject'],
131131
'headers' => isset($mail['headers']) ? explode("\n", $mail['headers']) : null,
132132
],

0 commit comments

Comments
 (0)