PHP code example of xblabs / ripple

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

    

xblabs / ripple example snippets




$dispatcher = new XB\Ripple\Dispatcher();

use XB\Ripple\Event;
use XB\Ripple\Dispatcher;

$dispatcher = new Dispatcher();

// Dispatch by name; returns an array of listener responses, or null if nothing handled it.
$responses = $dispatcher->dispatch( 'user.login' );

// Dispatch with a target object and params. $user is the target; ['id' => 123] the params.
$dispatcher->dispatch( 'user.login', $user, [ 'id' => 123 ] );

// Or build the Event yourself.
$dispatcher->dispatch( new Event( 'user.login', $user, [ 'id' => 123 ] ) );

// Dispatch and stop at the first listener that returns a truthy value.
$result = $dispatcher->dispatchUntil( 'user.login' );

// Dispatch and return only the first response.
$result = $dispatcher->dispatchGetFirst( 'user.login' );

// Closure
$dispatcher->addListener( 'user.login', static function ( Event $e ) {
    // $e->getType(), $e->getTarget(), $e->getParams()
} );

// Class method
$dispatcher->addListener( 'user.login', [ $service, 'onLogin' ] );

$dispatcher->addListener( 'math.add', static function ( $a, $b ) {
    return $a + $b;
} );
$dispatcher->dispatch( 'math.add', null, [ 2, 3 ] ); // => [5]

$dispatcher->addListener( 'event.name', $low, -10 );
$dispatcher->addListener( 'event.name', $high, 100 ); // fires first

$dispatcher->addListener( 'event.name', static function ( Event $e ) {
    $e->stopPropagation(); // remaining listeners are skipped
} );

// Events created with cancelable=false ignore stopPropagation().
$event = new Event( 'event.name', null, null, cancelable: false );
$dispatcher->dispatch( $event );

$dispatcher->once( 'app.boot', static fn( Event $e ) => bootstrap() );
$dispatcher->onceWildcard( 'cache.*', static fn( Event $e ) => warmCache( $e->getType() ) );

$dispatcher->addWildcardListener( 'user.*', static function ( Event $e ) {
    // receives user.login, user.logout, user.profile.update, ...
    match ( $e->getType() ) {
        'user.login'  => /* ... */ null,
        'user.logout' => /* ... */ null,
        default       => null,
    };
} );

use XB\Ripple\Event;
use XB\Ripple\EventSubscriberInterface;

class UserSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            'user.login'  => 'onLogin',                    // method name
            'user.logout' => [ 'onLogout', 100 ],          // method + priority
            'user.*'      => [ [ 'audit', 200 ], [ 'log', -10 ] ], // multiple handlers
        ];
    }

    public function onLogin( Event $e ): void { /* ... */ }
    public function onLogout( Event $e ): void { /* ... */ }
    public function audit( Event $e ): void { /* ... */ }
    public function log( Event $e ): void { /* ... */ }
}

$dispatcher->addSubscriber( new UserSubscriber() );
// $dispatcher->removeSubscriber( $subscriber ); // detaches everything it registered

$dispatcher->hasListener( 'user.login' );
$dispatcher->getListenersForEvent( 'user.login' );
$dispatcher->getAllListeners();
$dispatcher->getWildcardListeners();

$dispatcher->removeListener( 'user.login', $listener );
$dispatcher->removeWildcardListener( 'user.*', $listener );
$dispatcher->removeListenersForEvent( 'user.login' );
$dispatcher->removeAllListeners();

$dispatcher->setEventClass( MyEvent::class ); // must extend XB\Ripple\Event

use XB\Ripple\DispatcherStatic;

DispatcherStatic::addListener( 'user.login', $listener );
DispatcherStatic::dispatch( 'user.login' );

DispatcherStatic::setDispatcher( $myDispatcher ); // inject a specific instance
DispatcherStatic::reset();                        // drop the shared instance entirely

use XB\Ripple\Dispatcher;
use XB\Ripple\Psr14\Psr14Dispatcher;

$ripple = new Dispatcher();
$ripple->addListener( 'user.login', $listener );

$psr = new Psr14Dispatcher( $ripple );
$event = $psr->dispatch( new XB\Ripple\Event( 'user.login' ) ); // returns the event