From 3fa62af06b9eb98cfb5a5a85ad4f7ec89ac1c8a2 Mon Sep 17 00:00:00 2001 From: Henrik Westphal Date: Mon, 2 Oct 2017 12:10:06 +0200 Subject: [PATCH] Fixed Swift Mailer integration --- .../Compiler/SwiftMailerPass.php | 46 +++++++++++++++++++ SncRedisBundle.php | 2 + SwiftMailer/RedisSpool.php | 12 +---- SwiftMailer/RedisSpool5.php | 28 +++++++++++ SwiftMailer/RedisSpool6.php | 28 +++++++++++ 5 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 DependencyInjection/Compiler/SwiftMailerPass.php create mode 100644 SwiftMailer/RedisSpool5.php create mode 100644 SwiftMailer/RedisSpool6.php diff --git a/DependencyInjection/Compiler/SwiftMailerPass.php b/DependencyInjection/Compiler/SwiftMailerPass.php new file mode 100644 index 00000000..fdad1773 --- /dev/null +++ b/DependencyInjection/Compiler/SwiftMailerPass.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Snc\RedisBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +class SwiftMailerPass implements CompilerPassInterface +{ + /** + * {@inheritdoc} + */ + public function process(ContainerBuilder $container) + { + $serviceId = 'snc_redis.swiftmailer.spool'; + if ($container->hasDefinition($serviceId)) { + $handlerDefinition = $container->getDefinition($serviceId); + if ('Snc\\RedisBundle\\SwiftMailer\\RedisSpool' === $handlerDefinition->getClass()) { + // default class, lets check for Swift Mailer version and set correct class + $class = new \ReflectionClass('\\Swift_Spool'); + $parameters = $class->getMethod('queueMessage')->getParameters(); + switch ($parameters[0]->getClass()->getName()) { + case 'Swift_Mime_Message': + // Swift Mailer 5.x + $handlerDefinition->setClass($handlerDefinition->getClass().'5'); + break; + case 'Swift_Mime_SimpleMessage': + // Swift Mailer 6.x + $handlerDefinition->setClass($handlerDefinition->getClass().'6'); + break; + default: + throw new \RuntimeException('Failed to detect the current Swift Mailer version.'); + } + } + } + } +} diff --git a/SncRedisBundle.php b/SncRedisBundle.php index 796588f6..66d0dddd 100644 --- a/SncRedisBundle.php +++ b/SncRedisBundle.php @@ -13,6 +13,7 @@ use Snc\RedisBundle\DependencyInjection\Compiler\LoggingPass; use Snc\RedisBundle\DependencyInjection\Compiler\MonologPass; +use Snc\RedisBundle\DependencyInjection\Compiler\SwiftMailerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -29,5 +30,6 @@ public function build(ContainerBuilder $container) parent::build($container); $container->addCompilerPass(new LoggingPass()); $container->addCompilerPass(new MonologPass()); + $container->addCompilerPass(new SwiftMailerPass()); } } diff --git a/SwiftMailer/RedisSpool.php b/SwiftMailer/RedisSpool.php index 6c0a45ad..98ba40bc 100644 --- a/SwiftMailer/RedisSpool.php +++ b/SwiftMailer/RedisSpool.php @@ -14,7 +14,7 @@ /** * RedisSpool */ -class RedisSpool extends \Swift_ConfigurableSpool +abstract class RedisSpool extends \Swift_ConfigurableSpool { /** * @var string @@ -64,16 +64,6 @@ public function isStarted() return true; } - /** - * {@inheritdoc} - */ - public function queueMessage(\Swift_Mime_SimpleMessage $message) - { - $this->redis->rpush($this->key, serialize($message)); - - return true; - } - /** * {@inheritdoc} */ diff --git a/SwiftMailer/RedisSpool5.php b/SwiftMailer/RedisSpool5.php new file mode 100644 index 00000000..9a2cdd8f --- /dev/null +++ b/SwiftMailer/RedisSpool5.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Snc\RedisBundle\SwiftMailer; + +/** + * RedisSpool5 (Swift Mailer 5.x) + */ +class RedisSpool5 extends RedisSpool +{ + /** + * {@inheritdoc} + */ + public function queueMessage(\Swift_Mime_Message $message) + { + $this->redis->rpush($this->key, serialize($message)); + + return true; + } +} diff --git a/SwiftMailer/RedisSpool6.php b/SwiftMailer/RedisSpool6.php new file mode 100644 index 00000000..4f2db8b8 --- /dev/null +++ b/SwiftMailer/RedisSpool6.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Snc\RedisBundle\SwiftMailer; + +/** + * RedisSpool6 (Swift Mailer 6.x) + */ +class RedisSpool6 extends RedisSpool +{ + /** + * {@inheritdoc} + */ + public function queueMessage(\Swift_Mime_SimpleMessage $message) + { + $this->redis->rpush($this->key, serialize($message)); + + return true; + } +}