mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-01-16 13:00:42 +01:00
Add SymfonyMailCollector (#554)
* Add SymfonyMailCollector * add examples for SymfonyMailCollector
This commit is contained in:
parent
17ec0485bd
commit
8f6045d465
@ -1,4 +1,8 @@
|
||||
# Changelog
|
||||
2023-09-08
|
||||
|
||||
- Add SymfonyMailCollector (#554)
|
||||
|
||||
2021-12-21
|
||||
|
||||
- Add support for `symfony/var-dumper^6` package
|
||||
|
@ -27,6 +27,7 @@ Includes collectors for:
|
||||
- [Monolog](https://github.com/Seldaek/monolog)
|
||||
- [Propel](http://propelorm.org/)
|
||||
- [Slim](http://slimframework.com)
|
||||
- [Symfony Mailer](https://symfony.com/doc/current/mailer.html)
|
||||
- [Swift Mailer](http://swiftmailer.org/)
|
||||
- [Twig](http://twig.symfony.com/)
|
||||
|
||||
|
6
demo/bridge/symfonymailer/composer.json
Normal file
6
demo/bridge/symfonymailer/composer.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"require": {
|
||||
"symfony/event-dispatcher": "*",
|
||||
"symfony/mailer": "*"
|
||||
}
|
||||
}
|
48
demo/bridge/symfonymailer/index.php
Normal file
48
demo/bridge/symfonymailer/index.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
use DebugBar\Bridge\Symfony\SymfonyMailCollector;
|
||||
use DebugBar\DataCollector\MessagesCollector;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\Mailer\Event\SentMessageEvent;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
use Symfony\Component\Mailer\Transport\AbstractTransport;
|
||||
use Symfony\Component\Mime\Email;
|
||||
|
||||
include __DIR__ . '/vendor/autoload.php';
|
||||
include __DIR__ . '/../../bootstrap.php';
|
||||
|
||||
$debugbarRenderer->setBaseUrl('../../../src/DebugBar/Resources');
|
||||
|
||||
$mailCollector = new SymfonyMailCollector();
|
||||
$debugbar->addCollector($mailCollector);
|
||||
$logger = new MessagesCollector('mails');
|
||||
$debugbar['messages']->aggregate($logger);
|
||||
|
||||
// Add even listener for SentMessageEvent
|
||||
$dispatcher = new EventDispatcher();
|
||||
$dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) use ($mailCollector): void {
|
||||
$mailCollector->addSymfonyMessage($event->getMessage());
|
||||
});
|
||||
|
||||
// Creates NullTransport Mailer for testing
|
||||
$mailer = new Mailer(new class ($dispatcher, $logger) extends AbstractTransport {
|
||||
protected function doSend(\Symfony\Component\Mailer\SentMessage $message): void
|
||||
{
|
||||
$this->getLogger()->debug('Sending message "'.$message->getOriginalMessage()->getSubject().'"');
|
||||
}
|
||||
public function __toString(): string{ return 'null://'; }
|
||||
});
|
||||
|
||||
$email = (new Email())
|
||||
->from('john@doe.com')
|
||||
->to('you@example.com')
|
||||
//->cc('cc@example.com')
|
||||
//->bcc('bcc@example.com')
|
||||
//->replyTo('fabien@example.com')
|
||||
//->priority(Email::PRIORITY_HIGH)
|
||||
->subject('Wonderful Subject')
|
||||
->text('Here is the message itself');
|
||||
|
||||
$mailer->send($email);
|
||||
|
||||
render_demo_page();
|
@ -42,6 +42,7 @@ render_demo_page(function() {
|
||||
<li><a href="bridge/propel">Propel</a></li>
|
||||
<li><a href="bridge/slim">Slim</a></li>
|
||||
<li><a href="bridge/swiftmailer">Swift mailer</a></li>
|
||||
<li><a href="bridge/symfonymailer">Symfony mailer</a></li>
|
||||
<li><a href="bridge/twig">Twig</a></li>
|
||||
</ul>
|
||||
<?php
|
||||
|
@ -86,6 +86,20 @@ Display log messages and sent mail using `DebugBar\Bridge\SwiftMailer\SwiftLogCo
|
||||
$debugbar['messages']->aggregate(new DebugBar\Bridge\SwiftMailer\SwiftLogCollector($mailer));
|
||||
$debugbar->addCollector(new DebugBar\Bridge\SwiftMailer\SwiftMailCollector($mailer));
|
||||
|
||||
## Symfony Mailer
|
||||
|
||||
https://symfony.com/doc/current/mailer.html
|
||||
|
||||
Display log messages and sent mail using `DebugBar\Bridge\Symfony\SymfonyMailCollector`
|
||||
|
||||
use Symfony\Component\Mailer\Event\SentMessageEvent;
|
||||
|
||||
$mailCollector = new DebugBar\Bridge\Symfony\SymfonyMailCollector();
|
||||
$debugbar->addCollector($mailCollector);
|
||||
$eventDispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) use (&$mailCollector): void {
|
||||
$mailCollector->addSymfonyMessage($event->getMessage());
|
||||
});
|
||||
|
||||
## Twig
|
||||
|
||||
http://twig.sensiolabs.org/
|
||||
|
84
src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php
Normal file
84
src/DebugBar/Bridge/Symfony/SymfonyMailCollector.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace DebugBar\Bridge\Symfony;
|
||||
|
||||
use DebugBar\DataCollector\AssetProvider;
|
||||
use DebugBar\DataCollector\DataCollector;
|
||||
use DebugBar\DataCollector\Renderable;
|
||||
|
||||
/**
|
||||
* Collects data about sent mail events
|
||||
*
|
||||
* https://github.com/symfony/mailer
|
||||
*/
|
||||
class SymfonyMailCollector extends DataCollector implements Renderable, AssetProvider
|
||||
{
|
||||
/** @var array */
|
||||
private $messages = array();
|
||||
|
||||
/** @var bool */
|
||||
private $showDetailed = false;
|
||||
|
||||
/** @param \Symfony\Component\Mailer\SentMessage $message */
|
||||
public function addSymfonyMessage($message)
|
||||
{
|
||||
$this->messages[] = $message->getOriginalMessage();
|
||||
}
|
||||
|
||||
public function showMessageDetail()
|
||||
{
|
||||
$this->showDetailed = true;
|
||||
}
|
||||
|
||||
public function collect()
|
||||
{
|
||||
$mails = array();
|
||||
|
||||
foreach ($this->messages as $message) {
|
||||
/* @var \Symfony\Component\Mime\Message $message */
|
||||
$mails[] = array(
|
||||
'to' => array_map(function ($address) {
|
||||
/* @var \Symfony\Component\Mime\Address $address */
|
||||
return $address->toString();
|
||||
}, $message->getTo()),
|
||||
'subject' => $message->getSubject(),
|
||||
'headers' => ($this->showDetailed ? $message : $message->getHeaders())->toString(),
|
||||
);;
|
||||
}
|
||||
|
||||
return array(
|
||||
'count' => count($mails),
|
||||
'mails' => $mails,
|
||||
);
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'symfonymailer_mails';
|
||||
}
|
||||
|
||||
public function getWidgets()
|
||||
{
|
||||
return array(
|
||||
'emails' => array(
|
||||
'icon' => 'inbox',
|
||||
'widget' => 'PhpDebugBar.Widgets.MailsWidget',
|
||||
'map' => 'symfonymailer_mails.mails',
|
||||
'default' => '[]',
|
||||
'title' => 'Mails'
|
||||
),
|
||||
'emails:badge' => array(
|
||||
'map' => 'symfonymailer_mails.count',
|
||||
'default' => 'null'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function getAssets()
|
||||
{
|
||||
return array(
|
||||
'css' => 'widgets/mails/widget.css',
|
||||
'js' => 'widgets/mails/widget.js'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user