PHP code example of jimphle / messaging

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

    

jimphle / messaging example snippets


use Jimphle\Example\MessageHandler\SayHelloHandler;

use Jimphle\Messaging\MessageHandler\HandleMessage;
use Jimphle\Messaging\MessageHandlerProvider;
use Jimphle\Messaging\Command;

$messageHandlerDefinitions = array(
    'say-hello' => new SayHelloHandler(),
);
$messagingContext = new HandleMessage(
    new MessageHandlerProvider(
        new ArrayObject(
            $messageHandlerDefinitions
        )
    )
);

$response = $messagingContext->handle(
    Command::generate(
        'say-hello',
        array(
            'name' => 'World'
        )
    )
);
echo $response->answer;

# => Hello World!

use Jimphle\Example\MessageHandler\BeObsceneHandler;
use Jimphle\Example\MessageHandler\SayHelloHandler;

use Jimphle\Messaging\MessageHandler\HandleMessagesToProcessDirectly;
use Jimphle\Messaging\MessageHandler\HandleMessage;
use Jimphle\Messaging\MessageHandlerProvider;
use Jimphle\Messaging\Command;

$messageHandlerDefinitions = array(
    'say-hello' => new SayHelloHandler(),
    'said-hello' => array(
        new BeObsceneHandler()
    )
);
$messagingContext = new HandleMessagesToProcessDirectly(
    new HandleMessage(
        new MessageHandlerProvider(
            new ArrayObject(
                $messageHandlerDefinitions
            )
        )
    )
);

$response = $messagingContext->handle(
    Command::generate(
        'say-hello',
        array(
            'name' => 'World'
        )
    )
);
echo $response->answer . "\n";

# => GTFO Joscha!
# => Hello World!

$messageFilterDefinitions = array(new SomeMessageFilter());
$messagingContext = new HandleMessagesToProcessDirectly(
    new ApplyFilter(
        $messageFilterDefinitions,
        new HandleMessage(
            new MessageHandlerProvider(
                new ArrayObject(
                    $messageHandlerDefinitions
                )
            )
        )
    )
);

$messagingContext = new HandleMessagesToProcessDirectly(
    new TransactionalMessageHandler(
        new PDO('some-dsn'),
        new ApplyFilter(
            $messageFilterDefinitions,
            new HandleMessage(
                new MessageHandlerProvider(
                    new ArrayObject(
                        $messageHandlerDefinitions
                    )
                )
            )
        )
    )
);

use Jimphle\Messaging\Plugin\Pdo\TransactionalAnnotation as withPdoTransaction;

/**
 * @withPdoTransaction
 */
class SayHelloHandler extends AbstractMessageHandler
{
    public function handle(Message $message)
    {
        return $this->response(
            array('answer' => sprintf("Hello %s!", $message->name))
        )
            ->addMessageToProcessDirectly(
                $this->event('said-hello', array('name' => 'Joscha'))
            );
    }
}