|
12 | 12 | namespaceSymfony\Bridge\Monolog\Handler; |
13 | 13 |
|
14 | 14 | useMonolog\Handler\SwiftMailerHandlerasBaseSwiftMailerHandler; |
| 15 | +useMonolog\Logger; |
15 | 16 | useSymfony\Component\Console\Event\ConsoleTerminateEvent; |
16 | 17 | useSymfony\Component\HttpKernel\Event\PostResponseEvent; |
17 | 18 |
|
18 | | -/** |
19 | | - * Extended SwiftMailerHandler that flushes mail queue if necessary. |
20 | | - * |
21 | | - * @author Philipp Kräutli <pkraeutli@astina.ch> |
22 | | - * |
23 | | - * @final since Symfony 4.3 |
24 | | - */ |
25 | | -class SwiftMailerHandlerextends BaseSwiftMailerHandler |
26 | | -{ |
27 | | -protected$transport; |
28 | | - |
29 | | -protected$instantFlush =false; |
30 | | - |
31 | | -publicfunctionsetTransport(\Swift_Transport$transport) |
32 | | - { |
33 | | -$this->transport =$transport; |
34 | | - } |
35 | | - |
| 19 | +if (Logger::API <2) { |
36 | 20 | /** |
37 | | - * After the kernel has been terminated we will always flush messages. |
| 21 | + * Extended SwiftMailerHandler that flushes mail queue if necessary. |
| 22 | + * |
| 23 | + * @author Philipp Kräutli <pkraeutli@astina.ch> |
| 24 | + * |
| 25 | + * @final since Symfony 4.3 |
38 | 26 | */ |
39 | | -publicfunctiononKernelTerminate(PostResponseEvent$event) |
| 27 | +class SwiftMailerHandlerextends BaseSwiftMailerHandler |
40 | 28 | { |
41 | | -$this->instantFlush =true; |
42 | | - } |
| 29 | +protected$transport; |
43 | 30 |
|
44 | | -/** |
45 | | - * After the CLI application has been terminated we will always flush messages. |
46 | | - */ |
47 | | -publicfunctiononCliTerminate(ConsoleTerminateEvent$event) |
48 | | - { |
49 | | -$this->instantFlush =true; |
50 | | - } |
| 31 | +protected$instantFlush =false; |
51 | 32 |
|
52 | | -/** |
53 | | - * {@inheritdoc} |
54 | | - */ |
55 | | -protectedfunctionsend($content,array$records) |
56 | | - { |
57 | | -parent::send($content,$records); |
| 33 | +publicfunctionsetTransport(\Swift_Transport$transport) |
| 34 | + { |
| 35 | +$this->transport =$transport; |
| 36 | + } |
58 | 37 |
|
59 | | -if ($this->instantFlush) { |
| 38 | +/** |
| 39 | + * After the kernel has been terminated we will always flush messages. |
| 40 | + */ |
| 41 | +publicfunctiononKernelTerminate(PostResponseEvent$event) |
| 42 | + { |
| 43 | +$this->instantFlush =true; |
| 44 | + } |
| 45 | + |
| 46 | +/** |
| 47 | + * After the CLI application has been terminated we will always flush messages. |
| 48 | + */ |
| 49 | +publicfunctiononCliTerminate(ConsoleTerminateEvent$event) |
| 50 | + { |
| 51 | +$this->instantFlush =true; |
| 52 | + } |
| 53 | + |
| 54 | +/** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | +protectedfunctionsend($content,array$records) |
| 58 | + { |
| 59 | +parent::send($content,$records); |
| 60 | + |
| 61 | +if ($this->instantFlush) { |
| 62 | +$this->flushMemorySpool(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +/** |
| 67 | + * {@inheritdoc} |
| 68 | + */ |
| 69 | +publicfunctionreset() |
| 70 | + { |
60 | 71 | $this->flushMemorySpool(); |
61 | 72 | } |
62 | | - } |
63 | 73 |
|
64 | | -/** |
65 | | - * {@inheritdoc} |
66 | | - */ |
67 | | -publicfunctionreset() |
68 | | - { |
69 | | -$this->flushMemorySpool(); |
70 | | - } |
| 74 | +/** |
| 75 | + * Flushes the mail queue if a memory spool is used. |
| 76 | + */ |
| 77 | +privatefunctionflushMemorySpool() |
| 78 | + { |
| 79 | +$mailerTransport =$this->mailer->getTransport(); |
| 80 | +if (!$mailerTransportinstanceof \Swift_Transport_SpoolTransport) { |
| 81 | +return; |
| 82 | + } |
71 | 83 |
|
| 84 | +$spool =$mailerTransport->getSpool(); |
| 85 | +if (!$spoolinstanceof \Swift_MemorySpool) { |
| 86 | +return; |
| 87 | + } |
| 88 | + |
| 89 | +if (null ===$this->transport) { |
| 90 | +thrownew \Exception('No transport available to flush mail queue'); |
| 91 | + } |
| 92 | + |
| 93 | +$spool->flushQueue($this->transport); |
| 94 | + } |
| 95 | + } |
| 96 | +}else { |
72 | 97 | /** |
73 | | - * Flushes the mail queue if a memory spool is used. |
| 98 | + * Extended SwiftMailerHandler that flushes mail queue if necessary. |
| 99 | + * |
| 100 | + * @author Philipp Kräutli <pkraeutli@astina.ch> |
| 101 | + * |
| 102 | + * @final since Symfony 4.3 |
74 | 103 | */ |
75 | | -privatefunctionflushMemorySpool() |
| 104 | +class SwiftMailerHandlerextends BaseSwiftMailerHandler |
76 | 105 | { |
77 | | -$mailerTransport =$this->mailer->getTransport(); |
78 | | -if (!$mailerTransportinstanceof \Swift_Transport_SpoolTransport) { |
79 | | -return; |
| 106 | +protected$transport; |
| 107 | + |
| 108 | +protected$instantFlush =false; |
| 109 | + |
| 110 | +publicfunctionsetTransport(\Swift_Transport$transport) |
| 111 | + { |
| 112 | +$this->transport =$transport; |
| 113 | + } |
| 114 | + |
| 115 | +/** |
| 116 | + * After the kernel has been terminated we will always flush messages. |
| 117 | + */ |
| 118 | +publicfunctiononKernelTerminate(PostResponseEvent$event) |
| 119 | + { |
| 120 | +$this->instantFlush =true; |
80 | 121 | } |
81 | 122 |
|
82 | | -$spool =$mailerTransport->getSpool(); |
83 | | -if (!$spoolinstanceof \Swift_MemorySpool) { |
84 | | -return; |
| 123 | +/** |
| 124 | + * After the CLI application has been terminated we will always flush messages. |
| 125 | + */ |
| 126 | +publicfunctiononCliTerminate(ConsoleTerminateEvent$event) |
| 127 | + { |
| 128 | +$this->instantFlush =true; |
85 | 129 | } |
86 | 130 |
|
87 | | -if (null ===$this->transport) { |
88 | | -thrownew \Exception('No transport available to flush mail queue'); |
| 131 | +/** |
| 132 | + * {@inheritdoc} |
| 133 | + */ |
| 134 | +protectedfunctionsend(string$content,array$records):void |
| 135 | + { |
| 136 | +parent::send($content,$records); |
| 137 | + |
| 138 | +if ($this->instantFlush) { |
| 139 | +$this->flushMemorySpool(); |
| 140 | + } |
89 | 141 | } |
90 | 142 |
|
91 | | -$spool->flushQueue($this->transport); |
| 143 | +/** |
| 144 | + * {@inheritdoc} |
| 145 | + */ |
| 146 | +publicfunctionreset() |
| 147 | + { |
| 148 | +$this->flushMemorySpool(); |
| 149 | + } |
| 150 | + |
| 151 | +/** |
| 152 | + * Flushes the mail queue if a memory spool is used. |
| 153 | + */ |
| 154 | +privatefunctionflushMemorySpool() |
| 155 | + { |
| 156 | +$mailerTransport =$this->mailer->getTransport(); |
| 157 | +if (!$mailerTransportinstanceof \Swift_Transport_SpoolTransport) { |
| 158 | +return; |
| 159 | + } |
| 160 | + |
| 161 | +$spool =$mailerTransport->getSpool(); |
| 162 | +if (!$spoolinstanceof \Swift_MemorySpool) { |
| 163 | +return; |
| 164 | + } |
| 165 | + |
| 166 | +if (null ===$this->transport) { |
| 167 | +thrownew \Exception('No transport available to flush mail queue'); |
| 168 | + } |
| 169 | + |
| 170 | +$spool->flushQueue($this->transport); |
| 171 | + } |
92 | 172 | } |
93 | 173 | } |