Update
This commit is contained in:
parent
a37785b391
commit
33458b2ca3
9915 changed files with 1247019 additions and 0 deletions
|
|
@ -0,0 +1,60 @@
|
|||
<?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\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* RegisterPluginsPass registers Swiftmailer plugins.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class RegisterPluginsPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->findDefinition('swiftmailer.mailer') || !$container->getParameter('swiftmailer.mailers')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$mailers = $container->getParameter('swiftmailer.mailers');
|
||||
foreach ($mailers as $name => $mailer) {
|
||||
$plugins = $this->findSortedByPriorityTaggedServiceIds($container, sprintf('swiftmailer.%s.plugin', $name));
|
||||
$transport = sprintf('swiftmailer.mailer.%s.transport', $name);
|
||||
$definition = $container->findDefinition($transport);
|
||||
foreach ($plugins as $id => $args) {
|
||||
$definition->addMethodCall('registerPlugin', array(new Reference($id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function findSortedByPriorityTaggedServiceIds(ContainerBuilder $container, $tag)
|
||||
{
|
||||
$taggedServices = $container->findTaggedServiceIds($tag);
|
||||
uasort(
|
||||
$taggedServices,
|
||||
function ($tagA, $tagB) {
|
||||
$priorityTagA = isset($tagA[0]['priority']) ? $tagA[0]['priority'] : 0;
|
||||
$priorityTagB = isset($tagB[0]['priority']) ? $tagB[0]['priority'] : 0;
|
||||
|
||||
return $priorityTagA - $priorityTagB;
|
||||
}
|
||||
);
|
||||
|
||||
return $taggedServices;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,197 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
|
||||
/**
|
||||
* This class contains the configuration information for the bundle.
|
||||
*
|
||||
* This information is solely responsible for how the different configuration
|
||||
* sections are normalized, and merged.
|
||||
*
|
||||
* @author Christophe Coevoet <stof@notk.org>
|
||||
*/
|
||||
class Configuration implements ConfigurationInterface
|
||||
{
|
||||
private $debug;
|
||||
|
||||
/**
|
||||
* @param bool $debug The kernel.debug value
|
||||
*/
|
||||
public function __construct($debug)
|
||||
{
|
||||
$this->debug = (Boolean) $debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('swiftmailer');
|
||||
|
||||
$rootNode
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('mailers', $v) && !array_key_exists('mailer', $v); })
|
||||
->then(function ($v) {
|
||||
$mailer = array();
|
||||
foreach ($v as $key => $value) {
|
||||
if ('default_mailer' == $key) {
|
||||
continue;
|
||||
}
|
||||
$mailer[$key] = $v[$key];
|
||||
unset($v[$key]);
|
||||
}
|
||||
$v['default_mailer'] = isset($v['default_mailer']) ? (string) $v['default_mailer'] : 'default';
|
||||
$v['mailers'] = array($v['default_mailer'] => $mailer);
|
||||
|
||||
return $v;
|
||||
})
|
||||
->end()
|
||||
->children()
|
||||
->scalarNode('default_mailer')->end()
|
||||
->append($this->getMailersNode())
|
||||
->end()
|
||||
->fixXmlConfig('mailer')
|
||||
;
|
||||
|
||||
return $treeBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayNodeDefinition
|
||||
*/
|
||||
private function getMailersNode()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$node = $treeBuilder->root('mailers');
|
||||
|
||||
$node
|
||||
->requiresAtLeastOneElement()
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
// BC layer for "delivery_address: null" (the case of a string goes through the XML normalization too)
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) {
|
||||
return is_array($v) && array_key_exists('delivery_address', $v) && null === $v['delivery_address'];
|
||||
})
|
||||
->then(function ($v) {
|
||||
@trigger_error('The swiftmailer.delivery_address configuration key is deprecated since version 2.3.10 and will be removed in 3.0. Use the swiftmailer.delivery_addresses configuration key instead (or remove the empty setting)', E_USER_DEPRECATED);
|
||||
unset($v['delivery_address']);
|
||||
|
||||
if (!isset($v['delivery_addresses'])) {
|
||||
$v['delivery_addresses'] = array();
|
||||
}
|
||||
|
||||
return $v;
|
||||
})
|
||||
->end()
|
||||
->children()
|
||||
->scalarNode('url')->defaultNull()->end()
|
||||
->scalarNode('transport')->defaultValue('smtp')->end()
|
||||
->scalarNode('username')->defaultNull()->end()
|
||||
->scalarNode('password')->defaultNull()->end()
|
||||
->scalarNode('host')->defaultValue('localhost')->end()
|
||||
->scalarNode('port')->defaultNull()->end()
|
||||
->scalarNode('timeout')->defaultValue(30)->end()
|
||||
->scalarNode('source_ip')->defaultNull()->end()
|
||||
->scalarNode('local_domain')->defaultNull()->end()
|
||||
->arrayNode('stream_options')
|
||||
->ignoreExtraKeys(false)
|
||||
->normalizeKeys(false)
|
||||
->beforeNormalization()
|
||||
->ifTrue(function ($v) { return isset($v['stream-option']); })
|
||||
->then(function ($v) {
|
||||
$recurse = function ($array) use (&$recurse) {
|
||||
if (isset($array['name'])) {
|
||||
$array = array($array);
|
||||
}
|
||||
$n = array();
|
||||
foreach ($array as $v) {
|
||||
$k = $v['name'];
|
||||
if (isset($v['value'])) {
|
||||
$n[$k] = $v['value'];
|
||||
} elseif (isset($v['stream-option'])) {
|
||||
$n[$k] = $recurse($v['stream-option']);
|
||||
}
|
||||
}
|
||||
return $n;
|
||||
};
|
||||
return $recurse($v['stream-option']);
|
||||
})
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(function ($v) { return !method_exists('Swift_Transport_EsmtpTransport', 'setStreamOptions'); })
|
||||
->thenInvalid('stream_options is only available in Swiftmailer 5.4.2 or later.')
|
||||
->end()
|
||||
->end()
|
||||
->scalarNode('encryption')
|
||||
->defaultNull()
|
||||
->validate()
|
||||
->ifNotInArray(array('tls', 'ssl', null))
|
||||
->thenInvalid('The %s encryption is not supported')
|
||||
->end()
|
||||
->end()
|
||||
->scalarNode('auth_mode')
|
||||
->defaultNull()
|
||||
->validate()
|
||||
->ifNotInArray(array('plain', 'login', 'cram-md5', null))
|
||||
->thenInvalid('The %s authentication mode is not supported')
|
||||
->end()
|
||||
->end()
|
||||
->scalarNode('sender_address')->end()
|
||||
->arrayNode('delivery_addresses')
|
||||
->performNoDeepMerging()
|
||||
->beforeNormalization()
|
||||
->ifArray()
|
||||
->then(function ($v) { return array_values($v); })
|
||||
->end()
|
||||
->prototype('scalar')
|
||||
->end()
|
||||
->end()
|
||||
->arrayNode('antiflood')
|
||||
->children()
|
||||
->scalarNode('threshold')->defaultValue(99)->end()
|
||||
->scalarNode('sleep')->defaultValue(0)->end()
|
||||
->end()
|
||||
->end()
|
||||
->booleanNode('logging')->defaultValue($this->debug)->end()
|
||||
->arrayNode('spool')
|
||||
->children()
|
||||
->scalarNode('type')->defaultValue('file')->end()
|
||||
->scalarNode('path')->defaultValue('%kernel.cache_dir%/swiftmailer/spool')->end()
|
||||
->scalarNode('id')->defaultNull()->info('Used by "service" type')->end()
|
||||
->end()
|
||||
->validate()
|
||||
->ifTrue(function ($v) { return 'service' === $v['type'] && empty($v['id']); })
|
||||
->thenInvalid('You have to configure the service id')
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('delivery_whitelist_pattern', 'delivery_whitelist')
|
||||
->fixXmlConfig('delivery_address', 'delivery_addresses')
|
||||
->children()
|
||||
->arrayNode('delivery_whitelist')
|
||||
->prototype('scalar')
|
||||
->end()
|
||||
->end()
|
||||
->booleanNode('disable_delivery')->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
class SmtpTransportConfigurator
|
||||
{
|
||||
private $localDomain;
|
||||
|
||||
private $requestContext;
|
||||
|
||||
public function __construct($localDomain, RequestContext $requestContext = null)
|
||||
{
|
||||
$this->localDomain = $localDomain;
|
||||
$this->requestContext = $requestContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the local domain based on the current request context.
|
||||
*/
|
||||
public function configure(\Swift_Transport_AbstractSmtpTransport $transport)
|
||||
{
|
||||
if ($this->localDomain) {
|
||||
$transport->setLocalDomain($this->localDomain);
|
||||
} elseif ($this->requestContext) {
|
||||
$transport->setLocalDomain($this->requestContext->getHost());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,407 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
|
||||
/**
|
||||
* SwiftmailerExtension is an extension for the SwiftMailer library.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class SwiftmailerExtension extends Extension
|
||||
{
|
||||
/**
|
||||
* Loads the Swift Mailer configuration.
|
||||
*
|
||||
* Usage example:
|
||||
*
|
||||
* <swiftmailer:config transport="gmail">
|
||||
* <swiftmailer:username>fabien</swift:username>
|
||||
* <swiftmailer:password>xxxxx</swift:password>
|
||||
* <swiftmailer:spool path="/path/to/spool/" />
|
||||
* </swiftmailer:config>
|
||||
*
|
||||
* @param array $configs An array of configuration settings
|
||||
* @param ContainerBuilder $container A ContainerBuilder instance
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('swiftmailer.xml');
|
||||
|
||||
$configuration = $this->getConfiguration($configs, $container);
|
||||
$config = $this->processConfiguration($configuration, $configs);
|
||||
|
||||
$mailers = array();
|
||||
foreach ($config['mailers'] as $name => $mailer) {
|
||||
$isDefaultMailer = $config['default_mailer'] === $name;
|
||||
$this->configureMailer($name, $mailer, $container, $isDefaultMailer);
|
||||
$mailers[$name] = sprintf('swiftmailer.mailer.%s', $name);
|
||||
}
|
||||
ksort($mailers);
|
||||
$container->setParameter('swiftmailer.mailers', $mailers);
|
||||
$container->setParameter('swiftmailer.default_mailer', $config['default_mailer']);
|
||||
|
||||
$container->findDefinition('swiftmailer.data_collector')->addTag('data_collector', array('template' => '@Swiftmailer/Collector/swiftmailer.html.twig', 'id' => 'swiftmailer', 'priority' => 245));
|
||||
|
||||
$container->setAlias('mailer', 'swiftmailer.mailer');
|
||||
$container->getAlias('mailer')->setPublic(true);
|
||||
}
|
||||
|
||||
protected function configureMailer($name, array $mailer, ContainerBuilder $container, $isDefaultMailer = false)
|
||||
{
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.transport.eventdispatcher.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name), $definitionDecorator)
|
||||
;
|
||||
|
||||
$usedEnvs = null;
|
||||
$disableDelivery = isset($mailer['disable_delivery']) && $mailer['disable_delivery'];
|
||||
|
||||
if (method_exists($container, 'resolveEnvPlaceholders')) {
|
||||
$options = array();
|
||||
$envVariablesAllowed = array('transport', 'url', 'username', 'password', 'host', 'port', 'timeout', 'source_ip', 'local_domain', 'encryption', 'auth_mode');
|
||||
foreach ($envVariablesAllowed as $key) {
|
||||
$container->resolveEnvPlaceholders($mailer[$key], null, $usedEnvs);
|
||||
$options[$key] = $mailer[$key];
|
||||
}
|
||||
}
|
||||
if ($usedEnvs && !$disableDelivery) {
|
||||
$transportId = sprintf('swiftmailer.mailer.%s.transport.dynamic', $name);
|
||||
$definitionDecorator = new Definition('\Swift_Transport');
|
||||
$definitionDecorator->setFactory(array('Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerTransportFactory', 'createTransport'));
|
||||
$definitionDecorator->setArguments(array(
|
||||
$options,
|
||||
new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE),
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
|
||||
));
|
||||
$container->setDefinition(sprintf('swiftmailer.mailer.%s.transport.dynamic', $name), $definitionDecorator);
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), $transportId);
|
||||
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.mailer.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s', $name), $definitionDecorator)
|
||||
->replaceArgument(0, new Reference(sprintf('swiftmailer.mailer.%s.transport', $name)))
|
||||
;
|
||||
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name), 'dynamic');
|
||||
} else {
|
||||
$mailer = SwiftmailerTransportFactory::resolveOptions($mailer);
|
||||
$transport = $disableDelivery ? 'null' : $mailer['transport'];
|
||||
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name), $transport);
|
||||
|
||||
$transportId = in_array($transport, array('smtp', 'sendmail', 'null', 'mail'))
|
||||
? sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport)
|
||||
: $transport;
|
||||
|
||||
$this->configureMailerTransport($name, $mailer, $container, $transport, $isDefaultMailer);
|
||||
}
|
||||
$this->configureMailerSpool($name, $mailer, $container, $transportId, $isDefaultMailer);
|
||||
$this->configureMailerSenderAddress($name, $mailer, $container, $isDefaultMailer);
|
||||
$this->configureMailerAntiFlood($name, $mailer, $container, $isDefaultMailer);
|
||||
$this->configureMailerDeliveryAddress($name, $mailer, $container, $isDefaultMailer);
|
||||
$this->configureMailerLogging($name, $mailer, $container, $isDefaultMailer);
|
||||
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name), !$disableDelivery);
|
||||
|
||||
// alias
|
||||
if ($isDefaultMailer) {
|
||||
$container->setAlias('swiftmailer.mailer', sprintf('swiftmailer.mailer.%s', $name));
|
||||
$container->setAlias('swiftmailer.transport', sprintf('swiftmailer.mailer.%s.transport', $name));
|
||||
$container->setParameter('swiftmailer.spool.enabled', $container->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)));
|
||||
$container->setParameter('swiftmailer.delivery.enabled', $container->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)));
|
||||
$container->setParameter('swiftmailer.single_address', $container->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name)));
|
||||
$container->setAlias('Swift_Mailer', new Alias('swiftmailer.mailer', false));
|
||||
$container->setAlias('Swift_Transport', new Alias('swiftmailer.transport', false));
|
||||
}
|
||||
}
|
||||
|
||||
protected function configureMailerTransport($name, array $mailer, ContainerBuilder $container, $transport, $isDefaultMailer = false)
|
||||
{
|
||||
foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode', 'timeout', 'source_ip', 'local_domain') as $key) {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.%s', $name, $key), $mailer[$key]);
|
||||
}
|
||||
|
||||
if ('smtp' === $transport) {
|
||||
$authDecorator = $this->createChildDefinition('swiftmailer.transport.authhandler.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.authhandler', $name), $authDecorator)
|
||||
->addMethodCall('setUsername', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.username%%', $name)))
|
||||
->addMethodCall('setPassword', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.password%%', $name)))
|
||||
->addMethodCall('setAuthMode', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.auth_mode%%', $name)));
|
||||
|
||||
$bufferDecorator = $this->createChildDefinition('swiftmailer.transport.buffer.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator);
|
||||
|
||||
$configuratorDecorator = $this->createChildDefinition('swiftmailer.transport.smtp.configurator.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.transport.configurator.%s', $name), $configuratorDecorator)
|
||||
->setArguments(array(
|
||||
sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name),
|
||||
new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE),
|
||||
))
|
||||
;
|
||||
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.transport.smtp.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.smtp', $name), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.buffer', $name)),
|
||||
array(new Reference(sprintf('swiftmailer.mailer.%s.transport.authhandler', $name))),
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
|
||||
))
|
||||
->addMethodCall('setHost', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.host%%', $name)))
|
||||
->addMethodCall('setPort', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.port%%', $name)))
|
||||
->addMethodCall('setEncryption', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.encryption%%', $name)))
|
||||
->addMethodCall('setTimeout', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.timeout%%', $name)))
|
||||
->addMethodCall('setSourceIp', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.source_ip%%', $name)))
|
||||
->setConfigurator(array(new Reference(sprintf('swiftmailer.transport.configurator.%s', $name)), 'configure'))
|
||||
;
|
||||
|
||||
if (isset($mailer['stream_options'])) {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.transport.smtp.stream_options', $name), $mailer['stream_options']);
|
||||
$definitionDecorator->addMethodCall('setStreamOptions', array(sprintf('%%swiftmailer.mailer.%s.transport.smtp.stream_options%%', $name)));
|
||||
}
|
||||
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport));
|
||||
} elseif ('sendmail' === $transport) {
|
||||
$bufferDecorator = $this->createChildDefinition('swiftmailer.transport.buffer.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.buffer', $name), $bufferDecorator);
|
||||
|
||||
$configuratorDecorator = $this->createChildDefinition('swiftmailer.transport.smtp.configurator.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.transport.configurator.%s', $name), $configuratorDecorator)
|
||||
->setArguments(array(
|
||||
sprintf('%%swiftmailer.mailer.%s.transport.smtp.local_domain%%', $name),
|
||||
new Reference('router.request_context', ContainerInterface::NULL_ON_INVALID_REFERENCE),
|
||||
))
|
||||
;
|
||||
|
||||
$definitionDecorator = $this->createChildDefinition(sprintf('swiftmailer.transport.%s.abstract', $transport));
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.buffer', $name)),
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
|
||||
))
|
||||
->setConfigurator(array(new Reference(sprintf('swiftmailer.transport.configurator.%s', $name)), 'configure'))
|
||||
;
|
||||
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport));
|
||||
} elseif ('mail' === $transport) {
|
||||
// deprecated
|
||||
$definitionDecorator = $this->createChildDefinition(sprintf('swiftmailer.transport.%s.abstract', $transport));
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport), $definitionDecorator)
|
||||
->addArgument(new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)))
|
||||
;
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport));
|
||||
} elseif ('null' === $transport) {
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.transport.null.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.null', $name, $transport), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
|
||||
))
|
||||
;
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.%s', $name, $transport));
|
||||
} else {
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.transport.%s', $transport));
|
||||
}
|
||||
|
||||
if (method_exists('Symfony\Component\DependencyInjection\Alias', 'setPrivate')) {
|
||||
$container->getAlias(sprintf('swiftmailer.mailer.%s.transport', $name))->setPrivate(false);
|
||||
}
|
||||
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.mailer.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s', $name), $definitionDecorator)
|
||||
->replaceArgument(0, new Reference(sprintf('swiftmailer.mailer.%s.transport', $name)))
|
||||
;
|
||||
}
|
||||
|
||||
protected function configureMailerSpool($name, array $mailer, ContainerBuilder $container, $transport, $isDefaultMailer = false)
|
||||
{
|
||||
if (isset($mailer['spool'])) {
|
||||
$type = $mailer['spool']['type'];
|
||||
if ('service' === $type) {
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.spool.service', $name), $mailer['spool']['id']);
|
||||
} else {
|
||||
foreach (array('path') as $key) {
|
||||
$container->setParameter(sprintf('swiftmailer.spool.%s.%s.%s', $name, $type, $key), $mailer['spool'][$key].'/'.$name);
|
||||
}
|
||||
}
|
||||
|
||||
$definitionDecorator = $this->createChildDefinition(sprintf('swiftmailer.spool.%s.abstract', $type));
|
||||
if ('file' === $type) {
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.spool.file', $name), $definitionDecorator)
|
||||
->replaceArgument(0, sprintf('%%swiftmailer.spool.%s.file.path%%', $name))
|
||||
;
|
||||
} elseif ('memory' === $type) {
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.spool.memory', $name), $definitionDecorator)
|
||||
;
|
||||
}
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.spool', $name), sprintf('swiftmailer.mailer.%s.spool.%s', $name, $type));
|
||||
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.transport.spool.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.transport.spool', $name), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.transport.eventdispatcher', $name)),
|
||||
new Reference(sprintf('swiftmailer.mailer.%s.spool', $name)),
|
||||
))
|
||||
;
|
||||
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport.real', $name), $transport);
|
||||
$container->getAlias(sprintf('swiftmailer.mailer.%s.transport.real', $name))->setPublic(true);
|
||||
$container->setAlias(sprintf('swiftmailer.mailer.%s.transport', $name), sprintf('swiftmailer.mailer.%s.transport.spool', $name));
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name), true);
|
||||
if (true === $isDefaultMailer) {
|
||||
$container->setAlias('swiftmailer.spool', sprintf('swiftmailer.mailer.%s.spool', $name));
|
||||
$container->setAlias('swiftmailer.transport.real', sprintf('swiftmailer.mailer.%s.transport.real', $name));
|
||||
$container->setAlias('Swift_Spool', new Alias('swiftmailer.spool', false));
|
||||
}
|
||||
} else {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name), false);
|
||||
}
|
||||
}
|
||||
|
||||
protected function configureMailerSenderAddress($name, array $mailer, ContainerBuilder $container, $isDefaultMailer = false)
|
||||
{
|
||||
if (isset($mailer['sender_address']) && $mailer['sender_address']) {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.sender_address', $name), $mailer['sender_address']);
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.plugin.impersonate.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.plugin.impersonate', $name), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
sprintf('%%swiftmailer.mailer.%s.sender_address%%', $name),
|
||||
))
|
||||
;
|
||||
$container->getDefinition(sprintf('swiftmailer.mailer.%s.plugin.impersonate', $name))->addTag(sprintf('swiftmailer.%s.plugin', $name));
|
||||
if (true === $isDefaultMailer) {
|
||||
$container->setAlias('swiftmailer.plugin.impersonate', sprintf('swiftmailer.mailer.%s.plugin.impersonate', $name));
|
||||
$container->setParameter('swiftmailer.sender_address', $container->getParameter(sprintf('swiftmailer.mailer.%s.sender_address', $name)));
|
||||
}
|
||||
} else {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.plugin.impersonate', $name), null);
|
||||
}
|
||||
}
|
||||
|
||||
protected function configureMailerAntiFlood($name, array $mailer, ContainerBuilder $container, $isDefaultMailer = false)
|
||||
{
|
||||
if (isset($mailer['antiflood'])) {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.antiflood.threshold', $name), $mailer['antiflood']['threshold']);
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.antiflood.sleep', $name), $mailer['antiflood']['sleep']);
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.plugin.antiflood.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.plugin.antiflood', $name), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
sprintf('%%swiftmailer.mailer.%s.antiflood.threshold%%', $name),
|
||||
sprintf('%%swiftmailer.mailer.%s.antiflood.sleep%%', $name),
|
||||
))
|
||||
;
|
||||
$container->getDefinition(sprintf('swiftmailer.mailer.%s.plugin.antiflood', $name))->addTag(sprintf('swiftmailer.%s.plugin', $name));
|
||||
if (true === $isDefaultMailer) {
|
||||
$container->setAlias('swiftmailer.mailer.plugin.antiflood', sprintf('swiftmailer.mailer.%s.plugin.antiflood', $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function configureMailerDeliveryAddress($name, array $mailer, ContainerBuilder $container, $isDefaultMailer = false)
|
||||
{
|
||||
if (count($mailer['delivery_addresses']) > 0) {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.single_address', $name), $mailer['delivery_addresses'][0]);
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery_addresses', $name), $mailer['delivery_addresses']);
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.delivery_whitelist', $name), $mailer['delivery_whitelist']);
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.plugin.redirecting.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.plugin.redirecting', $name), $definitionDecorator)
|
||||
->setArguments(array(
|
||||
sprintf('%%swiftmailer.mailer.%s.delivery_addresses%%', $name),
|
||||
sprintf('%%swiftmailer.mailer.%s.delivery_whitelist%%', $name),
|
||||
))
|
||||
;
|
||||
$container->getDefinition(sprintf('swiftmailer.mailer.%s.plugin.redirecting', $name))->addTag(sprintf('swiftmailer.%s.plugin', $name));
|
||||
if (true === $isDefaultMailer) {
|
||||
$container->setAlias('swiftmailer.plugin.redirecting', sprintf('swiftmailer.mailer.%s.plugin.redirecting', $name));
|
||||
}
|
||||
} else {
|
||||
$container->setParameter(sprintf('swiftmailer.mailer.%s.single_address', $name), null);
|
||||
}
|
||||
}
|
||||
|
||||
protected function configureMailerLogging($name, array $mailer, ContainerBuilder $container, $isDefaultMailer = false)
|
||||
{
|
||||
if ($mailer['logging']) {
|
||||
$container->getDefinition('swiftmailer.plugin.messagelogger.abstract');
|
||||
$definitionDecorator = $this->createChildDefinition('swiftmailer.plugin.messagelogger.abstract');
|
||||
$container
|
||||
->setDefinition(sprintf('swiftmailer.mailer.%s.plugin.messagelogger', $name), $definitionDecorator)
|
||||
;
|
||||
$container->getDefinition(sprintf('swiftmailer.mailer.%s.plugin.messagelogger', $name))
|
||||
->setPublic(true)
|
||||
->addTag(sprintf('swiftmailer.%s.plugin', $name));
|
||||
if (true === $isDefaultMailer) {
|
||||
$container->setAlias('swiftmailer.plugin.messagelogger', sprintf('swiftmailer.mailer.%s.plugin.messagelogger', $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the base path for the XSD files.
|
||||
*
|
||||
* @return string The XSD base path
|
||||
*/
|
||||
public function getXsdValidationBasePath()
|
||||
{
|
||||
return __DIR__.'/../Resources/config/schema';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the namespace to be used for this extension (XML namespace).
|
||||
*
|
||||
* @return string The XML namespace
|
||||
*/
|
||||
public function getNamespace()
|
||||
{
|
||||
return 'http://symfony.com/schema/dic/swiftmailer';
|
||||
}
|
||||
|
||||
public function getConfiguration(array $config, ContainerBuilder $container)
|
||||
{
|
||||
return new Configuration($container->getParameter('kernel.debug'));
|
||||
}
|
||||
|
||||
private function createChildDefinition($id)
|
||||
{
|
||||
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
|
||||
return new ChildDefinition($id);
|
||||
}
|
||||
|
||||
return new DefinitionDecorator($id);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\Routing\RequestContext;
|
||||
|
||||
/**
|
||||
* Factory to create a \Swift_Transport object.
|
||||
*
|
||||
* @author Romain Gautier <mail@romain.sh>
|
||||
*/
|
||||
class SwiftmailerTransportFactory
|
||||
{
|
||||
/**
|
||||
* @param array $options
|
||||
* @param RequestContext|null $requestContext
|
||||
* @param \Swift_Events_EventDispatcher $eventDispatcher
|
||||
*
|
||||
* @return \Swift_Transport
|
||||
*
|
||||
* @throws \InvalidArgumentException if the scheme is not a built-in Swiftmailer transport
|
||||
*/
|
||||
public static function createTransport(array $options, RequestContext $requestContext = null, \Swift_Events_EventDispatcher $eventDispatcher)
|
||||
{
|
||||
$options = static::resolveOptions($options);
|
||||
|
||||
if ('smtp' === $options['transport']) {
|
||||
$smtpAuthHandler = new \Swift_Transport_Esmtp_AuthHandler(array(
|
||||
new \Swift_Transport_Esmtp_Auth_CramMd5Authenticator(),
|
||||
new \Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
|
||||
new \Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
|
||||
));
|
||||
$smtpAuthHandler->setUsername($options['username']);
|
||||
$smtpAuthHandler->setPassword($options['password']);
|
||||
$smtpAuthHandler->setAuthMode($options['auth_mode']);
|
||||
|
||||
$transport = new \Swift_Transport_EsmtpTransport(
|
||||
new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
|
||||
array($smtpAuthHandler),
|
||||
$eventDispatcher
|
||||
);
|
||||
$transport->setHost($options['host']);
|
||||
$transport->setPort($options['port']);
|
||||
$transport->setEncryption($options['encryption']);
|
||||
$transport->setTimeout($options['timeout']);
|
||||
$transport->setSourceIp($options['source_ip']);
|
||||
|
||||
$smtpTransportConfigurator = new SmtpTransportConfigurator(null, $requestContext);
|
||||
$smtpTransportConfigurator->configure($transport);
|
||||
} elseif ('sendmail' === $options['transport']) {
|
||||
$transport = new \Swift_Transport_SendmailTransport(
|
||||
new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory()),
|
||||
$eventDispatcher
|
||||
);
|
||||
|
||||
$smtpTransportConfigurator = new SmtpTransportConfigurator(null, $requestContext);
|
||||
$smtpTransportConfigurator->configure($transport);
|
||||
} elseif ('mail' === $options['transport']) {
|
||||
$transport = new \Swift_Transport_MailTransport(new \Swift_Transport_SimpleMailInvoker(), $eventDispatcher);
|
||||
} elseif ('null' === $options['transport']) {
|
||||
$transport = new \Swift_Transport_NullTransport($eventDispatcher);
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf('Not a built-in Swiftmailer transport: %s.', $options['transport']));
|
||||
}
|
||||
|
||||
return $transport;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*
|
||||
* @return array options
|
||||
*/
|
||||
public static function resolveOptions(array $options)
|
||||
{
|
||||
$options += array(
|
||||
'transport' => null,
|
||||
'username' => null,
|
||||
'password' => null,
|
||||
'host' => null,
|
||||
'port' => null,
|
||||
'timeout' => null,
|
||||
'source_ip' => null,
|
||||
'local_domain' => null,
|
||||
'encryption' => null,
|
||||
'auth_mode' => null,
|
||||
);
|
||||
|
||||
if (isset($options['url'])) {
|
||||
$parts = parse_url($options['url']);
|
||||
if (isset($parts['scheme'])) {
|
||||
$options['transport'] = $parts['scheme'];
|
||||
}
|
||||
if (isset($parts['user'])) {
|
||||
$options['username'] = $parts['user'];
|
||||
}
|
||||
if (isset($parts['pass'])) {
|
||||
$options['password'] = $parts['pass'];
|
||||
}
|
||||
if (isset($parts['host'])) {
|
||||
$options['host'] = $parts['host'];
|
||||
}
|
||||
if (isset($parts['port'])) {
|
||||
$options['port'] = $parts['port'];
|
||||
}
|
||||
if (isset($parts['query'])) {
|
||||
parse_str($parts['query'], $query);
|
||||
foreach ($options as $key => $value) {
|
||||
if (isset($query[$key])) {
|
||||
$options[$key] = $query[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($options['transport'])) {
|
||||
$options['transport'] = 'null';
|
||||
} elseif ('gmail' === $options['transport']) {
|
||||
$options['encryption'] = 'ssl';
|
||||
$options['auth_mode'] = 'login';
|
||||
$options['host'] = 'smtp.gmail.com';
|
||||
$options['transport'] = 'smtp';
|
||||
}
|
||||
|
||||
if (!isset($options['port'])) {
|
||||
$options['port'] = 'ssl' === $options['encryption'] ? 465 : 25;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue