PHP code example of tobento / service-event

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

    

tobento / service-event example snippets


use Tobento\Service\Event\Dispatcher;
use Tobento\Service\Event\Listeners;

class FooEvent {}

$listeners = new Listeners();

$listeners->add(function(FooEvent $event) {
    // do something
});

$dispatcher = new Dispatcher($listeners);

$event = $dispatcher->dispatch(new FooEvent());

use Tobento\Service\Event\Events;

class FooEvent {}

$events = new Events();

$events->listen(function(FooEvent $event) {
    // do something
});

$event = $events->dispatch(new FooEvent());

use Tobento\Service\Event\Listeners;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\CallableFactoryInterface;
use Tobento\Service\Event\ListenerEventsResolverInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
    
$listeners = new Listeners(
    callableFactory: null, // null|CallableFactoryInterface
    listenerEventsResolver: null, // null|ListenerEventsResolverInterface
);

var_dump($listeners instanceof ListenersInterface);
// bool(true)

var_dump($listeners instanceof ListenerProviderInterface);
// bool(true)

use Tobento\Service\Event\Listeners;
use Tobento\Service\Event\AutowiringCallableFactory;
use Tobento\Service\Container\Container;

class FooListener
{
    public function foo(FooEvent $event): void
    {
        // do something
    }
}

// any PSR-11 container
$container = new Container();
    
$listeners = new Listeners(
    callableFactory: new AutowiringCallableFactory($container),
);

$listeners->add(FooListener::class);

class FooListener
{
    public function __invoke(FooEvent $event): void
    {
        // do something
    }
}

class FooBuildInListener
{
    public function __construct(protected int $number) {}
    
    public function __invoke(FooEvent $event): void
    {
        // do something
    }
}

$listeners->add(new FooListener());

// using autowiring:
$listeners->add(FooListener::class);

// using autowiring with build-in parameters:
$listeners->add([FooBuildInListener::class, ['number' => 5]]);

class FooBarListener
{
    public function foo(FooEvent $event): void
    {
        // do something
    }
    
    public function bar(BarEvent $event): void
    {
        // do something
    }
    
    public function another(AnotherEvent $event): void
    {
        // do something
    }    
    
    public function fooAndBar(FooEvent|BarEvent $event): void
    {
        // do something
    }    
}

// only listen to foo and bar event: 
$listeners->add(new FooBarListener())
          ->event(FooEvent::class, BarEvent::class);
          
// using autowiring:
$listeners->add(FooBarListener::class)
          ->event(FooEvent::class, BarEvent::class);

$listeners->add(function(FooEvent $event) {
    // do something
});

$listeners->add(function(FooEvent $event) {
    // do something
})->priority(1500);

// gets called first as higher priority.
$listeners->add(function(FooEvent $event) {
    // do something
})->priority(2000);

use Tobento\Service\Event\ListenerInterface;
use Tobento\Service\Event\CallableFactoryInterface;

interface ListenerInterface
{
    /**
     * Returns the listener.
     *    
     * @return mixed
     */
    public function getListener(): mixed;

    /**
     * Returns the listener events.
     *    
     * @return array<string, array<mixed>>
     */
    public function getListenerEvents(): array;
    
    /**
     * Returns the listeners for the specified event.
     *
     * @param object $event
     * @param CallableFactoryInterface $callableFactory
     * @return iterable<callable>
     *   An iterable (array, iterator, or generator) of callables. Each
     *   callable MUST be type-compatible with $event.
     */
    public function getForEvent(object $event, CallableFactoryInterface $callableFactory): iterable;
    
    /**
     * Returns the priority.
     *    
     * @return int
     */
    public function getPriority(): int;   
}

$listeners->addListener(new AnyCustomListener());

use Tobento\Service\Event\ListenerInterface;

foreach($listeners->all() as $listener) {
    var_dump($listener instanceof ListenerInterface);
    // bool(true)
}

use Tobento\Service\Event\Dispatcher;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use Psr\Container\ContainerInterface;
use Tobento\Service\Event\Listeners;

$listeners = new Listeners();

$dispatcher = new Dispatcher(
    listenerProvider: $listeners, // ListenerProviderInterface
    container: null, // null|ContainerInterface
);

var_dump($dispatcher instanceof EventDispatcherInterface);
// bool(true)

use Tobento\Service\Event\Dispatcher;
use Tobento\Service\Event\Listeners;
use Tobento\Service\Container\Container;

class FooEvent {}
class Bar {}

$listeners = new Listeners();

// adding more parameters after the $event.
$listeners->add(function(FooEvent $event, Bar $bar) {
    // do something
});

// Any PSR-11 container
$container = new Container();

$dispatcher = new Dispatcher(
    listenerProvider: $listeners,
    container: $container,
);

$dispatcher->dispatch(new FooEvent());

$dispatcher->dispatch(new AnyEvent());

use Tobento\Service\Event\Events;
use Tobento\Service\Event\EventsInterface;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\DispatcherFactoryInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

$events = new Events(
    listeners: null, // null|ListenersInterface
    dispatcherFactory: null, // null|DispatcherFactoryInterface
);

var_dump($events instanceof EventsInterface);
// bool(true)

var_dump($events instanceof EventDispatcherInterface);
// bool(true)

use Tobento\Service\Event\EventsFactory;
use Tobento\Service\Event\EventsFactoryInterface;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\DispatcherFactoryInterface;
use Tobento\Service\Event\EventsInterface;

$eventsFactory = new EventsFactory();

var_dump($eventsFactory instanceof EventsFactoryInterface);
// bool(true)

$events = $eventsFactory->createEvents(
    listeners: null, // null|ListenersInterface
    dispatcherFactory: null, // null|DispatcherFactoryInterface
);

var_dump($events instanceof EventsInterface);
// bool(true)

use Tobento\Service\Event\AutowiringEventsFactory;
use Tobento\Service\Event\EventsFactoryInterface;
use Tobento\Service\Event\EventsInterface;
use Tobento\Service\Container\Container;

// Any PSR-11 container
$container = new Container();

$eventsFactory = new AutowiringEventsFactory(
    container: $container,
    withAutowiringDispatching: true,
);

var_dump($eventsFactory instanceof EventsFactoryInterface);
// bool(true)

$events = $eventsFactory->createEvents();

var_dump($events instanceof EventsInterface);
// bool(true)

$events->listen(FooListener::class);

$events->listen(AnyListener::class)
       ->event(FooEvent::class)
       ->priority(2000);

use Tobento\Service\Event\ListenersInterface;

$listeners = $events->listeners();

var_dump($listeners instanceof ListenersInterface);
// bool(true)

$listeners->add(AnyListener::class);

$events->dispatch(new AnyEvent());

use Tobento\Service\Event\EventsAware;
use Tobento\Service\Event\HasEvents;
use Tobento\Service\Event\EventsInterface;
use Tobento\Service\Event\Events;

class AnyService implements EventsAware
{
    use HasEvents;
    
    public function __construct(EventsInterface $events)
    {
        $this->events = $events;
    }
}

$service = new AnyService(new Events());

var_dump($service->events() instanceof EventsInterface);
// bool(true)