carloscarucce/php-triggers

Create events and listeners using php

v1.4.2 2019-11-01 13:43 UTC

This package is auto-updated.

Last update: 2024-10-29 05:34:37 UTC


README

Create events and listeners using php

Instalation

All you have to do is run composer require carloscarucce/php-triggers or add the following to your composer.json:

{
  "require": {
    "carloscarucce/php-triggers": "1.*"
  }
}

How to use

First you have to setup a listener to your event.

  1. Declare listener:
use PhpTriggers\Event;
use PhpTriggers\EventListener;

class MyListener extends EventListener
{
    public function listen(Event $event, ...$data)
    {
        list($name, $lastName) = $data;
        
        echo "Welcome $name $lastName !";
    }

    public function __construct()
    {
        $this->listensTo('check-in');
    }
}
  1. Register listener:
$listener = new MyListener();
$listener->register();

Then all you have to do is trigger that event whenever you want to:

use PhpTriggers\Event;

Event::create('check-in', [
    'name' => 'John',
    'lastName' => 'Doe'
])->trigger();

Enjoy!