CodefyPHP FrameworkCodefyPHP Framework
  • Home
  • Community
    • Forum
    • Github
    • YouTube
  • News
  • Home
  • Community
    • Forum
    • Github
    • YouTube
  • News
home/Knowledge Base/Qubus/Event Dispatcher
Popular Search:installation, codex, mail

Event Dispatcher

119 views 0

Written by Joshua
August 14, 2024

Many applications develop a system that allows for code behavior change without modifying the core. Many of these systems will implement the observer pattern or the mediator pattern.

CodefyPHP’s event dispatcher implements the observer pattern. Create custom events and listeners that allows components in your project to react if something occurs.

The event dispatcher contains three elements: the event, the listener, and the dispatcher. The dispatcher class raises events that the listeners throughout the system will listen for or subscribe to.

Please note that the event dispatcher is different from the Event Bus for domain events.

The following example should help explain in more detail on how to use the event dispatcher. This example shows how to use it to send an email after a user registers to a site.

First we need an event class:

<?php

use Qubus\EventDispatcher\GenericEvent;

final class MessageSent extends GenericEvent
{
    public const EVENT_NAME = self::class;

    public function getName(): string
    {
        return self::EVENT_NAME;
    }
    public function message(): string
    {
        return 'Congrats! Your account was created successfully.';
    }
}

Next, you could create a listener or subscriber. For this example, I am going to use a subscriber. Subscribers allow you to group together events rather than just one event via a listener.

<?php

use Qubus\EventDispatcher\Event;
use Qubus\EventDispatcher\EventSubscriber;

final class MessageSentSubscriber implements EventSubscriber
{

    public static function getSubscribedEvents(): array
    {
        return [
            MessageSent::class => 'onMessageSent',
        ];
    }

    public function onMessageSent(Event $event)
    {
        echo $event->message();
    }
}

The MessageSentSubscriber class implements the EventSubscriber interface which needs to implement the getSubscribedEvents method. The getSubscribedEvents array should return an array of events you want it to be subscribed to. For the array, the key is the event name (MessageSent::class) and the value is the method that should be called when the event is triggered, also know as the listener method.

Now that we have our classes in place, create a file called subscriber_event.php and add the following:

<?php

require('vendor/autoload.php');

use Qubus\EventDispatcher\Dispatcher;
use Qubus\EventDispatcher\Event;
use Qubus\EventDispatcher\EventSubscriber;
use Qubus\EventDispatcher\GenericEvent;
use Qubus\Exception\Data\TypeException;

// init the event dispatcher
$dispatcher = new Dispatcher();

// register the subscriber
$subscriber = new MessageSentSubscriber();
try {
    $dispatcher->addSubscriber($subscriber);
} catch (TypeException $e) {
    return $e->getMessage();
}

// dispatch
$dispatcher->dispatch(MessageSent::class, new MessageSent());

When you type out the following command php subscriber_event.php via a terminal and hit return, you should see your message: Congrats! Your account was created successfully.

Working with an event dispatcher might not seem as simple as a hook/plugin system like you find in a CMS like WordPress, but once you get the hang of it, you will see that it is pretty powerful.

Forum

If you have any questions or issues, please feel free to post to the Documentation Forum.

SLA Support

If you are needing more hands on support, needing consultation, or help with setup, support me on Github at $60 or more. Once you've sponsored me, you will receive an email on the best way to contact me to start your support.

Edit on Github

Last Updated on August 14, 2024 by Joshua

Related Articles
  • Mailer
  • Logger
  • Filesystem
  • Migrations
  • Database
  • Routing

Didn't find your answer? Check out the Forum

Leave A Comment Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Routing

Database  

  • Copyright 2025 CodefyPHP.com. All Rights Reserved

Popular Search:installation, codex, mail