Event Listeners

All messages are dispatched using a routing key that is three strings delimited by a period and generally formatted as package.alias.method where package is generally the alias of the package dispatching the message, alias is anything the developer chose, and method is the actual method name that will be called within the event listeners.

You will need to consult each package's documentation to ascertain the dispatched events it supports. For example, the users package dispatches messages for users.profiles.preCreate and users.profiles.postCreate. Using this as an example, you may create a new listener with the create listener CLI command, such as:

apex create listener my-package my-listener --routing-key users.profiles

This will generate a new file at /src/MyPackage/Listeners/MyListener.php. For an example of how to utilize the postCreate method:

<?php
declare(strict_types = 1);

namespace App\MyPackage\Listeners;

use Apex\App\Interfaces\ListenerInterface;
use Apex\Cluster\Interfaces\{MessageRequestInterface, FeHandlerInterface};
use App\Users\User;

/**
 * Listener - MyListener
 */
class MyListener implements ListenerInterface
{

    /**
     * Routing key for which the listener accepts messages for.
     */
    public static string $routing_key = 'users.profiles';


    /**
     * post create
     */
    public function postCreate(MessageRequestInterface $msg, FeHandlerInterface $handler)
    {

        // Get params, and load user
        $uuid = $msg->getParams();
        if (!$user = User::loadUuid($uuid)) { 
            return;
        }

        // Do something with $user
    }

}

Every time a user is created, a message will be dispatched to the users.profiles.postCreate routing key, which will invoke the above class then execute the postCreate method. This is just an example, and please consult with each package's documentation to see what messages they dispatch and parameters are passed.

You may also view additional information on the Github, such as: