PHP code example of spiffy / spiffy-event

1. Go to this page and download the library: Download spiffy/spiffy-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spiffy / spiffy-event example snippets


use Spiffy\Event\Event;

// Create an event that fires on 'foo'
$event = new Event('foo');

// Creates an event with a target
$event = new Event('foo', 'target');
$event->getTarget(); // 'target'

// Event can have parameters too
$event = new Event('foo', 'target', ['foo' => 'bar']);
$event->getParams()['foo']; // 'bar'

use Spiffy\Event\EventManager;

$em = new EventManager();

// Listen with a priority of 1
$em->on('foo', function() { echo 'a'; }, 1);

// Listen with a higher priority
$em->on('foo', function() { echo 'b'; }, 10);

// Event default with priority 0
$em->on('foo', function() { echo 'c'; });
$em->on('foo', function() { echo 'd'; });

// echos 'bacd'

use Spiffy\Event\Event;
use Spiffy\Event\EventManager;

$em = new EventManager();
$em->on('foo', function() { echo 'fired'; });

// Simplest form of fire requiring just the type
$em->fire('foo'); // fired

// You can also specify the target and params when using the type
$em->fire('foo', 'target', ['foo' => 'bar']);

// You can also provide your own event.
// This is identical to the fire above.
$event = new Event('foo', 'target', ['foo' => 'bar']);
$em->fire($event);

use Spiffy\Event\Event;
use Spiffy\Event\EventManager;

$em = new EventManager();

// Respones are returned as a SplQueue (FIFO).
$em->on('foo', function() { return 'a'; });
$em->on('foo', function() { return 'b'; });

// Outputs 'ab'
foreach ($em->fire('foo') as $response) {
    echo $response;
}

use Spiffy\Event\Event;
use Spiffy\Event\Plugin;

class MyPlugin implements Plugin
{
    public function plug(Manager $events)
    {
        $events->on('foo', [$this, 'onFoo']);
    }

    public function onFoo(Event $e)
    {
        echo 'do foo';
    }
}

$em = new EventManager();
$em->attach(new MyPlugin());

// output is 'do foo'
$em->fire('foo');