1 | <?php |
||
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() |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function getStatus() |
||
106 | |||
107 | /** |
||
108 | * Check whether this Backend is run on the CLI. |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | protected function isCommandLineInterface() |
||
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.