Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
# API
2
 
3
The API that événement exposes is defined by the
4
`Evenement\EventEmitterInterface`. The interface is useful if you want to
5
define an interface that extends the emitter and implicitly defines certain
6
events to be emitted, or if you want to type hint an `EventEmitter` to be
7
passed to a method without coupling to the specific implementation.
8
 
9
## on($event, callable $listener)
10
 
11
Allows you to subscribe to an event.
12
 
13
Example:
14
 
15
```php
16
$emitter->on('user.created', function (User $user) use ($logger) {
17
    $logger->log(sprintf("User '%s' was created.", $user->getLogin()));
18
});
19
```
20
 
21
Since the listener can be any callable, you could also use an instance method
22
instead of the anonymous function:
23
 
24
```php
25
$loggerSubscriber = new LoggerSubscriber($logger);
26
$emitter->on('user.created', array($loggerSubscriber, 'onUserCreated'));
27
```
28
 
29
This has the benefit that listener does not even need to know that the emitter
30
exists.
31
 
32
You can also accept more than one parameter for the listener:
33
 
34
```php
35
$emitter->on('numbers_added', function ($result, $a, $b) {});
36
```
37
 
38
## once($event, callable $listener)
39
 
40
Convenience method that adds a listener which is guaranteed to only be called
41
once.
42
 
43
Example:
44
 
45
```php
46
$conn->once('connected', function () use ($conn, $data) {
47
    $conn->send($data);
48
});
49
```
50
 
51
## emit($event, array $arguments = [])
52
 
53
Emit an event, which will call all listeners.
54
 
55
Example:
56
 
57
```php
58
$conn->emit('data', [$data]);
59
```
60
 
61
The second argument to emit is an array of listener arguments. This is how you
62
specify more args:
63
 
64
```php
65
$result = $a + $b;
66
$emitter->emit('numbers_added', [$result, $a, $b]);
67
```
68
 
69
## listeners($event)
70
 
71
Allows you to inspect the listeners attached to an event. Particularly useful
72
to check if there are any listeners at all.
73
 
74
Example:
75
 
76
```php
77
$e = new \RuntimeException('Everything is broken!');
78
if (0 === count($emitter->listeners('error'))) {
79
    throw $e;
80
}
81
```
82
 
83
## removeListener($event, callable $listener)
84
 
85
Remove a specific listener for a specific event.
86
 
87
## removeAllListeners($event = null)
88
 
89
Remove all listeners for a specific event or all listeners all together. This
90
is useful for long-running processes, where you want to remove listeners in
91
order to allow them to get garbage collected.