PHP code example of alexanderc / open-fw-eventer

1. Go to this page and download the library: Download alexanderc/open-fw-eventer 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/ */

    

alexanderc / open-fw-eventer example snippets



use OpenFW\Events\Eventer;
use OpenFW\Events\Event;
use OpenFW\Events\Matchers\BinaryMatcher;
use OpenFW\Events\Matchers\RegexMatcher;
use OpenFW\Events\Traits\SimplifiedApiTrait;

$eventer = new Eventer();

$events = [
     'foo.bar.event',
     'foo.baz.smth',
     'foo.habra.event',
     'smth.habra.post'
];

foreach($events as $event) {
     $eventer->register($event);
}

echo "Adding some listeners\n";

$eventer->addListener(new BinaryMatcher('foo.habra.event'), function(Event $event) {
     echo sprintf("This will be called on %s event only\n", $event);
});

$eventer->addListener(new RegexMatcher('.+\.habra\..+'), function(Event $event) {
     echo sprintf("Wow, calling habra events! (%s)\n", $event);
});

$eventer->addOnceListener(new RegexMatcher('foo\..+\.event'), function(Event $event) {
     echo sprintf("This event is one of [foo.bar.event, foo.habra.event] -> %s. ", $event),
             "Also this is thrown only once!\n";
});

echo "Trigger all events once using binary matcher\n";
foreach($events as $event) {
     $eventer->trigger($event, ['some', 'data', 'provided', 'to', 'each', 'listener']);
}

echo "Trigger all events that matches against an RegexMatcher\n";
$eventer->triggerUsingMatcher(
             new RegexMatcher('foo\..+\.event'),
             ['some', 'data', 'provided', 'to', 'each', 'listener']
);

// too much words??? check SimplifiedApiTrait...