Skip to content

Commit 71ad844

Browse files
committed
Merge pull request #66 from xabbuh/issue-65
configure Swiftmailer handlers transports depending on the current conta...
2 parents 29a17a3 + 7b3658a commit 71ad844

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* Sets the transport for Swiftmailer handlers depending on the existing
20+
* container definitions.
21+
*
22+
* @author Christian Flothmann <[email protected]>
23+
*/
24+
class AddSwiftMailerTransportPass implements CompilerPassInterface
25+
{
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function process(ContainerBuilder $container)
30+
{
31+
$handlers = $container->getParameter('monolog.swift_mailer.handlers');
32+
33+
foreach ($handlers as $id) {
34+
$definition = $container->getDefinition($id);
35+
36+
if (
37+
$container->hasAlias('swiftmailer.transport.real') ||
38+
$container->hasDefinition('swiftmailer.transport.real')
39+
) {
40+
$definition->addMethodCall(
41+
'setTransport',
42+
array(new Reference('swiftmailer.transport.real'))
43+
);
44+
} elseif (
45+
$container->hasAlias('swiftmailer.transport') ||
46+
$container->hasDefinition('swiftmailer.transport')
47+
) {
48+
$definition->addMethodCall(
49+
'setTransport',
50+
array(new Reference('swiftmailer.transport'))
51+
);
52+
}
53+
}
54+
}
55+
}

DependencyInjection/MonologExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class MonologExtension extends Extension
2929
{
3030
private $nestedHandlers = array();
3131

32+
private $swiftMailerHandlers = array();
33+
3234
/**
3335
* Loads the Monolog configuration.
3436
*
@@ -54,6 +56,11 @@ public function load(array $configs, ContainerBuilder $container)
5456
);
5557
}
5658

59+
$container->setParameter(
60+
'monolog.swift_mailer.handlers',
61+
$this->swiftMailerHandlers
62+
);
63+
5764
ksort($handlers);
5865
$sortedHandlers = array();
5966
foreach ($handlers as $priorityHandlers) {
@@ -315,7 +322,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
315322
$handler['bubble'],
316323
));
317324
if (!$oldHandler) {
318-
$definition->addMethodCall('setTransport', array(new Reference('swiftmailer.transport.real')));
325+
$this->swiftMailerHandlers[] = $handlerId;
319326
$definition->addTag('kernel.event_listener', array('event' => 'kernel.terminate', 'method' => 'onKernelTerminate'));
320327
}
321328
break;

MonologBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\MonologBundle;
1313

14+
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass;
1415
use Symfony\Component\HttpKernel\Bundle\Bundle;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
@@ -31,5 +32,6 @@ public function build(ContainerBuilder $container)
3132
$container->addCompilerPass($channelPass = new LoggerChannelPass());
3233
$container->addCompilerPass(new DebugHandlerPass($channelPass));
3334
$container->addCompilerPass(new AddProcessorsPass());
35+
$container->addCompilerPass(new AddSwiftMailerTransportPass());
3436
}
3537
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass;
15+
use Symfony\Component\DependencyInjection\Reference;
16+
17+
/**
18+
* @author Christian Flothmann <[email protected]>
19+
*/
20+
class AddSwiftMailerTransportPassTest extends \PHPUnit_Framework_TestCase
21+
{
22+
private $compilerPass;
23+
24+
private $container;
25+
26+
private $definition;
27+
28+
protected function setUp()
29+
{
30+
$this->compilerPass = new AddSwiftMailerTransportPass();
31+
$this->definition = $this->getMock('\Symfony\Component\DependencyInjection\Definition');
32+
$this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
33+
$this->container->expects($this->any())
34+
->method('getParameter')
35+
->with('monolog.swift_mailer.handlers')
36+
->will($this->returnValue(array('foo')));
37+
$this->container->expects($this->any())
38+
->method('getDefinition')
39+
->with('foo')
40+
->will($this->returnValue($this->definition));
41+
}
42+
43+
public function testWithRealTransport()
44+
{
45+
$this->container
46+
->expects($this->any())
47+
->method('hasDefinition')
48+
->with('swiftmailer.transport.real')
49+
->will($this->returnValue(true));
50+
$this->definition
51+
->expects($this->once())
52+
->method('addMethodCall')
53+
->with(
54+
'setTransport',
55+
$this->equalTo(array(new Reference('swiftmailer.transport.real')))
56+
);
57+
58+
$this->compilerPass->process($this->container);
59+
}
60+
61+
public function testWithoutRealTransport()
62+
{
63+
$this->container
64+
->expects($this->any())
65+
->method('hasDefinition')
66+
->will($this->returnValueMap(
67+
array(
68+
array('swiftmailer.transport.real', false),
69+
array('swiftmailer.transport', true),
70+
)
71+
));
72+
$this->definition
73+
->expects($this->once())
74+
->method('addMethodCall')
75+
->with(
76+
'setTransport',
77+
$this->equalTo(array(new Reference('swiftmailer.transport')))
78+
);
79+
80+
$this->compilerPass->process($this->container);
81+
}
82+
}

0 commit comments

Comments
 (0)