11<?php
22
33/*
4- * This file is part of the Monolog package.
4+ * This file is part of the Symfony package.
55 *
6- * (c) Jordi Boggiano <[email protected] > 6+ * (c) Fabien Potencier <[email protected] > 77 *
88 * For the full copyright and license information, please view the LICENSE
99 * file that was distributed with this source code.
1212namespace Symfony \Bundle \MonologBundle \Handler ;
1313
1414use Monolog \Handler \SwiftMailerHandler as BaseSwiftMailerHandler ;
15+ use Symfony \Component \HttpKernel \Event \PostResponseEvent ;
1516
1617/**
1718 * Extended SwiftMailerHandler that flushes mail queue if necessary
@@ -22,6 +23,8 @@ class SwiftMailerHandler extends BaseSwiftMailerHandler
2223{
2324 protected $ transport ;
2425
26+ protected $ instantFlush = false ;
27+
2528 /**
2629 * @param \Swift_Transport $transport
2730 */
@@ -30,20 +33,32 @@ public function setTransport(\Swift_Transport $transport)
3033 $ this ->transport = $ transport ;
3134 }
3235
36+ /**
37+ * After the kernel has been terminated we will always flush messages
38+ *
39+ * @param PostResponseEvent $event
40+ */
41+ public function onKernelTerminate (PostResponseEvent $ event )
42+ {
43+ $ this ->instantFlush = true ;
44+ }
45+
3346 /**
3447 * {@inheritdoc}
3548 */
3649 protected function send ($ content , array $ records )
3750 {
3851 parent ::send ($ content , $ records );
3952
40- $ this ->flushQueue ();
53+ if ($ this ->instantFlush ) {
54+ $ this ->flushMemorySpool ();
55+ }
4156 }
4257
4358 /**
44- * Flushes the mail queue if necessary
59+ * Flushes the mail queue if a memory spool is used
4560 */
46- private function flushQueue ()
61+ private function flushMemorySpool ()
4762 {
4863 $ transport = $ this ->mailer ->getTransport ();
4964 if (!$ transport instanceof \Swift_Transport_SpoolTransport) {
0 commit comments