Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure Swiftmailer handlers transports depending on the current conta... #66

Merged
merged 1 commit into from
Feb 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -316,7 +323,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);
}
}