From b7362410434d7f1155e0729e3a75c83384d3b699 Mon Sep 17 00:00:00 2001 From: erikn69 Date: Tue, 19 Sep 2023 15:22:07 -0500 Subject: [PATCH] SymfonyMailCollector support (#1457) --- composer.json | 2 +- config/debugbar.php | 1 + readme.md | 2 +- src/LaravelDebugbar.php | 52 +++++++++++++++++++++++------ src/Support/Clockwork/Converter.php | 6 ++-- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index f1dc1ee4..052fdd3f 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": "^8.0", - "maximebf/debugbar": "^1.18.2", + "maximebf/debugbar": "^1.19", "illuminate/routing": "^9|^10", "illuminate/session": "^9|^10", "illuminate/support": "^9|^10", diff --git a/config/debugbar.php b/config/debugbar.php index 6b688786..c1778963 100644 --- a/config/debugbar.php +++ b/config/debugbar.php @@ -206,6 +206,7 @@ 'slow_threshold' => false, // Only track queries that last longer than this time in ms ], 'mail' => [ + 'timeline' => false, // Add mails to the timeline 'full_log' => false, ], 'views' => [ diff --git a/readme.md b/readme.md index a747891a..f11a0261 100644 --- a/readme.md +++ b/readme.md @@ -30,7 +30,7 @@ This package includes some custom collectors: Bootstraps the following collectors for Laravel: - LogCollector: Show all Log messages - - SwiftMailCollector and SwiftLogCollector for Mail + - SymfonyMailCollector for Mail And the default collectors: - PhpInfoCollector diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index a0642eac..2213f269 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -18,8 +18,7 @@ use Barryvdh\Debugbar\Storage\SocketStorage; use Barryvdh\Debugbar\Storage\FilesystemStorage; use DebugBar\Bridge\MonologCollector; -use DebugBar\Bridge\SwiftMailer\SwiftLogCollector; -use DebugBar\Bridge\SwiftMailer\SwiftMailCollector; +use DebugBar\Bridge\Symfony\SymfonyMailCollector; use DebugBar\DataCollector\ConfigCollector; use DebugBar\DataCollector\DataCollectorInterface; use DebugBar\DataCollector\ExceptionsCollector; @@ -36,10 +35,15 @@ use Exception; use Throwable; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Mail\Events\MessageSent; use Illuminate\Session\SessionManager; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Mailer\Envelope; +use Symfony\Component\Mailer\SentMessage; +use Symfony\Component\Mailer\Transport\AbstractTransport; +use Symfony\Component\Mime\RawMessage; /** * Debug bar subclass which adds all without Request and with LaravelCollector. @@ -440,16 +444,42 @@ function ($event, $params) use ($queryCollector) { } } - if ($this->shouldCollect('mail', true) && class_exists('Illuminate\Mail\MailServiceProvider') && $this->checkVersion('9.0', '<')) { + if ($this->shouldCollect('mail', true) && class_exists('Illuminate\Mail\MailServiceProvider') && isset($this->app['events'])) { try { - $mailer = $this->app['mailer']->getSwiftMailer(); - $this->addCollector(new SwiftMailCollector($mailer)); - if ( - $this->app['config']->get('debugbar.options.mail.full_log') && $this->hasCollector( - 'messages' - ) - ) { - $this['messages']->aggregate(new SwiftLogCollector($mailer)); + $mailCollector = new SymfonyMailCollector(); + $this->addCollector($mailCollector); + $this->app['events']->listen(function (MessageSent $event) use ($mailCollector) { + $mailCollector->addSymfonyMessage($event->sent->getSymfonySentMessage()); + }); + + if ($this->app['config']->get('debugbar.options.mail.full_log')) { + $mailCollector->showMessageDetail(); + } + + if ($debugbar->hasCollector('time') && $this->app['config']->get('debugbar.options.mail.timeline')) { + $transport = $this->app['mailer']->getSymfonyTransport(); + $this->app['mailer']->setSymfonyTransport(new class($transport, $this) extends AbstractTransport{ + private $originalTransport; + private $laravelDebugbar; + + public function __construct($transport, $laravelDebugbar) + { + $this->originalTransport = $transport; + $this->laravelDebugbar = $laravelDebugbar; + } + public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage + { + return $this->laravelDebugbar['time']->measure( + 'mail: '. Str::limit($message->getSubject(), 100), + function () use ($message, $envelope) { + return $this->originalTransport->send($message, $envelope); + }, + 'mail' + ); + } + protected function doSend(SentMessage $message): void {} + public function __toString(): string{ $this->originalTransport->__toString(); } + }); } } catch (\Exception $e) { $this->addThrowable( diff --git a/src/Support/Clockwork/Converter.php b/src/Support/Clockwork/Converter.php index dbd4c05f..521c32c8 100644 --- a/src/Support/Clockwork/Converter.php +++ b/src/Support/Clockwork/Converter.php @@ -122,11 +122,11 @@ public function convert($data) } } - if (isset($data['swiftmailer_mails'])) { - foreach ($data['swiftmailer_mails']['mails'] as $mail) { + if (isset($data['symfonymailer_mails'])) { + foreach ($data['symfonymailer_mails']['mails'] as $mail) { $output['emailsData'][] = [ 'data' => [ - 'to' => $mail['to'], + 'to' => implode(', ', $mail['to']), 'subject' => $mail['subject'], 'headers' => isset($mail['headers']) ? explode("\n", $mail['headers']) : null, ],