Close search
Hoa

Hack book of Hoa\Event

Unfortunately, the documentation of this library has not been written yet. However, the README.md file may contain enough information to help you. This document is an extract of the README.md file.
Sorry for this inconvenience. Be ensured we are working hard to fix this.

Help on IRC Help on Gitter Documentation Board

This library allows to use events and listeners in PHP. This is an observer design-pattern implementation.

Learn more.

Quick usage

We propose a quick overview of how to use events and listeners.

Events

An event is:

In Hoa, an event channel has the following form: hoa://Event/LibraryName/AnId:pseudo-class#anAnchor. For instance, the hoa://Event/Exception channel contains all exceptions that have been thrown. The hoa://Event/Stream/StreamName:close-before contains all streams that are about to close. Thus, the following example will observe all thrown exceptions:

Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(
    function (Hoa\Event\Bucket $bucket) {
        var_dump(
            $bucket->getSource(),
            $bucket->getData()
        );
    }
);

Because attach expects a callable and because Hoa's callable implementation is smart, we can directly attach a stream to an event, like:

Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(
    new Hoa\File\Write('Foo.log')
);

This way, all exceptions will be printed on the Foo.log file.

Listeners

Contrary to an event, a listener is:

The Hoa\Event\Listenable interface requires the on method to be present to register a listener to a listener ID. For instance, the following example listens the message listener ID, i.e. when a message is received by the WebSocket server, the closure is executed:

$server = new Hoa\Websocket\Server(…);
$server->on('message', function (Hoa\Event\Bucket $bucket) {
    var_dump(
        $bucket->getSource(),
        $bucket->getData()
    );
});

An error or a suggestion about the documentation? Contributions are welcome!

Comments

menu