gos / pubsub-router-bundle
Symfony PubSub Router Bundle
Fund package maintenance!
mbabker
Installs: 1 985 890
Dependents: 1
Suggesters: 0
Security: 0
Stars: 42
Watchers: 7
Forks: 6
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.2 || ^8.0
- symfony/config: ^4.4.42 || ^5.4 || ^6.0
- symfony/console: ^4.4.42 || ^5.4 || ^6.0
- symfony/dependency-injection: ^4.4.42 || ^5.4 || ^6.0
- symfony/deprecation-contracts: ^2.1 || ^3.0
- symfony/http-foundation: ^4.4.42 || ^5.4 || ^6.0
- symfony/http-kernel: ^4.4.42 || ^5.4 || ^6.0
- symfony/polyfill-php80: ^1.22
- symfony/yaml: ^4.4.42 || ^5.4 || ^6.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.1.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: 1.8.5
- phpstan/phpstan-phpunit: 1.1.1
- phpstan/phpstan-symfony: 1.2.13
- phpunit/phpunit: ^8.5 || ^9.3
- psr/container: ^1.0 || ^2.0
- symfony/phpunit-bridge: ^5.4 || ^6.0
- 3.x-dev
- 2.x-dev
- v2.8.0
- v2.7.1
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v2.0.0-beta
- 1.x-dev
- v1.7.0
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-11-20 06:05:43 UTC
README
About
GosPubSubRouterBundle is a Symfony Bundle whose 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.
Support
Features
- Route definition
- Route matching
- Route generator
Installation
Add the bundle to your project using Composer:
composer require gos/pubsub-router-bundle
Once installed, you will need to add the bundle to your project.
If your project is based on Symfony Flex, the bundle should be automatically added to your config/bundles.php
file:
Gos\Bundle\PubSubRouterBundle\GosPubSubRouterBundle::class => ['all' => true],
If your project is based on the Symfony Standard Edition, you will need to add the bundle to your Kernel's registerBundles
method by editing app/AppKernel.php
:
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( ... new \Gos\Bundle\PubSubRouterBundle\GosPubSubRouterBundle() ); ... }
Bundle configuration
Below is an example bundle configuration. For projects based on Symfony Flex, this should be stored in config/packages/gos_pubsub_router.yaml
. For projects based on Symfony Standard Edition, this should be added to app/config/config.yml
.
#Gos PubSub Router gos_pubsub_router: routers: websocket: #available from container through gos_pubsub_router.websocket resources: - @GosNotificationBundle/Resources/config/pubsub/websocket/notification.yml redis: #available from container through gos_pubsub_router.redis resources: - @GosNotificationBundle/Resources/config/pubsub/redis/notification.yml
NOTE : Each router is 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: ['Acme\Chat\MessageHandler', 'addPushers'] requirements: role: "editor|admin|client" application: "[a-z]+" user_ref: "\d+"
Example with redis pubsub
user_app_notification: channel: notification:user:{role}:{application}:{user_ref} handler: ['Acme\Chat\MessageHandler', 'addPushers'] requirements: role: "editor|admin|client" application: "[a-z-]+-app" user_ref: "\d+"
NOTE : The handler is not typehinted, this allows you to define the handler callback in any way you'd like (such as an array to call a method on a class or a string to call a PHP function or a service from the container).
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); // $routeName -> 'user_app_notification // $route -> instance of Gos\Bundle\PubSubRouterBundle\Router\Route // $attributes -> [ 'role' => 'admin', 'application' => 'billing-app', 'user_ref' => '639409' ]
What about mismatch?
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, typehint against
Gos\Bundle\PubSubRouterBundle\Generator\GeneratorInterface
- If you only need to match route, typehint against
Gos\Bundle\PubSubRouterBundle\Matcher\MatcherInterface
- If you need both, typehint against
Gos\Bundle\PubSubRouterBundle\Router\RouterInterface
Router CLI
php bin/console gos:prouter:debug -r websocket
dump all registered routes for websocket router
License
MIT, See LICENSE
file in the root of project.