Commit 32ca5b51 authored by Slavi Pantaleev's avatar Slavi Pantaleev

Do not care about `mailer` service changes

Until now we tried to detect `mailer` service changes,
and always flushed messages for them, just to be safe.

To keep things simple, we don't do this anymore.
If `mailer` gets replaced by another service, flushing will
fail as expected. People should not replace it.
parent efd3757d
......@@ -21,7 +21,6 @@ use Silex\ServiceProviderInterface;
*/
class SwiftmailerServiceProvider implements ServiceProviderInterface
{
private $mailerFactory;
private $mailerCreated;
public function register(Application $app)
......@@ -29,7 +28,7 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
$app['swiftmailer.options'] = array();
$self = $this;
$app['mailer'] = $this->mailerFactory = $app->share(function () use ($app, $self) {
$app['mailer'] = $app->share(function () use ($app, $self) {
$self->mailerCreated = true;
return new \Swift_Mailer($app['swiftmailer.spooltransport']);
});
......@@ -94,12 +93,11 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
$self = $this;
$app->finish(function () use ($app, $self) {
//To speed things up (by avoiding Swift Mailer initialization), flush messages only if
//our mailer has been "used" or got replaced by another service we have no knowledge off.
if ($app->raw('mailer') === $self->mailerFactory && !$self->mailerCreated) {
return;
//To speed things up (by avoiding Swift Mailer initialization), flush
//messages only if our mailer has been created (potentially used)
if ($self->mailerCreated) {
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
}
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
});
}
}
......@@ -62,7 +62,7 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertCount(0, $app['swiftmailer.spool']->getMessages());
}
public function testSwiftMailerAvoidsFlushesIfPossible()
public function testSwiftMailerAvoidsFlushesIfMailerIsUnused()
{
$app = new Application();
......@@ -82,54 +82,4 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->terminate($request, $response);
$this->assertFalse($app['swiftmailer.spool']->hasFlushed);
}
public function testSwiftMailerWillAlwaysFlushIfServiceChanges()
{
$app = new Application();
$app->register(new SwiftmailerServiceProvider());
$app->boot();
$app['swiftmailer.spool'] = $app->share(function () {
return new SpoolStub();
});
$app['mailer'] = $app->share(function () {
return null; //No need to actually return a mailer instance
});
$app->get('/', function() use ($app) { });
$request = Request::create('/');
$response = $app->handle($request);
$this->assertCount(0, $app['swiftmailer.spool']->getMessages());
$app->terminate($request, $response);
$this->assertTrue($app['swiftmailer.spool']->hasFlushed);
}
public function testSwiftMailerWillAlwaysFlushIfServiceIsExtended()
{
$app = new Application();
$app->register(new SwiftmailerServiceProvider());
$app->boot();
$app['swiftmailer.spool'] = $app->share(function () {
return new SpoolStub();
});
$app->extend('mailer', function ($mailer) {
return $mailer;
});
$app->get('/', function() use ($app) { });
$request = Request::create('/');
$response = $app->handle($request);
$this->assertCount(0, $app['swiftmailer.spool']->getMessages());
$app->terminate($request, $response);
$this->assertTrue($app['swiftmailer.spool']->hasFlushed);
}
}
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