Skip to content

Commit a6494a9

Browse files
committed
Merge pull request #134 from weaverryan/swift_performance
Adding a Swift MessageFactory for performance
2 parents 3cf6864 + a673353 commit a6494a9

File tree

3 files changed

+89
-36
lines changed

3 files changed

+89
-36
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -390,33 +390,21 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
390390
$prototype = new Reference($handler['email_prototype']['id']);
391391
}
392392
} else {
393-
$message = new Definition('Swift_Message');
394-
$message->setLazy($handler['lazy']);
395-
$message->setPublic(false);
396-
$message->addMethodCall('setFrom', array($handler['from_email']));
397-
$message->addMethodCall('setTo', array($handler['to_email']));
398-
$message->addMethodCall('setSubject', array($handler['subject']));
399-
400-
if (isset($handler['mailer'])) {
401-
$mailer = $handler['mailer'];
402-
} else {
403-
$mailer = 'mailer';
404-
}
405-
406-
if (method_exists('Symfony\Component\DependencyInjection\Definition', 'setFactory')) {
407-
$message->setFactory(array(new Reference($mailer), 'createMessage'));
408-
} else {
409-
$message->setFactoryService($mailer);
410-
$message->setFactoryMethod('createMessage');
411-
}
412-
413-
if (isset($handler['content_type'])) {
414-
$message->addMethodCall('setContentType', array($handler['content_type']));
415-
}
393+
$messageFactory = new Definition('Symfony\Bundle\MonologBundle\SwiftMailer\MessageFactory');
394+
$messageFactory->setLazy(true);
395+
$messageFactory->setPublic(false);
396+
$messageFactory->setArguments(array(
397+
new Reference($handler['mailer']),
398+
$handler['from_email'],
399+
$handler['to_email'],
400+
$handler['subject'],
401+
$handler['content_type']
402+
));
416403

417-
$messageId = sprintf('%s.mail_prototype', $handlerId);
418-
$container->setDefinition($messageId, $message);
419-
$prototype = new Reference($messageId);
404+
$messageFactoryId = sprintf('%s.mail_message_factory', $handlerId);
405+
$container->setDefinition($messageFactoryId, $messageFactory);
406+
// set the prototype as a callable
407+
$prototype = array(new Reference($messageFactoryId), 'createMessage');
420408
}
421409
$definition->setArguments(array(
422410
new Reference($handler['mailer']),

SwiftMailer/MessageFactory.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MonologBundle\SwiftMailer;
13+
14+
/**
15+
* Helps create Swift_Message objects, lazily
16+
*
17+
* @author Ryan Weaver <[email protected]>
18+
*/
19+
class MessageFactory
20+
{
21+
private $mailer;
22+
23+
private $fromEmail;
24+
25+
private $toEmail;
26+
27+
private $subject;
28+
29+
private $contentType;
30+
31+
public function __construct(\Swift_Mailer $mailer, $fromEmail, $toEmail, $subject, $contentType = null)
32+
{
33+
$this->mailer = $mailer;
34+
$this->fromEmail = $fromEmail;
35+
$this->toEmail = $toEmail;
36+
$this->subject = $subject;
37+
$this->contentType = $contentType;
38+
}
39+
40+
/**
41+
* Creates a Swift_Message template that will be used to send the log message
42+
*
43+
* @param string $content formatted email body to be sent
44+
* @param array $records Log records that formed the content
45+
* @return \Swift_Message
46+
*/
47+
public function createMessage($content, array $records)
48+
{
49+
/** @var \Swift_Message $message */
50+
$message = $this->mailer->createMessage();
51+
$message->setTo($this->toEmail);
52+
$message->setFrom($this->fromEmail);
53+
$message->setSubject($this->subject);
54+
55+
if ($this->contentType) {
56+
$message->setContentType($this->contentType);
57+
}
58+
59+
return $message;
60+
}
61+
}

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,26 @@ public function testSingleEmailRecipient()
140140
{
141141
$container = $this->getContainer('single_email_recipient');
142142

143-
$this->assertSame(array(
144-
array('setFrom', array('[email protected]')),
145-
array('setTo', array(array('[email protected]'))),
146-
array('setSubject', array('An Error Occurred!')),
147-
), $container->getDefinition('monolog.handler.swift.mail_prototype')->getMethodCalls());
143+
$this->assertEquals(array(
144+
new Reference('mailer'),
145+
'[email protected]', // from
146+
array('[email protected]'), // to
147+
'An Error Occurred!', // subject
148+
null,
149+
), $container->getDefinition('monolog.handler.swift.mail_message_factory')->getArguments());
148150
}
149151

150152
public function testMultipleEmailRecipients()
151153
{
152154
$container = $this->getContainer('multiple_email_recipients');
153155

154-
$this->assertSame(array(
155-
array('setFrom', array('[email protected]')),
156-
array('setTo', array(array('[email protected]', '[email protected]'))),
157-
array('setSubject', array('An Error Occurred!')),
158-
), $container->getDefinition('monolog.handler.swift.mail_prototype')->getMethodCalls());
156+
$this->assertEquals (array(
157+
new Reference('mailer'),
158+
159+
160+
'An Error Occurred!',
161+
null
162+
), $container->getDefinition('monolog.handler.swift.mail_message_factory')->getArguments());
159163
}
160164

161165
public function testChannelParametersResolved()

0 commit comments

Comments
 (0)