From c617dfad4a0d0bbf02d290bd242fd6d013fe0603 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Wed, 13 Dec 2023 13:54:03 -0500 Subject: [PATCH] Add server middleware autoconfiguration --- docs/managing-middleware.md | 35 ++++++++++--------- src/Attribute/AsServerMiddleware.php | 16 +++++++++ .../BabDevWebSocketExtension.php | 5 +++ 3 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 src/Attribute/AsServerMiddleware.php diff --git a/docs/managing-middleware.md b/docs/managing-middleware.md index 3a8dfd9..c0c9c12 100644 --- a/docs/managing-middleware.md +++ b/docs/managing-middleware.md @@ -22,17 +22,7 @@ New middleware should be added with a priority lower than 0 as the `BabDev\WebSo Middleware must have the decorated middleware as the first parameter in the constructor, and cannot use a named parameter in the service definition (i.e. `$middleware: !abstract decorated middleware` for YAML); this is required for the compiler pass which builds the middleware stack to correctly set the arguments. -Because priorities must be specified, this bundle does not provide autoconfiguration of the `BabDev\WebSocket\Server\ServerMiddleware` interface to the `babdev.websocket_server.server_middleware` tag, so services will have to be manually tagged. This can be done with either explicit configuration in your container configuration files, or if you are using autowiring, the `#[Autoconfigure]` attribute on your middleware class. - -```yaml -# config/services.yaml -services: - App\WebSocket\Middleware\EarlyMiddleware: - arguments: - - !abstract decorated middleware - tags: - - { name: babdev.websocket_server.server_middleware, priority: -75 } -``` +The bundle supports autoconfiguration of `BabDev\WebSocket\Server\ServerMiddleware` classes using the `#[AsServerMiddleware]` attribute on your middleware class. ```php ['priority' => -75]]])] -final class EarlyMiddleware implements ServerMiddleware +#[AsServerMiddleware(priority: -75)] +final readonly class EarlyMiddleware implements ServerMiddleware { public function __construct( - private readonly ServerMiddleware $middleware, - ) { - } + private ServerMiddleware $middleware, + ) {} /* Class implementation */ } ``` + +If you are not using autoconfiguration, the service should be tagged with the `babdev.websocket_server.server_middleware` service tag and the priority specified. + +```yaml +# config/services.yaml +services: + App\WebSocket\Middleware\EarlyMiddleware: + arguments: + - !abstract decorated middleware + tags: + - { name: babdev.websocket_server.server_middleware, priority: -75 } +``` diff --git a/src/Attribute/AsServerMiddleware.php b/src/Attribute/AsServerMiddleware.php new file mode 100644 index 0000000..d9bc491 --- /dev/null +++ b/src/Attribute/AsServerMiddleware.php @@ -0,0 +1,16 @@ +addTag('babdev_websocket_server.message_handler'); }); + $container->registerAttributeForAutoconfiguration(AsServerMiddleware::class, static function (ChildDefinition $definition, AsServerMiddleware $attribute): void { + $definition->addTag('babdev_websocket_server.server_middleware', ['priority' => $attribute->priority]); + }); + $container->registerForAutoconfiguration(PeriodicManager::class)->addTag('babdev_websocket_server.periodic_manager'); $this->registerAuthenticationConfiguration($mergedConfig, $container);