hoa/event
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Hoa\Event is a comprehensive PHP library dedicated to providing a robust implementation of the observer design-pattern. This makes it possible to utilise events and listeners in PHP. The library enables both asynchronous and anonymous events, as well as synchronous and identified listeners. These areas cater to a broad span of data diffusion needs, ranging from isolated to close-knit components.
As for how you use Hoa\Event, it's remarkably straightforward and flexible due to its implementation of callable. First, install it with Composer using the command composer require hoa/event '~2.0'
.
To observe anomalies like exceptions or streams about to close, you can utilise code such as;
Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(
function (Hoa\Event\Bucket $bucket) {
var_dump(
$bucket->getSource(),
$bucket->getData()
);
}
);
This block of code sets up a watching eye on any exception thrown. You can streamline the process further by attaching a stream directly to an event;
Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(
new Hoa\File\Write('Foo.log')
);
Now, every exception will be logged onto the Foo.log
file.
For listeners, they ought to be synchronous and identified. To exemplify, if you have a WebSocket server and you wanted to listen to the message listener ID, the following code does the trick;
$server = new Hoa\Websocket\Server(β¦);
$server->on('message', function (Hoa\Event\Bucket $bucket) {
var_dump(
$bucket->getSource(),
$bucket->getData()
);
});
The closure is executed every time a message is received by the WebSocket server.
Hoa\Event's documentation can be found at Hoa\Event documentation. The documents discuss in detail how to utilise this library and delve into how it works. Information on how to generate the docs locally is also provided if you prefer that. Furthermore, to help your understanding of Hoa libraries, they also provide help through their IRC channel or forum at users.hoa-project.net. If interested in contributing, steps are outlined in the contributor guide.