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