|
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
|
|
|
public function on($event, callable $listener) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->listeners[$event][] = $listener; |
|
49
|
|
|
$this->client->getWsClient()->on($event, $listener); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $event |
|
54
|
|
|
* @param callable $listener |
|
55
|
|
|
*/ |
|
56
|
|
|
public function once($event, callable $listener) |
|
57
|
|
|
{ |
|
58
|
|
|
$onceListener = function () use (&$onceListener, $event, $listener) { |
|
59
|
|
|
$this->removeListener($event, $onceListener); |
|
60
|
|
|
|
|
61
|
|
|
call_user_func_array($listener, func_get_args()); |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
|
|
$this->on($event, $onceListener); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $event |
|
69
|
|
|
* @param callable $listener |
|
70
|
|
|
*/ |
|
71
|
|
|
public function removeListener($event, callable $listener) |
|
72
|
|
|
{ |
|
73
|
|
|
if (isset($this->listeners[$event])) { |
|
74
|
|
|
if (false !== $index = array_search($listener, $this->listeners[$event], true)) { |
|
75
|
|
|
unset($this->listeners[$event][$index]); |
|
76
|
|
|
$this->client->getWsClient()->removeListener($event, $listener); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $event |
|
83
|
|
|
*/ |
|
84
|
|
|
public function removeAllListeners($event = null) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($event !== null) { |
|
87
|
|
|
if (isset($this->listeners[$event])) { |
|
88
|
|
|
unset($this->listeners[$event]); |
|
89
|
|
|
$this->client->getWsClient()->removeAllListeners($event); |
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
foreach ($this->listeners as $event => $listeners) { |
|
93
|
|
|
$this->client->getWsClient()->removeAllListeners($event); |
|
94
|
|
|
} |
|
95
|
|
|
$this->listeners = []; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
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
|
|
|
public function __construct(AriClient $client, $response) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->client = $client; |
|
115
|
|
|
|
|
116
|
|
|
parent::__construct($response); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function __destruct() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->removeAllListeners(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|