Skip to content

Commit 32dec1d

Browse files
committed
update
1 parent a613942 commit 32dec1d

File tree

6 files changed

+433
-2
lines changed

6 files changed

+433
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
vendor/
3+
js/
4+
composer.lock

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
1-
# emitter
2-
Event emitter component
1+
# Emitter
2+
3+
Event emitter component.
4+
5+
## Installation
6+
7+
```
8+
$ composer install socketio-php/emitter
9+
```
10+
11+
## Test
12+
13+
```shell
14+
➜ emitter git:(master) ✗ php vendor/bin/phpunit tests/EmitterTest.php
15+
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
16+
17+
.......... 10 / 10 (100%)
18+
19+
Time: 24 ms, Memory: 4.00 MB
20+
21+
OK (10 tests, 10 assertions)
22+
```
23+
24+
## API
25+
26+
### Emitter#on(event, fn)
27+
28+
Register an `event` handler `fn`.
29+
30+
### Emitter#once(event, fn)
31+
32+
Register a single-shot `event` handler `fn`,
33+
removed immediately after it is invoked the
34+
first time.
35+
36+
### Emitter#off(event, fn)
37+
38+
* Pass `event` and `fn` to remove a listener.
39+
* Pass `event` to remove all listeners on that event.
40+
* Pass nothing to remove all listeners on all events.
41+
42+
### Emitter#emit(event, ...)
43+
44+
Emit an `event` with variable option args.
45+
46+
### Emitter#listeners(event)
47+
48+
Return an array of callbacks, or an empty array.
49+
50+
### Emitter#hasListeners(event)
51+
52+
Check if this emitter has `event` handlers.
53+
54+
## License
55+
56+
MIT

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "socketio-php/emitter",
3+
"description": "event emitter",
4+
"type": "library",
5+
"require": {
6+
"php": ">=7.1.22"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "^7.5"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Emitter\\": "src/",
14+
"EmitterTest\\": "tests/"
15+
}
16+
},
17+
"license": "MIT"
18+
}

phpunit.xml.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<phpunit bootstrap="vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="parser">
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
</phpunit>

src/Emitter.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
namespace Emitter;
4+
5+
/**
6+
* Class Emitter
7+
*
8+
* @package Emitter
9+
*/
10+
class Emitter
11+
{
12+
private $callbacks = [];
13+
14+
/**
15+
* @param string $event
16+
* @param callable $fn
17+
*
18+
* @return Emitter
19+
*/
20+
public function on(string $event, callable $fn)
21+
{
22+
return $this->addEventListener($event, $fn);
23+
}
24+
25+
/**
26+
* @param string $event
27+
* @param callable $fn
28+
*
29+
* @return $this
30+
*/
31+
public function addEventListener(string $event, callable $fn)
32+
{
33+
if (!isset($this->callbacks['$' . $event])) {
34+
$this->callbacks['$' . $event] = [];
35+
}
36+
37+
array_push($this->callbacks['$' . $event], $fn);
38+
39+
return $this;
40+
}
41+
42+
/**
43+
* @param string $event
44+
* @param callable $fn
45+
*
46+
* @return $this
47+
*/
48+
public function once(string $event, callable $fn)
49+
{
50+
$on = function ($retFn = false) use ($event, $fn) {
51+
$arguments = func_get_args();
52+
if (is_bool(current($arguments))) {
53+
$arguments = array_slice($arguments, 1);
54+
}else{
55+
$retFn = false;
56+
}
57+
if (!$retFn) {
58+
$this->off($event, $fn);
59+
$fn(...$arguments);
60+
61+
return null;
62+
} else {
63+
return $fn;
64+
}
65+
};
66+
67+
$this->on($event, $on);
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* @param string $event
74+
* @param callable|null $fn
75+
*
76+
* @return Emitter
77+
*/
78+
public function off(string $event = '', ?callable $fn = null)
79+
{
80+
return $this->removeListener($event, $fn);
81+
}
82+
83+
/**
84+
* @param string $event
85+
* @param callable|null $fn
86+
*
87+
* @return Emitter
88+
*/
89+
public function removeListener(string $event = '', ?callable $fn = null)
90+
{
91+
return $this->removeAllListeners($event, $fn);
92+
}
93+
94+
/**
95+
* @param string $event
96+
* @param callable|null $fn
97+
*
98+
* @return Emitter
99+
*/
100+
public function removeAllListeners(string $event = '', ?callable $fn = null)
101+
{
102+
return $this->removeEventListener($event, $fn);
103+
}
104+
105+
/**
106+
* @param string $event
107+
* @param callable|null $fn
108+
*
109+
* @return $this
110+
*/
111+
public function removeEventListener(string $event = '', ?callable $fn = null)
112+
{
113+
// all
114+
$arguments = func_get_args();
115+
$arguments = array_filter($arguments);
116+
if (0 === count($arguments)) {
117+
$this->callbacks = [];
118+
119+
return $this;
120+
}
121+
122+
123+
if (!isset($this->callbacks['$' . $event])) return $this;
124+
125+
// specific event
126+
$callbacks = $this->callbacks['$' . $event];
127+
128+
// remove all handlers
129+
if (1 == count($arguments)) {
130+
unset($this->callbacks['$' . $event]);
131+
132+
return $this;
133+
}
134+
135+
// remove specific handler
136+
$cb = null;
137+
for ($i = 0; $i < count($callbacks); $i++) {
138+
$cb = $callbacks[$i];
139+
if ($cb === $fn || $cb(true) === $fn) {
140+
unset($callbacks[$i]);
141+
unset($this->callbacks['$' . $event]);
142+
break;
143+
}
144+
}
145+
146+
return $this;
147+
}
148+
149+
/**
150+
* @param string $event
151+
*/
152+
public function emit(string $event)
153+
{
154+
$arguments = func_get_args();
155+
$args = array_slice($arguments, 1);
156+
$callbacks = $this->callbacks['$' . $event];
157+
158+
if (!empty($callbacks)) {
159+
for ($i = 0, $len = count($callbacks); $i < $len; ++$i) {
160+
$callbacks[$i](...$args);
161+
}
162+
}
163+
}
164+
165+
/**
166+
* @param string $event
167+
*
168+
* @return array|mixed
169+
*/
170+
public function listeners(string $event)
171+
{
172+
return $this->callbacks['$' . $event] ?? [];
173+
}
174+
175+
/**
176+
* @param string $event
177+
*
178+
* @return bool
179+
*/
180+
public function hasListeners(string $event)
181+
{
182+
return count($this->listeners($event)) ? true : false;
183+
}
184+
185+
}

0 commit comments

Comments
 (0)