Developed with love by KnpLabs Hire us for your project!
31

PubSubRouterBundle

by GeniusesOfSymfony

:vertical_traffic_light: PubSub Router to easily plug handler on channel

Gos PubSub Router Bundle

Join the chat at https://gitter.im/GeniusesOfSymfony/PubSubRouterBundle Build Status Latest Stable Version Total Downloads License

About

Gos PubSub Router is a Symfony2 Bundle, his goal is to plug any logic behind pubsub channel. When you use PubSub pattern you will make face to a problem, rely channels with business logic. PubSub router is here to make the junction between channel and business logic.

Feature

  • [x] Route definition
  • [x] Route matching
  • [x] Route generator

Installation

Register the bundle in app/appKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            ...
            new \Gos\Bundle\PubSubRouterBundle\GosPubSubRouterBundle()
        );

        ...
    }

Bundle configuration

#Gos PubSub Router
gos_pubsub_router:
    routers:
        websocket: #available from container through gos_pubsub_router.websocket
            context:
                tokenSeparator: '/'
            resources:
                - @GosNotificationBundle/Resources/config/pubsub/websocket/notification.yml
        redis: #available from container through gos_pubsub_router.redis
            context:
                tokenSeparator: ':' #redis channel are like : notification:user:user2 so the token separator is :
            resources:
                - @GosNotificationBundle/Resources/config/pubsub/redis/notification.yml

NOTE : Each router are insulated. If you have several routers in the same class you will need to inject each router that you need.

Usage

Routing definition

Example with websocket pubsub

user_notification:
    channel: notification/user/{role}/{application}/{user_ref}
    handler:
        callback: ['Acme\Chat\MessageHandler', 'addPushers']
        args: [ 'redis', 'websocket' ]
    requirements:
        role:
            pattern: "editor|admin|client"
        application:
            pattern: "[a-z]+"
        user_ref:
            pattern: "\d+"
            wildcard: true

Example with redis pubsub

user_app_notification:
    channel: notification:user:{role}:{application}:{user_ref}
    handler:
        callback: ['Acme\Chat\MessageHandler', 'addPushers']
        args: [ 'redis', 'websocket' ]
    requirements:
        role:
            pattern: "editor|admin|client"
        application:
            pattern: "[a-z-]+-app"
        user_ref:
            pattern: "\d+"
            wildcard: true

NOTE : Wildcard match with *and all keyword

NOTE 2 : Callback is not type hinted, so you can pass a single string (like a service name) that only depend of your implementation when you retrieve the route ! You are free to choose what you to do !

Use router

Let's generate a route !

$router = $this->container->get('gos_pubsub_router.websocket');
$channel = $router->generate('user_notification', ['role' => 'admin', 'application' => 'blog-app', 'user_ref' => '123']);

echo $channel // notification/user/admin/blog/123

Match your first route !

use Gos\Bundle\PubSubRouterBundle\Request\PubSubRequest;

$channel = 'notification/user/admin/billing-app/639409'; // 'notification/user/admin/billing-app/*' work :)

list($routeName, $route, $attributes) = $router->match($channel);

$request = new PubSubRequest($routeName, $route, $attributes); //Create a request object if you want transport the request data as dependency

//$request->getAttributes->get('user_ref'); it's a parameterBag

// $router->match($channel, ':'); if you want override tokenSeparator from context, or if you dont have context.

// $routeName -> 'user_app_notification
// $route -> instance of Gos\Bundle\PubSubRouterBundle\Router\RouteInterface
// $attributes -> [ 'role' => 'admin', 'application' => 'billing-app', 'user_ref' => '639409' ]

What about miss match humm ?

use Gos\Bundle\PubSubRouterBundle\Exception\ResourceNotFoundException;

$channel = 'notification/user/admin/billing-app/azerty'; // will miss match

try {
    list($routeName, $route, $attributes) = $router->match($channel);
} catch (ResourceNotFoundException $e) {
    //handle exception
}
  • If you only need to generate route, type hint against Gos\Bundle\PubSubRouterBundle\Generator\GeneratorInterface
  • If you only need to match route, type hint against Gos\Bundle\PubSubRouterBundle\Matcher\MatcherInterface
  • If you need both, type hint against Gos\Bundle\PubSubRouterBundle\Router\RouterInterface

Router CLI

php app/console gos:prouter:debug -r websocket dump all registered routes for websocket router

License

MIT, See LICENSE file in the root of project.

The MIT License (MIT)

Copyright (c) 2015 Johann Saunier <johann_27@hotmail.fr>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
gos_pubsub_router:
matcher_class: Gos\Bundle\PubSubRouterBundle\Matcher\Matcher
generator_class: Gos\Bundle\PubSubRouterBundle\Generator\Generator
route_loader_class: Gos\Bundle\PubSubRouterBundle\Loader\RouteLoader
router_class: Gos\Bundle\PubSubRouterBundle\Router\Router
routers:

# Prototype
name:
context:
tokenSeparator: ~
resources: []
  • Update services.yml
    By web-flow, 1 year ago
  • Merge pull request #16 from darthf1/patch-1
    By web-flow, 1 year ago
  • Merge pull request #17 from Kronhyx/patch-1
    By web-flow, 1 year ago
  • symfony 4 service visibility
    By web-flow, 1 year ago
  • Update composer.json
    By web-flow, 1 year ago
  • Merge pull request #14 from Kolbasyatin/patch-1
    By web-flow, 2 years ago
  • Update DebugRouterCommand.php
    By web-flow, 2 years ago
  • Update DebugRouterCommand.php
    By web-flow, 2 years ago
  • Update DebugRouterCommand.php
    By web-flow, 2 years ago
  • Merge pull request #13 from DeRain/master
    By web-flow, 2 years ago
  • Made a "handler" param as optional
    By , 2 years ago
  • Merge pull request #11 from fortis/fortis-patch-1
    By web-flow, 2 years ago
  • Fix typo
    By web-flow, 2 years ago
  • Merge pull request #10 from aleksa2808/matcher-pattern-fix
    By ProPheT777, 3 years ago
  • fix for pattern ignoring
    By , 3 years ago
  • Merge pull request #8 from GeniusesOfSymfony/sf3
    By ProPheT777, 3 years ago
  • Merge pull request #7 from matthieuy/sf3
    By ProPheT777, 3 years ago
  • Fix yaml parser for SF3.0
    By matthieuy, 3 years ago
  • update composer
    By ProPheT777, 3 years ago
  • update composer
    By ProPheT777, 3 years ago
  • Merge pull request #6 from GeniusesOfSymfony/sf-23
    By ProPheT777, 3 years ago
  • Compatible with sf23
    By ProPheT777, 3 years ago
  • Remove debug garbage
    By ProPheT777, 3 years ago
  • Be compatible with php5.4
    By ProPheT777, 3 years ago
  • more badges
    By ProPheT777, 3 years ago
  • fix matcher
    By ProPheT777, 4 years ago
  • Fix matcher
    By ProPheT777, 4 years ago
  • fix generation/match with empty token
    By ProPheT777, 4 years ago
  • fix tokenizer & matcher
    By ProPheT777, 4 years ago
  • Fix matcher
    By ProPheT777, 4 years ago