sirix / mezzio-routing-contracts
Contracts for sirix/mezzio-routing-attributes route attribute modifiers
Package info
github.com/sirix777/mezzio-routing-contracts
pkg:composer/sirix/mezzio-routing-contracts
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- phpunit/phpunit: ^11.5
README
Contracts for sirix/mezzio-routing-attributes route attribute modifiers.
This package contains the stable public contract used by routing attributes that need to contribute middleware or default route options to a route definition.
Installation
composer require sirix/mezzio-routing-contracts
Usage
Implement RouteAttributeModifierInterface on any route-related attribute to inject middleware and/or provide default route options.
use Attribute; use Sirix\Mezzio\Routing\Contracts\RouteAttributeModifierInterface; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] final readonly class RequireTenant implements RouteAttributeModifierInterface { public function __construct(private string $tenantHeader = 'x-tenant-id') {} public function getMiddleware(): array { return [RequireTenantMiddleware::class]; } public function getDefaults(): array { return ['tenant_header' => $this->tenantHeader]; } }
Usage with routing attributes:
use Sirix\Mezzio\Routing\Attributes\Attribute\Get; final class OrdersHandler { #[Get('/orders', name: 'orders.list')] #[RequireTenant('x-org-id')] public function list(): ResponseInterface { // ... } }
The routing-attributes package discovers all implementations of RouteAttributeModifierInterface at boot time and merges their middleware and defaults into the route definition.
Contract
interface RouteAttributeModifierInterface { /** @return list<class-string<MiddlewareInterface>|non-empty-string> */ public function getMiddleware(): array; /** @return array<string, mixed> */ public function getDefaults(): array; }
getMiddleware()
Returns middleware identifiers that should be appended to the route pipeline.
Each item must be a middleware class name implementing Psr\Http\Server\MiddlewareInterface or another non-empty middleware identifier supported by the consuming router integration.
getDefaults()
Returns default route options keyed by option name. Consumers merge these values into the route defaults/options for the route that carries the attribute.
Versioning
The 1.x series follows Semantic Versioning.
Breaking changes to public contracts are reserved for the next major version.