PHP code example of gigi / event-request-response

1. Go to this page and download the library: Download gigi/event-request-response 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/ */

    

gigi / event-request-response example snippets


use gigi\events\classes\Messenger;

$handler = Messenger::getInstance();

$handler->subscribe('message.test', function ($message) {
    // do something with message
    // stops further message processing
    $message->setIsHandled(true);

    // optionally return mixed response
    // Will be accessible in reply object (Message::getData())
    return 'Data to return';
});

use gigi\events\classes\Message;

$message = new Message('message.test', 'Mixed data to send');

// one-way notifier
$handler->request($message);

// request-response notifier
// Returns array of Messages
$result = $handler->requestReply($message);

// request-response notifier
// Returns only first reply (Message object)
// Use if Your don't care of who can return an answer
// for example if you want to get list of all users ('users.get.all')
// or you know for sure that only one subscriber listening

$result = $handler->requestFirstReply($message);
examples/singleton.php
examples/di.php

return [
    ...
    'components' => [
        ...
        'subscriber' => 'gigi\events\classes\Subscriber',
        'publisher'  => 'gigi\events\classes\Publisher',
        ...