2017 © Pedro Pelรกez
 

library dispatcher

Event based request and command dispatcher for laravel 5

image

chencha/dispatcher

Event based request and command dispatcher for laravel 5

  • Tuesday, September 8, 2015
  • by jchencha
  • Repository
  • 1 Watchers
  • 0 Stars
  • 29 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Event Based Request Dispatch

This module provides a way to deligate handling of requests to multiple listeners., (*1)

This has the effect of making your controllers thin and your code DRY and testable, (*2)

A sample installation utilizing the module can be found here, (*3)

Sample Application, (*4)

This module requires a laravel installation., (*5)

Installation

You can use composer to install, (*6)

composer require chencha/dispatcher

Basic Usage

An assumption is made that your application utilizes PSR-4 Autoloading, (*7)

Directory structure

.
โ””โ”€โ”€ Sample
    โ”œโ”€โ”€ Commands
    โ”‚ย ย  โ””โ”€โ”€ SaveUser.php
    โ”œโ”€โ”€ Handlers
    โ”‚ย ย  โ”œโ”€โ”€ CommandHandler.php
    โ”‚ย ย  โ””โ”€โ”€ RequestHandler.php
    โ”œโ”€โ”€ Models
    โ”‚ย ย  โ””โ”€โ”€ Transactions.php
    โ””โ”€โ”€ Requests
        โ””โ”€โ”€ RetreiveUser.php

The handlers

The handlers are classes that register all classes that will respond to a request or a command. They must extend, (*8)

Chencha\Dispatcher\EventSubscriber;

The handler class must then provide the location of the commands of the commands or requests it will handle to the parent constructor., (*9)

Eg, (*10)

function __construct()
{
    $path = "Sample.Commands";
    parent::__construct($path);
}

The class has three methods of which only one is required. This are:, (*11)

  • beforeListeners
  • duringListeners (Required)
  • afterListeners
  • queuedListeners (Called outsite the request cycle. See http://laravel.com/docs/4.2/queues)

each of this methods must return an array if defined., (*12)

Eg, (*13)

/**
 * @return array
 */
function duringListeners()
{
    return [
        Transactions::class
    ];
}

Subscribe the handlers

For the framework to be aware of your registered classes. You need to register your handlers., (*14)

This is bootstrap work and should be done in either app/start/global.php file or whereever you normally register listeners., (*15)

A sample declaration, (*16)

Event::subscribe(new \Sample\Handlers\CommandHandler());

Usage

In your controllers use the trait, (*17)

use \Chencha\Dispatcher\RequestDispatcher;

Now to run the request, (*18)

function getSaveuser()
{
    $command = new \Sample\Commands\SaveUser(rand(1, 5));
    $this->runRequest($command);
    return "Success";
}

In this way all classes registered in the command handler will be called., (*19)

Since objects are usually passed by reference. Changes are made directly to the command object., (*20)

This is useful in a request where a response is needed, (*21)

Eg, (*22)

function getUser()
{
    $request = new \Sample\Requests\RetreiveUser(rand(1, 5));
    $this->runRequest($request);
    return $request->response;
}

In this way you can populate say the response public property with all needed values for the response., (*23)

Gotchas

Nesting Level

If you register a lot of classes you are likely to run into this error, (*24)

PHP Error: Maximum function nesting level of '100' reached, aborting

This is because of how the laravel event dispatcher works., (*25)

To sort this problem simply add the line, (*26)

xdebug.max_nesting_level = 200

to /etc/php5/fpm/conf.d/20-xdebug.ini, (*27)

The higher the max nesting level the more classes you can register., (*28)

Serialization of closure

Your objects should be as simple as possible, preferably native php types., (*29)

At all costs avoid closures as they can not be serialized., (*30)

The Versions

08/09 2015

dev-develop

dev-develop

Event based request and command dispatcher for laravel 5

  Sources   Download

MIT

The Requires

 

laravel events commander listeners

08/09 2015

dev-master

9999999-dev

Event based request and command dispatcher for laravel 5

  Sources   Download

MIT

The Requires

 

laravel events commander listeners

08/09 2015

2.0.1

2.0.1.0

Event based request and command dispatcher for laravel 5

  Sources   Download

MIT

The Requires

 

laravel events commander listeners

10/02 2015

1

1.0.0.0

Event based request and command dispatcher for laravel 4

  Sources   Download

MIT

The Requires

 

laravel events commander listeners

23/01 2015

0.1

0.1.0.0

Event based request and command dispatcher for laravel 4

  Sources   Download

MIT

The Requires

 

laravel events commander listeners