1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\Backend; |
15
|
|
|
|
16
|
|
|
use Laminas\Diagnostics\Result\Success; |
17
|
|
|
use Sonata\NotificationBundle\Iterator\IteratorProxyMessageIterator; |
18
|
|
|
use Sonata\NotificationBundle\Model\MessageInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\Event; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This backend postpones the handling of messages to a registered event. |
24
|
|
|
* |
25
|
|
|
* It's based on the asynchronous event dispatcher: |
26
|
|
|
* |
27
|
|
|
* @see https://gist.github.com/3852361 |
28
|
|
|
* |
29
|
|
|
* @author Toni Uebernickel <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class PostponeRuntimeBackend extends RuntimeBackend |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var MessageInterface[] |
35
|
|
|
*/ |
36
|
|
|
protected $messages = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* If set to true, you have to fire an event the onEvent method is subscribed to manually! |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $postponeOnCli = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param bool $postponeOnCli Whether to postpone the messages on the CLI, too |
47
|
|
|
*/ |
48
|
|
|
public function __construct(EventDispatcherInterface $dispatcher, $postponeOnCli = false) |
49
|
|
|
{ |
50
|
|
|
parent::__construct($dispatcher); |
51
|
|
|
|
52
|
|
|
$this->postponeOnCli = $postponeOnCli; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function publish(MessageInterface $message): void |
59
|
|
|
{ |
60
|
|
|
// if the message is generated from the cli the message is handled |
61
|
|
|
// directly as there is no kernel.terminate in cli |
62
|
|
|
if (!$this->postponeOnCli && $this->isCommandLineInterface()) { |
63
|
|
|
$this->handle($message, $this->dispatcher); |
64
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->messages[] = $message; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Listen on any event and handle the messages. |
73
|
|
|
* |
74
|
|
|
* Actually, an event is not necessary, you can call this method manually, to. |
75
|
|
|
* The event is not processed in any way. |
76
|
|
|
*/ |
77
|
|
|
public function onEvent(?Event $event = null): void |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
while (!empty($this->messages)) { |
80
|
|
|
$message = array_shift($this->messages); |
81
|
|
|
|
82
|
|
|
$this->handle($message, $this->dispatcher); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function getIterator() |
90
|
|
|
{ |
91
|
|
|
return new IteratorProxyMessageIterator(new \ArrayIterator($this->messages)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getStatus() |
98
|
|
|
{ |
99
|
|
|
return new Success('Postpone runtime backend', 'Ok (Postpone Runtime)'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check whether this Backend is run on the CLI. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function isCommandLineInterface() |
108
|
|
|
{ |
109
|
|
|
return 'cli' === \PHP_SAPI; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.