Events
Events provide a coupling-free mechanism that can be useful for transmitting data without needing to write complex structures.
Create a event
use Inphinit\Event;
Event::on('event1', function ($arg1, $arg2) {
print_r([$arg1, $arg2]);
});
// trigger event on route
$app->action('ANY', '/foo', function () {
Event::trigger('event1', ['param1', microtime(true)]);
});
Standard events
| Event | Description |
|---|---|
changestatus |
This event is always triggered when Inphinit\Http\Response::status() is executed. |
done |
It is triggered when a route has finished being processed. |
error |
It is triggered for all errors and warnings, and its behavior depends on how error_reporting is configured. |
maintenance |
This event will always be triggered when in maintenance mode. If a function added to this event returns return false, maintenance mode will be disabled at runtime, see: Bypass maintenance.
|
Exemplo de uso:
use Inphinit\Event;
Event::on('done', function () {
echo 'Response done';
});
Error event
The error event receives various parameters and can be useful for creating custom error logs:
use Inphinit\Event;
Event::on('error', function ($type, $message, $file, $line) {
var_dump($type, $message, $file, $line);
});
Http Status event
The changestatus event receives the new code and the previous one:
use Inphinit\Event;
Event::on('changestatus', function ($new_code, $previous_code) {
var_dump($new_code, $previous_code);
});
Priority of events defined
use Inphinit\Event;
// No priority specified (priority = 0)
Event::on('foobar', function ($arg1, $arg2) {
echo "1° function: {$arg1}, {$arg2} (priority = 0)<br>";
});
// No priority specified (priority = 0)
Event::on('foobar', function ($arg1, $arg2) {
echo "2° function: {$arg1}, {$arg2} (priority = 0)<br>";
});
// Low priority (priority = -20)
Event::on('foobar', function ($arg1, $arg2) {
echo "3° function: {$arg1}, {$arg2} (priority = -20)<br>";
}, -20);
// (priority = -300)
Event::on('foobar', function ($arg1, $arg2) {
echo "4° function: {$arg1}, {$arg2} (priority = -300)<br>";
}, -300);
// High priority (priority = 1)
Event::on('foobar', function ($arg1, $arg2) {
echo "5° function: {$arg1}, {$arg2} (priority = HIGH_PRIORITY)<br>";
}, Event::HIGH_PRIORITY);
// (priority = 600)
Event::on('foobar', function ($arg1, $arg2) {
echo "6° function: {$arg1}, {$arg2} (priority = 600)<br>";
}, 600);
// Low priority (priority = -1)
Event::on('foobar', function ($arg1, $arg2) {
echo "7° function: {$arg1}, {$arg2} (priority = LOW_PRIORITY)<br>";
}, Event::LOW_PRIORITY);
Event::trigger('foobar', ['foo', 'bar']);
Output:
6° function: foo, bar (priority = 600)
5° function: foo, bar (priority = HIGH_PRIORITY)
1° function: foo, bar (priority = 0)
2° function: foo, bar (priority = 0)
7° function: foo, bar (priority = LOW_PRIORITY)
3° function: foo, bar (priority = -20)
4° function: foo, bar (priority = -300)
Handle the propagation
If one of the callbacks added to the events executes return false;, the propagation will interrupt all subsequent events of the same call. In the following example, the fourth function (with priority -300) will not be executed because the third function interrupted the propagation:
use Inphinit\Event;
// No priority specified (priority = 0)
Event::on('foobar', function ($arg1, $arg2) {
echo "1° function: {$arg1}, {$arg2} (priority = 0)<br>";
});
// No priority specified (priority = 0)
Event::on('foobar', function ($arg1, $arg2) {
echo "2° function: {$arg1}, {$arg2} (priority = 0)<br>";
});
// Low priority (priority = -20)
Event::on('foobar', function ($arg1, $arg2) {
echo "3° function: {$arg1}, {$arg2} (priority = -20)<br>";
// Stop propagation
return false;
}, -20);
// (priority = -300)
// Note: It will NOT be executed because a higher-priority event stopped the propagation
Event::on('foobar', function ($arg1, $arg2) {
echo "4° function: {$arg1}, {$arg2} (priority = -300)<br>";
}, -300);
// High priority (priority = 1)
Event::on('foobar', function ($arg1, $arg2) {
echo "5° function: {$arg1}, {$arg2} (priority = HIGH_PRIORITY)<br>";
}, Event::HIGH_PRIORITY);
// (priority = 600)
Event::on('foobar', function ($arg1, $arg2) {
echo "6° function: {$arg1}, {$arg2} (priority = 600)<br>";
}, 600);
// Low priority (priority = -1)
Event::on('foobar', function ($arg1, $arg2) {
echo "7° function: {$arg1}, {$arg2} (priority = LOW_PRIORITY)<br>";
}, Event::LOW_PRIORITY);
Event::trigger('foobar', ['foo', 'bar']);
Output:
6° function: foo, bar (priority = 600)
5° function: foo, bar (priority = HIGH_PRIORITY)
1° function: foo, bar (priority = 0)
2° function: foo, bar (priority = 0)
7° function: foo, bar (priority = LOW_PRIORITY)
3° function: foo, bar (priority = -20)