1. Go to this page and download the library: Download tobento/app-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 / app-event example snippets
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Event\Boot\Event::class);
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\EventsFactoryInterface;
use Tobento\Service\Event\EventsInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Event\Boot\Event::class);
$app->booting();
$listeners = $app->get(ListenersInterface::class);
// is declared as prototype, meaning returning always a new instance!
$eventsFactory = $app->get(EventsFactoryInterface::class);
$events = $app->get(EventsInterface::class);
$eventDispatcher = $app->get(EventDispatcherInterface::class);
// var_dump($events === $eventDispatcher);
// bool(true)
// Run the app
$app->run();
namespace App\Event;
use App\Entity\User;
final class UserRegistered
{
public function __construct(
public readonly User $user
) {}
}
namespace App\Listener;
class SendWelcomeMail
{
public function __invoke(UserRegistered $event): void
{
// send welcome mail.
}
}
use Tobento\App\Event\ListenerRegistry;
use App\Event;
use App\Listener;
return [
/*
|--------------------------------------------------------------------------
| Default Event Listeners
|--------------------------------------------------------------------------
|
| Define the event listeners for the default events.
|
| As the events class uses reflection to scan listeners for its events named $event,
| there is no need to define its event(s) for a listener.
| But you might do so if you have multiple events in your listener and
| want only to listen for the specific events or just because of better overview.
|
*/
'listeners' => [
// Specify events to listeners:
Event\UserRegistered::class => [
Listener\SendWelcomeMail::class,
// with build-in parameters:
[Listener::class, ['number' => 5]],
// with specific priority:
new ListenerRegistry(
listener: Listener::class,
priority: 1,
),
],
// Specify listeners without event:
'auto' => [
Listener\SendWelcomeMail::class,
// with build-in parameters:
[Listener::class, ['number' => 5]],
// with specific priority:
new ListenerRegistry(
listener: Listener::class,
priority: 1,
),
],
],
// ...
];
use Tobento\App\AppFactory;
use Tobento\Service\Event\EventsInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Event\Boot\Event::class);
$app->booting();
$events = $app->get(EventsInterface::class);
// Add listeners
$events->listen(FooListener::class);
$events->listen(AnyListener::class)
->event(FooEvent::class)
->priority(2000);
// Run the app
$app->run();
namespace App\Service;
use Psr\EventDispatcher\EventDispatcherInterface;
use App\Event\UserRegistered;
use App\Entity\User;
final class UserService
{
public function __construct(
private readonly EventDispatcherInterface $dispatcher
) {}
public function register(User $user): void
{
// ...
$this->dispatcher->dispatch(new UserRegistered($user));
}
}
namespace App\Service;
use Tobento\Service\Event\EventsInterface;
use App\Event\UserRegistered;
use App\Entity\User;
final class UserService
{
public function __construct(
private readonly EventsInterface $dispatcher
) {}
public function register(User $user): void
{
// ...
$this->dispatcher->dispatch(new UserRegistered($user));
}
}
use Tobento\Service\Event\Events;
final class ShopEvents extends Events
{
//
}
use Tobento\App\Event\ListenerRegistry;
return [
// ...
/*
|--------------------------------------------------------------------------
| Specific Events Listeners
|--------------------------------------------------------------------------
|
| Define the event listeners for the specific events.
|
| As the events class uses reflection to scan listeners for its events named $event,
| there is no need to define its event(s) for a listener.
| But you might do so if you have multiple events in your listener and
| want only to listen for the specific events or just because of better overview.
|
*/
'events' => [
ShopEvents::class => [
// Specify events to listeners:
SomeEvent::class => [
Listener::class,
// with build-in parameters:
[Listener::class, ['number' => 5]],
],
// Specify listeners without event:
'auto' => [
Listener::class,
// with build-in parameters:
[Listener::class, ['number' => 5]],
// to define specific priority:
new ListenerRegistry(
listener: Listener::class,
priority: 1,
),
],
],
],
// ...
];
use Tobento\App\AppFactory;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Event\Boot\Event::class);
$app->booting();
$events = $app->get(ShopEvents::class);
// Add listeners
$events->listen(FooListener::class);
$events->listen(AnyListener::class)
->event(FooEvent::class)
->priority(2000);
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\App\Event\ConfigEventsRegistry;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Event\Boot\Event::class);
$app->booting();
$app->on(ShopEvents::class, function(ShopEvents $shopEvents) {
// Add listeners
$shopEvents->listen(FooListener::class);
// Or add listeners from config:
(new ConfigEventsRegistry(priority: 1000))->addListenersFromArray(
events: $shopEvents,
// using same definition as config listeners.
eventListeners: [
SomeEvent::class => [
Listener::class,
// with build-in parameters:
[Listener::class, ['number' => 5]],
// ...
],
],
);
});
// Run the app
$app->run();
namespace App\Service;
use Psr\EventDispatcher\EventDispatcherInterface;
final class ShopService
{
public function __construct(
private readonly EventDispatcherInterface $dispatcher
) {}
}
// or:
final class AnotherShopService
{
public function __construct(
private readonly ShopEvents $dispatcher
) {}
}
// using a closure:
$app->set(ShopService::class, function() {
return new ShopService(
dispatcher: $app->get(ShopEvents::class),
);
});
// using the construct method:
$app->set(ShopService::class)->construct($app->get(ShopEvents::class));
// using the on method:
$app->on(ShopService::class, ['dispatcher' => ShopEvents::class]);
app/config/event.php
app/config/event.php
app/config/event.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.