1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Swift_Message creation is now lazy when using callback

Swift_Message object is not created until right before the message is
sent, if it is sent at all. This prevents the expensive Swift_Mailer
autoloader from being invoked.

Swift_Mailer is now also required to run test suite
This commit is contained in:
John Kary
2015-03-01 19:25:14 -06:00
parent 1b34cd4d88
commit f5eaeeb144
3 changed files with 64 additions and 10 deletions

View File

@@ -23,7 +23,8 @@
"ruflin/elastica": "0.90.*",
"doctrine/couchdb": "~1.0@dev",
"aws/aws-sdk-php": "~2.4, >2.4.8",
"videlalvaro/php-amqplib": "~2.4"
"videlalvaro/php-amqplib": "~2.4",
"swiftmailer/swiftmailer": "~5.3"
},
"suggest": {
"graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",

View File

@@ -32,13 +32,8 @@ class SwiftMailerHandler extends MailHandler
public function __construct(\Swift_Mailer $mailer, $message, $level = Logger::ERROR, $bubble = true)
{
parent::__construct($level, $bubble);
$this->mailer = $mailer;
if (!$message instanceof \Swift_Message && is_callable($message)) {
$message = call_user_func($message);
}
if (!$message instanceof \Swift_Message) {
throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callable returning it');
}
$this->mailer = $mailer;
$this->message = $message;
}
@@ -47,10 +42,31 @@ class SwiftMailerHandler extends MailHandler
*/
protected function send($content, array $records)
{
$message = clone $this->message;
$this->mailer->send($this->buildMessage($content));
}
/**
* Creates instance of Swift_Message to be sent
*
* @param string $content
* @return \Swift_Message
*/
protected function buildMessage($content)
{
$message = null;
if ($this->message instanceof \Swift_Message) {
$message = clone $this->message;
} else if (is_callable($this->message)) {
$message = call_user_func($this->message);
}
if (!$message instanceof \Swift_Message) {
throw new \InvalidArgumentException('Could not resolve message as instance of Swift_Message or a callable returning it');
}
$message->setBody($content);
$message->setDate(time());
$this->mailer->send($message);
return $message;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\TestCase;
class SwiftMailerHandlerTest extends TestCase
{
/** @var \Swift_Mailer|\PHPUnit_Framework_MockObject_MockObject */
private $mailer;
public function setUp()
{
$this->mailer = $this
->getMockBuilder('Swift_Mailer')
->disableOriginalConstructor()
->getMock();
}
public function testMessageCreationIsLazyWhenUsingCallback()
{
$this->mailer->expects($this->never())
->method('send');
$callback = function () {
throw new \RuntimeException('Swift_Message creation callback should not have been called in this test');
};
$handler = new SwiftMailerHandler($this->mailer, $callback);
$records = [
$this->getRecord(Logger::DEBUG),
$this->getRecord(Logger::INFO),
];
$handler->handleBatch($records);
}
}