Skip to content

Commit

Permalink
Merge pull request #66 from xabbuh/issue-65
Browse files Browse the repository at this point in the history
configure Swiftmailer handlers transports depending on the current conta...
  • Loading branch information
Seldaek committed Feb 19, 2014
2 parents 29a17a3 + 7b3658a commit 71ad844
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
55 changes: 55 additions & 0 deletions DependencyInjection/Compiler/AddSwiftMailerTransportPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;

/**
* Sets the transport for Swiftmailer handlers depending on the existing
* container definitions.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class AddSwiftMailerTransportPass implements CompilerPassInterface
{
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
$handlers = $container->getParameter('monolog.swift_mailer.handlers');

foreach ($handlers as $id) {
$definition = $container->getDefinition($id);

if (
$container->hasAlias('swiftmailer.transport.real') ||
$container->hasDefinition('swiftmailer.transport.real')
) {
$definition->addMethodCall(
'setTransport',
array(new Reference('swiftmailer.transport.real'))
);
} elseif (
$container->hasAlias('swiftmailer.transport') ||
$container->hasDefinition('swiftmailer.transport')
) {
$definition->addMethodCall(
'setTransport',
array(new Reference('swiftmailer.transport'))
);
}
}
}
}
9 changes: 8 additions & 1 deletion DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MonologExtension extends Extension
{
private $nestedHandlers = array();

private $swiftMailerHandlers = array();

/**
* Loads the Monolog configuration.
*
Expand All @@ -54,6 +56,11 @@ public function load(array $configs, ContainerBuilder $container)
);
}

$container->setParameter(
'monolog.swift_mailer.handlers',
$this->swiftMailerHandlers
);

ksort($handlers);
$sortedHandlers = array();
foreach ($handlers as $priorityHandlers) {
Expand Down Expand Up @@ -315,7 +322,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$handler['bubble'],
));
if (!$oldHandler) {
$definition->addMethodCall('setTransport', array(new Reference('swiftmailer.transport.real')));
$this->swiftMailerHandlers[] = $handlerId;
$definition->addTag('kernel.event_listener', array('event' => 'kernel.terminate', 'method' => 'onKernelTerminate'));
}
break;
Expand Down
2 changes: 2 additions & 0 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\MonologBundle;

use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
Expand All @@ -31,5 +32,6 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass($channelPass = new LoggerChannelPass());
$container->addCompilerPass(new DebugHandlerPass($channelPass));
$container->addCompilerPass(new AddProcessorsPass());
$container->addCompilerPass(new AddSwiftMailerTransportPass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler;

use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class AddSwiftMailerTransportPassTest extends \PHPUnit_Framework_TestCase
{
private $compilerPass;

private $container;

private $definition;

protected function setUp()
{
$this->compilerPass = new AddSwiftMailerTransportPass();
$this->definition = $this->getMock('\Symfony\Component\DependencyInjection\Definition');
$this->container = $this->getMock('\Symfony\Component\DependencyInjection\ContainerBuilder');
$this->container->expects($this->any())
->method('getParameter')
->with('monolog.swift_mailer.handlers')
->will($this->returnValue(array('foo')));
$this->container->expects($this->any())
->method('getDefinition')
->with('foo')
->will($this->returnValue($this->definition));
}

public function testWithRealTransport()
{
$this->container
->expects($this->any())
->method('hasDefinition')
->with('swiftmailer.transport.real')
->will($this->returnValue(true));
$this->definition
->expects($this->once())
->method('addMethodCall')
->with(
'setTransport',
$this->equalTo(array(new Reference('swiftmailer.transport.real')))
);

$this->compilerPass->process($this->container);
}

public function testWithoutRealTransport()
{
$this->container
->expects($this->any())
->method('hasDefinition')
->will($this->returnValueMap(
array(
array('swiftmailer.transport.real', false),
array('swiftmailer.transport', true),
)
));
$this->definition
->expects($this->once())
->method('addMethodCall')
->with(
'setTransport',
$this->equalTo(array(new Reference('swiftmailer.transport')))
);

$this->compilerPass->process($this->container);
}
}

0 comments on commit 71ad844

Please sign in to comment.