2017 © Pedro Peláez
 

library messaging

Jimdo PHP library extraction of messaging component

image

jimphle/messaging

Jimdo PHP library extraction of messaging component

  • Thursday, August 24, 2017
  • by schnipseljagd
  • Repository
  • 73 Watchers
  • 5 Stars
  • 65 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Jimphle-messaging

Jimdo PHP library extraction of messaging component., (*1)

The Messaging component is base on message-handler which implement the MessageHandler Interface., (*2)

Handling commands and events

Let's get started with sending a command say-hello:, (*3)

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!

Ok what happened here? We passed the Command say-hello and it's payload to the messagingContext. The messagingContext did a lookup for A MessageHandler registered to say-hello., (*4)

Something more interesting. If you had a look at the SayHelloHandler you would have seen that the handler itself generated a new Event which is returned with the MessageResponse "to-be-processed-directly" by the calling context. To make our messagingContext to be able to handle this kind of messages we have to add another kind of handler., (*5)

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!

As you can see Our said-hello Event is handled by the messagingContext now. Which means the messagingContext handles the SayHelloHandler iterates over the returned "to-be-processed-directly" events and tries to handle them as well. How you may have noticed the order of the printed messages is reversed to the order the printing handlers are called. This happens because the response of the first called message-handler is the one which is returned here., (*6)

Message-filter and message-handler annotations

If you add the ApplyFilter MessageHandler to the messagingContext you add the possibility to apply filter to a message before passing it to the next message-handler., (*7)

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

However there are two predefined filter we can use here: * The Validation filter, based on Symfony-Validation-Component * The Authorization filter explained here another day, (*8)

Handling messages in a PDO transaction

To handle the messages in a PDO transaction we can add the TransactionalMessageHandler to the messagingContext., (*9)

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

To execute a message handler in a pdo transaction you just have to add an annotation., (*10)

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'))
            );
    }
}

The Versions

24/08 2017

dev-master

9999999-dev https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

24/08 2017

v1.2.1

1.2.1.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

24/08 2017

dev-fix-module-update

dev-fix-module-update https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

23/08 2017

v1.2.0

1.2.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

21/06 2016

v1.1.0

1.1.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

21/06 2016

v1.0.0

1.0.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

17/08 2015

v0.5.0

0.5.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

12/08 2015

v0.4.8

0.4.8.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

22/06 2015

v0.4.7

0.4.7.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

22/06 2015

v0.4.6

0.4.6.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

04/09 2014

v0.4.5

0.4.5.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

25/08 2014

v0.4.4

0.4.4.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

29/07 2014

v0.4.3

0.4.3.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

11/06 2014

v0.4.2

0.4.2.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

11/06 2014

v0.4.1

0.4.1.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

11/06 2014

v0.4.0

0.4.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

10/06 2014

v0.3.0

0.3.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

23/04 2014

v0.2.0

0.2.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler

15/04 2014

v0.1.0

0.1.0.0 https://github.com/Jimdo/jimphle-messaging

Jimdo PHP library extraction of messaging component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joscha

messaging message-handler event-handler command-handler