-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from xabbuh/issue-65
configure Swiftmailer handlers transports depending on the current conta...
- Loading branch information
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
DependencyInjection/Compiler/AddSwiftMailerTransportPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
Tests/DependencyInjection/Compiler/AddSwiftMailerTransportPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |