|
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 $_listeners; |
|
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) |
|
38
|
|
|
{ |
|
39
|
19 |
|
$this->exchangeStream($decorated); |
|
40
|
19 |
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function isReadable() |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->decorated->isReadable(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function pause() |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->decorated->pause(); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function resume() |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->decorated->resume(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function pipe(WritableStreamInterface $destination, array $options = array()) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->decorated->pipe($destination, $options); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function close() |
|
63
|
|
|
{ |
|
64
|
1 |
|
$this->decorated->close(); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function isWritable() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->decorated->isWritable(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function write($data) |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->decorated->write($data); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function end($data = null) |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->decorated->end($data); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
19 |
|
public function exchangeStream(DuplexStreamInterface $stream) |
|
83
|
|
|
{ |
|
84
|
19 |
|
static $events = ['data', 'end', 'drain', 'error', 'close', 'pipe']; |
|
85
|
|
|
|
|
86
|
19 |
|
if($this->decorated !== null) { |
|
87
|
6 |
|
$this->unsubscribe($this->decorated, $events); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
19 |
|
$this->subscribe($stream, $events); |
|
91
|
19 |
|
$this->decorated = $stream; |
|
92
|
19 |
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
private function unsubscribe(DuplexStreamInterface $stream, array $events) |
|
95
|
|
|
{ |
|
96
|
6 |
|
foreach ($events as $event) { |
|
97
|
6 |
|
if(!isset($this->_listeners[$event])) { |
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
$stream->removeListener($event, $this->_listeners[$event]); |
|
102
|
|
|
} |
|
103
|
6 |
|
} |
|
104
|
|
|
|
|
105
|
19 |
|
private function subscribe(DuplexStreamInterface $stream, array $events) |
|
106
|
|
|
{ |
|
107
|
19 |
|
foreach ($events as $event) { |
|
108
|
19 |
|
if(!isset($this->_listeners[$event])) { |
|
109
|
11 |
|
$this->_listeners[$event] = function (...$arguments) use ($event) { |
|
110
|
11 |
|
$this->emit($event, $arguments); |
|
111
|
11 |
|
}; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
19 |
|
$stream->on($event, $this->_listeners[$event]); |
|
115
|
|
|
} |
|
116
|
19 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|