Skip to content

Commit

Permalink
SymfonyMailCollector support (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 authored Sep 19, 2023
1 parent 8a3bab7 commit b736241
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 41 additions & 11 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/Support/Clockwork/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down

0 comments on commit b736241

Please sign in to comment.