Eventos

Os eventos fornecem um mecanismo livre de acoplamento que pode ser útil para transmitir dados sem a necessidade de escrever estruturas complexas.

Criar um evento

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)]); });

Eventos padrão

Evento Descrição
changestatus É acionado sempre que Inphinit\Http\Response::status() for executado.
done É acionado quando o processamento de uma rota for concluído.
error É acionado para todos os erros e avisos, e seu comportamento depende de como error_reporting está configurado.
maintenance Este evento será acionado em todas as solicitações quando o modo de manutenção estiver ativo. Se uma função adicionada a este evento retornar return false, o modo de manutenção será desativado em tempo de execução. Consulte: Ignorar manutenção.

Uso:

use Inphinit\Event; Event::on('done', function () { echo 'Response done'; });

Evento de erro

O evento error recebe vários parâmetros e pode ser útil para criar registros de erros personalizados:

use Inphinit\Event; Event::on('error', function ($type, $message, $file, $line) { var_dump($type, $message, $file, $line); });

Evento de status HTTP

O evento changestatus recebe o novo código e o anterior:

use Inphinit\Event; Event::on('changestatus', function ($new_code, $previous_code) { var_dump($new_code, $previous_code); });

Prioridade dos eventos definida

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']);

Saída:

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)

Gerencie a propagação

Se uma das funções callback adicionadas aos eventos tiver executado return false;, a propagação interromperá todos os eventos subsequentes da mesma chamada. No exemplo a seguir, a quarta função (com prioridade -300) não será executada porque a terceira função interrompeu a propagação:

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']);

Saída:

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)
Página inicial
Apoie com uma estrela no GitHub