Skip to content

Commit

Permalink
[Swiftmailer] added the swiftmailer.plugin tag to automatically regis…
Browse files Browse the repository at this point in the history
…ter Swiftmailer plugins
  • Loading branch information
fabpot committed Jun 6, 2011
1 parent a5daf47 commit 48733b9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
@@ -0,0 +1,36 @@
<?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\SwiftmailerBundle\DependencyInjection\Compiler;

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

/**
* RegisterPluginsPass registers Swiftmailer plugins.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RegisterPluginsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('swiftmailer.mailer')) {
return;
}

$definition = $container->findDefinition('swiftmailer.transport');
foreach ($container->findTaggedServiceIds('swiftmailer.plugin') as $id => $args) {
$definition->addMethodCall('registerPlugin', array(new Reference($id)));
}
}
}
Expand Up @@ -96,30 +96,30 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('swiftmailer.plugin.antiflood.threshold', $config['antiflood']['threshold']);
$container->setParameter('swiftmailer.plugin.antiflood.sleep', $config['antiflood']['sleep']);

$container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.antiflood')));
$container->getDefinition('swiftmailer.plugin.antiflood')->addTag('swiftmailer.plugin');
}

if ($config['logging']) {
$container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.messagelogger')));
$container->getDefinition('swiftmailer.plugin.messagelogger')->addTag('swiftmailer.plugin');
$container->findDefinition('swiftmailer.data_collector')->addTag('data_collector', array('template' => 'SwiftmailerBundle:Collector:swiftmailer', 'id' => 'swiftmailer'));
}

if (isset($config['sender_address']) && $config['sender_address']) {
$container->setParameter('swiftmailer.sender_address', $config['sender_address']);
$container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.impersonate')));
$container->getDefinition('swiftmailer.plugin.impersonate')->addTag('swiftmailer.plugin');
} else {
$container->setParameter('swiftmailer.sender_address', null);
}

if (isset($config['delivery_address']) && $config['delivery_address']) {
$container->setParameter('swiftmailer.single_address', $config['delivery_address']);
$container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.redirecting')));
$container->getDefinition('swiftmailer.plugin.redirecting')->addTag('swiftmailer.plugin');
} else {
$container->setParameter('swiftmailer.single_address', null);
}

if (isset($config['disable_delivery']) && $config['disable_delivery']) {
$container->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.blackhole')));
$container->getDefinition('swiftmailer.plugin.blackhole')->addTag('swiftmailer.plugin');
}
}

Expand Down
Expand Up @@ -67,7 +67,7 @@
</service>

<service id="swiftmailer.plugin.blackhole" class="%swiftmailer.plugin.blackhole.class%" public="false" />

<service id="swiftmailer.plugin.antiflood" class="%swiftmailer.plugin.antiflood.class%" public="false">
<argument>%swiftmailer.plugin.antiflood.threshold%</argument>
<argument>%swiftmailer.plugin.antiflood.sleep%</argument>
Expand All @@ -76,7 +76,7 @@
<service id="swiftmailer.plugin.impersonate" class="%swiftmailer.plugin.impersonate.class%" public="false">
<argument>%swiftmailer.sender_address%</argument>
</service>

<service id="swiftmailer.plugin.messagelogger" class="%swiftmailer.plugin.messagelogger.class%" />

<service id="swiftmailer.data_collector" class="%swiftmailer.data_collector.class%" public="false">
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/SwiftmailerBundle/SwiftmailerBundle.php
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Bundle\SwiftmailerBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\SwiftmailerBundle\DependencyInjection\Compiler\RegisterPluginsPass;

/**
* Bundle.
Expand All @@ -20,4 +22,10 @@
*/
class SwiftmailerBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new RegisterPluginsPass());
}
}

0 comments on commit 48733b9

Please sign in to comment.