Skip to content

Commit

Permalink
SymfonyMailCollector support
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Sep 8, 2023
1 parent 27b088a commit d4f9bc2
Show file tree
Hide file tree
Showing 5 changed files with 46 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.18.3",
"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
51 changes: 40 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 @@ -455,16 +459,41 @@ function ($event, $params) use ($queryCollector) {
}
}

if ($this->shouldCollect('mail', true) && class_exists('Illuminate\Mail\MailServiceProvider') && $this->checkVersion('9.0', '<')) {
if ($this->shouldCollect('mail', true) && isset($this->app['events']) && class_exists('Illuminate\Mail\MailServiceProvider')) {
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->app['events']->listen(function (MessageSent $event) use (&$mailCollector) {
$mailCollector->addSymfonyMessage($event->sent->getSymfonySentMessage());
});
$this->addCollector($mailCollector);

if ($this->app['config']->get('debugbar.options.mail.full_log')) {
$mailCollector->showMessageDetail();
}

if ($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->measure(
'mail: '. Str::limit($message->getSubject(), 100),
function () use ($message, $envelope) {
return $this->originalTransport->send($message, $envelope);
}
);
}
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 d4f9bc2

Please sign in to comment.