1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* XMPP Library |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Utils; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use React\Stream\DuplexStreamInterface; |
20
|
|
|
use React\Stream\WritableStreamInterface; |
21
|
|
|
|
22
|
|
|
abstract class StreamDecorator implements DuplexStreamInterface |
23
|
|
|
{ |
24
|
|
|
use BetterEmitter; |
25
|
|
|
|
26
|
|
|
private $_redirectors; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DuplexStreamInterface |
30
|
|
|
*/ |
31
|
|
|
protected $_decorated = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* StreamDecorator constructor. |
35
|
|
|
* @param DuplexStreamInterface $decorated |
36
|
|
|
*/ |
37
|
19 |
|
public function __construct(DuplexStreamInterface $decorated = null) |
38
|
|
|
{ |
39
|
19 |
|
if($decorated !== null) { |
40
|
19 |
|
$this->exchangeStream($decorated); |
41
|
|
|
} |
42
|
19 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function isReadable() |
45
|
|
|
{ |
46
|
1 |
|
return $this->_decorated->isReadable(); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function pause() |
50
|
|
|
{ |
51
|
1 |
|
$this->_decorated->pause(); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
public function resume() |
55
|
|
|
{ |
56
|
1 |
|
$this->_decorated->resume(); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function pipe(WritableStreamInterface $destination, array $options = array()) |
60
|
|
|
{ |
61
|
1 |
|
$this->_decorated->pipe($destination, $options); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
public function close() |
65
|
|
|
{ |
66
|
1 |
|
$this->_decorated->close(); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function isWritable() |
70
|
|
|
{ |
71
|
1 |
|
return $this->_decorated->isWritable(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public function write($data) |
75
|
|
|
{ |
76
|
1 |
|
return $this->_decorated->write($data); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function end($data = null) |
80
|
|
|
{ |
81
|
1 |
|
return $this->_decorated->end($data); |
82
|
|
|
} |
83
|
|
|
|
84
|
19 |
|
public function exchangeStream(DuplexStreamInterface $stream) |
85
|
|
|
{ |
86
|
19 |
|
static $events = ['data', 'end', 'drain', 'error', 'close', 'pipe']; |
87
|
|
|
|
88
|
19 |
|
if($this->_decorated !== null) { |
89
|
6 |
|
$this->unsubscribe($this->_decorated, $events); |
90
|
|
|
} |
91
|
|
|
|
92
|
19 |
|
$this->subscribe($stream, $events); |
93
|
19 |
|
$this->_decorated = $stream; |
94
|
19 |
|
} |
95
|
|
|
|
96
|
6 |
|
private function unsubscribe(DuplexStreamInterface $stream, array $events) |
97
|
|
|
{ |
98
|
6 |
|
foreach ($events as $event) { |
99
|
6 |
|
if(!isset($this->_redirectors[$event])) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
$stream->removeListener($event, $this->_redirectors[$event]); |
104
|
|
|
} |
105
|
6 |
|
} |
106
|
|
|
|
107
|
19 |
|
private function subscribe(DuplexStreamInterface $stream, array $events) |
108
|
|
|
{ |
109
|
19 |
|
foreach ($events as $event) { |
110
|
19 |
|
if(!isset($this->_redirectors[$event])) { |
111
|
11 |
|
$this->_redirectors[$event] = function (...$arguments) use ($event) { |
112
|
11 |
|
$this->emit($event, $arguments); |
113
|
11 |
|
}; |
114
|
|
|
} |
115
|
|
|
|
116
|
19 |
|
$stream->on($event, $this->_redirectors[$event]); |
117
|
|
|
} |
118
|
19 |
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|