Commit 3e2f70ed authored by Fabien Potencier's avatar Fabien Potencier

feature #1131 Flush spool queue for console terminate as well (dirkluijk)

This PR was squashed before being merged into the 2.0.x-dev branch (closes #1131).

Discussion
----------

Flush spool queue for console terminate as well

This allows to flush Swift messages automatically inside a Silex console application.

It basically does the same as the ``EmailSenderListener`` in Symfony's SwiftMailerBundle. (see https://github.com/symfony/SwiftmailerBundle/commit/4b90392463296acc9ccf8f10649d7ac4937ae0f3).

```php
use Symfony\Component\Console\Application;

$app = require __DIR__.'/app.php';

$console = new Application('My Silex Application', 'n/a');
$console->setDispatcher($app['dispatcher']);

// .. register commands that send emails

$console->run();
```

Commits
-------

8ecc11e6 Flush spool queue for console terminate as well
parents e12be31a 8ecc11e6
......@@ -14,6 +14,7 @@ namespace Silex\Provider;
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;
......@@ -92,12 +93,18 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListe
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);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment