Skip to content

Commit

Permalink
Add server middleware autoconfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Dec 13, 2023
1 parent abe2726 commit c617dfa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
35 changes: 18 additions & 17 deletions docs/managing-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,35 @@ 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
<?php declare(strict_types=1);

namespace App\WebSocket\Middleware;

use BabDev\WebSocket\Server\ServerMiddleware;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use BabDev\WebSocketBundle\Attribute\AsServerMiddleware;

#[Autoconfigure(tags: [['babdev.websocket_server.server_middleware' => ['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 }
```
16 changes: 16 additions & 0 deletions src/Attribute/AsServerMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace BabDev\WebSocketBundle\Attribute;

use BabDev\WebSocket\Server\ServerMiddleware;

/**
* Attribute used to configure {@see ServerMiddleware}.
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsServerMiddleware
{
public function __construct(
public int $priority,
) {}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/BabDevWebSocketExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BabDev\WebSocketBundle\DependencyInjection;

use BabDev\WebSocketBundle\Attribute\AsMessageHandler;
use BabDev\WebSocketBundle\Attribute\AsServerMiddleware;
use BabDev\WebSocketBundle\DependencyInjection\Factory\Authentication\AuthenticationProviderFactory;
use BabDev\WebSocketBundle\PeriodicManager\PeriodicManager;
use Doctrine\DBAL\Connection;
Expand Down Expand Up @@ -47,6 +48,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$definition->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);
Expand Down

0 comments on commit c617dfa

Please sign in to comment.