Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Flush spool queue for console terminate as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Luijk authored and fabpot committed Mar 27, 2015
1 parent e12be31 commit 8ecc11e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Silex/Provider/SwiftmailerServiceProvider.php
Expand Up @@ -14,6 +14,7 @@
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
Expand Down Expand Up @@ -92,12 +93,18 @@ public function register(Container $app)

public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addListener(KernelEvents::TERMINATE, function (PostResponseEvent $event) use ($app) {
$onTerminate = function (PostResponseEvent $event) use ($app) {
// To speed things up (by avoiding Swift Mailer initialization), flush
// messages only if our mailer has been created (potentially used)
if ($app['mailer.initialized']) {
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
}
});
};

$dispatcher->addListener(KernelEvents::TERMINATE, $onTerminate);

if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
$dispatcher->addListener(ConsoleEvents::TERMINATE, $onTerminate);
}
}
}

0 comments on commit 8ecc11e

Please sign in to comment.