Giter VIP home page Giter VIP logo

slim-event-dispatcher's Introduction

Slim Event Dispatcher

Build Status

This library is an implementation of League\Event for Slim Framework. This works with the latest version of Slim (V3).

Installation

It's recommended that you use Composer to install Slim Event Dispatcher.

$ composer require aneek/slim-event-dispatcher "^1.0"

This will install Slim Event Dispatcher and all required dependencies. This package requires PHP 5.6 or newer. Though version 1.x is not recommended for PHP 7.0 and above. The next major version (v2.x) will support PHP 7.x.

Usage

This is not a Slim Middleware but a package which integrates with your Slim application by extending the SlimEventManager class and adding it to Slim's dependency container. Currently there are two ways of integration.

Using Class Constructor

Slim\Event\SlimEventManager class accepts an array argument in it's constructor and initializes all Listeners (more information on listeners).

The below file content serves as a application loader file for Slim Framework:

<?php

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Container;
use Slim\App;
use Slim\Event\SlimEventManager;

require 'vendor/autoload.php';

$settings = [
    'settings' => [
        'determineRouteBeforeAppMiddleware' => true,
        'displayErrorDetails' => true,
    ],
];

// Array of event Listeners
$events = [
    'event.one' => [
        // First element is the FQCN class. This element is mandatory.
        // Second element is the listener priority but this is not mandatory.
        [\FooListener::class, 100],
        [\BarListener::class]    
    ],
    'event.two' => [
        [\BazListener::class]
    ]
];

$container = new Container();
$container['event_manager'] = function ($c) use($events) {
    $emitter = new SlimEventManager($events);
    return $emitter;
};

$app = new App($container);

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $this->get('event_manager')->emit('event.one', 'parameter_one', 'parameter_two');
    return $response->write("Hello, " . $args['name']);
});

$app->run();

Using add Method with Callable

<?php

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Container;
use Slim\App;
use Slim\Event\SlimEventManager;

require 'vendor/autoload.php';

$settings = [
    'settings' => [
        'determineRouteBeforeAppMiddleware' => true,
        'displayErrorDetails' => true,
    ],
];


$container = new Container();
// Add Event manager to dependency.
$container['event_manager'] = function ($c) {
    $emitter = new SlimEventManager();
    return $emitter;
};

$app = new App($container);

$app->getContainer()->get('event_manager')->add('event.one', function ($event, $param_1, $param_2) {
    // Do processing of the event.
    echo $param_1; // Prints parameter_one
    echo $param_2; // Prints parameter_two
});


$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $this->get('event_manager')->emit('event.one', 'parameter_one', 'parameter_two');
    return $response->write("Hello, " . $args['name']);
});

$app->run();

Creating Events or Listeners

Events or Listeners can be created by following the below guides from League\Event.

Issues

Please open a new issue and I will be happy to look into it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.