1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2014 Brian Smith <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace phparia\Resources; |
20
|
|
|
|
21
|
|
|
use phparia\Client\AriClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Base type ARI resources |
25
|
|
|
* |
26
|
|
|
* The event code is a wrapper to isolate the listeners for the particular resource |
27
|
|
|
* |
28
|
|
|
* @author Brian Smith <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Resource extends Response |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $listeners = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AriClient |
39
|
|
|
*/ |
40
|
|
|
protected $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $event |
44
|
|
|
* @param callable $listener |
45
|
|
|
*/ |
46
|
3 |
|
public function on($event, callable $listener) |
47
|
|
|
{ |
48
|
3 |
|
$this->listeners[$event][] = $listener; |
49
|
3 |
|
$this->client->getWsClient()->on($event, $listener); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $event |
54
|
|
|
* @param callable $listener |
55
|
|
|
*/ |
56
|
|
|
public function once($event, callable $listener) |
57
|
|
|
{ |
58
|
3 |
|
$onceListener = function () use (&$onceListener, $event, $listener) { |
59
|
2 |
|
$this->removeListener($event, $onceListener); |
60
|
|
|
|
61
|
2 |
|
call_user_func_array($listener, func_get_args()); |
62
|
3 |
|
}; |
63
|
|
|
|
64
|
3 |
|
$this->on($event, $onceListener); |
65
|
3 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $event |
69
|
|
|
* @param callable $listener |
70
|
|
|
*/ |
71
|
2 |
|
public function removeListener($event, callable $listener) |
72
|
|
|
{ |
73
|
2 |
|
if (isset($this->listeners[$event])) { |
74
|
2 |
|
if (false !== $index = array_search($listener, $this->listeners[$event], true)) { |
75
|
2 |
|
unset($this->listeners[$event][$index]); |
76
|
2 |
|
$this->client->getWsClient()->removeListener($event, $listener); |
77
|
2 |
|
} |
78
|
2 |
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $event |
83
|
|
|
*/ |
84
|
50 |
|
public function removeAllListeners($event = null) |
85
|
|
|
{ |
86
|
50 |
|
if ($event !== null) { |
87
|
|
|
if (isset($this->listeners[$event])) { |
88
|
|
|
unset($this->listeners[$event]); |
89
|
|
|
$this->client->getWsClient()->removeAllListeners($event); |
90
|
|
|
} |
91
|
|
|
} else { |
92
|
50 |
|
foreach ($this->listeners as $event => $listeners) { |
93
|
1 |
|
$this->client->getWsClient()->removeAllListeners($event); |
94
|
50 |
|
} |
95
|
50 |
|
$this->listeners = []; |
96
|
|
|
} |
97
|
50 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $event |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function listeners($event) |
104
|
|
|
{ |
105
|
|
|
return isset($this->listeners[$event]) ? $this->listeners[$event] : []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param AriClient $client |
110
|
|
|
* @param string $response The raw json response response data from ARI |
111
|
|
|
*/ |
112
|
55 |
|
public function __construct(AriClient $client, $response) |
113
|
|
|
{ |
114
|
55 |
|
$this->client = $client; |
115
|
|
|
|
116
|
55 |
|
parent::__construct($response); |
117
|
55 |
|
} |
118
|
|
|
|
119
|
50 |
|
public function __destruct() |
120
|
|
|
{ |
121
|
50 |
|
$this->removeAllListeners(); |
122
|
50 |
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|