PHP code example of pollen-solutions / event

1. Go to this page and download the library: Download pollen-solutions/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/ */

    

pollen-solutions / event example snippets


use Pollen\Event\EventDispatcher;

// Create Dispatcher
$dispatcher = new EventDispatcher();

// Subscribe events
$dispatcher->on('event.demo', function () {
    var_dump('one');
});
$dispatcher->on('event.demo', function () {
    var_dump('two');
});

// Dispatch events
$dispatcher->trigger('event.demo');

// Output
// >> (string) 'one'
// >> (string) 'two' 

use Pollen\Event\EventDispatcher;

// Create Dispatcher
$dispatcher = new EventDispatcher();

// Subscribe events
$dispatcher->on('event.demo', function () {
    var_dump('one');
}, 10);
$dispatcher->on('event.demo', function () {
    var_dump('two');
}, 20);

// Dispatch events
$dispatcher->trigger('event.demo');

// Output
// >> (string) 'two'
// >> (string) 'one' 

use Pollen\Event\EventDispatcher;
use Pollen\Event\StoppableEvent;

// Create Dispatcher
$dispatcher = new EventDispatcher();

// Subscribe events
$dispatcher->on('event.demo', function (StoppableEvent $e) {
    $e->stopPropagation();
    var_dump('one');
});
$dispatcher->on('event.demo', function () {
    var_dump('two');
});

// Dispatch events
$dispatcher->trigger('event.demo');

// Output
// >> (string) 'one' 

use Pollen\Event\EventDispatcher;
use Pollen\Event\TriggeredEvent;

// Create Dispatcher
$dispatcher = new EventDispatcher();

// Subscribe events
$dispatcher->on('event.demo', function (TriggeredEvent $e, $arg1, $arg2) {
    var_dump('one', $arg1, $arg2);
    $e->setEventArgs(['newValue1', 'newValue2']);
});
$dispatcher->on('event.demo', function (TriggeredEvent $e, $arg1, $arg2) {
    var_dump('two', $arg1, $arg2);
});

// Dispatch events
$dispatcher->trigger('event.demo', ['value1', 'value2']);

// Output
// >> (string) 'one' 
// >> (string) 'value1'
// >> (string) 'value2'
// >> (string) 'two' 
// >> (string) 'newValue1'
// >> (string) 'newValue2'

use Pollen\Event\EventDispatcher;

// Create Dispatcher
$dispatcher = new EventDispatcher();

// Subscribe events
$dispatcher->one('event.demo', function () {
    var_dump('one');
});
$dispatcher->on('event.demo', function () {
    var_dump('two');
});

// Dispatch events
// First
$dispatcher->trigger('event.demo');
// Second
$dispatcher->trigger('event.demo');

// Output
// First dispatch
// >> (string) 'one' 
// >> (string) 'two'
// Second dispatch
// >> (string) 'two'

namespace {
use Pollen\Container\Container;
use Pollen\Event\EventDispatcher;

// Container Declaration 
$container = new Container();

class ServiceDemoNamedClass
{
    public function __invoke($e, $v)
    {
        var_dump('one >> '. $v);
    }
}
$container->add(
    'container.service1',
    ServiceDemoNamedClass::class
);

class ServiceDemoClosuredClass
{
    public function __invoke($e, $v)
    {
        var_dump('two >> '. $v);
    }
}
$container->add(
    'container.service2',
    function () {
       return new ServiceDemoClosuredClass();
    }
);

// Class Declaration
class InvokableDemoClass
{
    public function __invoke($e, $v)
    {
        var_dump('three >> '. $v);
    }
}

class InstantiableDemoClass
{
    public function __invoke($e, $v)
    {
        var_dump('four >> '. $v);
    }
}

// Create Dispatcher
$dispatcher = new EventDispatcher([], $container);

// Subscribe events
// Good practice
$dispatcher->on('event.demo', 'container.service1');
$dispatcher->on('event.demo', 'container.service2');
$dispatcher->on('event.demo', InvokableDemoClass::class);
// Increased memory usage practice
$dispatcher->on('event.demo', new InstantiableDemoClass());

// Dispatch events
$dispatcher->trigger('event.demo', ['value']);
}