Eventor
A sane interface to PHP's libevent Extension, (*1)
Install
Install it via composer:, (*2)
{
"require": {
"chh/eventor": "*"
}
}
Then do:, (*3)
# Only if you don't have composer:
% wget http://getcomposer.org/composer.phar
% php composer.phar install
Example
<?php
$base = new Eventor\Base;
$fd = STDIN;
# Call the callback every time data is
# written to $fd.
$event = $base->read($fd, function($event) {
static $requests = 0;
$requests++;
if ($requests == 10) {
$event->base->exit();
}
printf("You wrote: %s", fgets($event->fd));
});
$base->loop();
API
class Eventor
Methods
Eventor\Event read(resource $fd, callable $callback)
Creates a new read event and registers it in the event loop with the
given $callback., (*4)
When data is written to $fd the callback is called and receives an instance of Eventor\Event as sole argument., (*5)
Eventor\Event write(resource $fd, callable $callback)
Creates a new write event and registers it in the event loop with the
given $callback., (*6)
When data can be written to $fd the callback is called and receives an instance of
Eventor\Event as sole argument., (*7)
add(Eventor\Event $event)
Register a manually created Event instance in the event loop., (*8)
delete(Eventor\Event $event)
Delete the event from the event loop, which causes it to trigger
the callback never again., (*9)
loop()
Starts the event loop, this blocks until all events are dispatched (if the events
are not persistent) or one of the breakLoop() or exit() methods is called
on the Eventor\Base., (*10)
halt()
Breaks the loop immediately, similar to using the break keyword in a loop., (*11)
shutdown(int $timeout = -1)
Gracefully shut down the event loop by finishing the next dispatch and
exiting after the timeout. The $timeout argument receives the time in
microseconds when the loop should exit, when omitted the loop is exited after
the next dispatch., (*12)
class Eventor\Event
The Event class is useful for more low level access to the libevent extensions'
functionality and is also passed to the event handlers when they're called., (*13)
Methods
__construct(resource $fd, int $events, callable $callback)
Initializes the event instance with the file descriptor and the
flags. Events is an integer consisting of one or more of these constants
defined in the Eventor\Event class:, (*14)
-
Event::READ, listens on writes to the file descriptor.
Event::WRITE
-
Event::SIGNAL, the file descriptor is handled as signal.
-
Event::TIMEOUT, triggers the event after a timeout.
-
Event::PERSIST, event is not deleted from the base after the
handler was called. This is set by default, you have to call
setNonPersistent() to disable this flag.
Example:, (*15)
<?php
use Eventor\Event,
Eventor\Base;
$base = new Base;
$event = new Event(STDIN, Event::READ, function($event) {
echo fgets($event->fd);
});
$base->add($event);
$base->loop();
setNonPersistent($enable = true)
Sets the event to non-persistent mode, by registering it without setting
the Eventor\Event::PERSIST flag. This causes the event handler to get triggered only once., (*16)
Properties
base
The instance of the Eventor\Base, to which this event is bound. Is null in the case this event is not bound to any base., (*17)
callback
The callback which gets called when the event is ready., (*18)
fd
The fd property is the file descriptor, to which this event is bound., (*19)
License
Copyright © 2012 Christoph Hochstrasser, (*20)
Eventor is licensed under the MIT license which is bundled with this
package in the file LICENSE.txt., (*21)