index/src/Http/Routing/PatternRoute.php

34 lines
870 B
PHP

<?php
// PatternRoute.php
// Created: 2024-03-28
// Updated: 2025-03-07
namespace Index\Http\Routing;
use Attribute;
use Closure;
/**
* Marks a method as a pattern route.
*/
#[Attribute(RouteAttribute::FLAGS)]
class PatternRoute extends RouteAttribute {
private string $pattern;
/**
* @param string $method HTTP Method.
* @param string $pattern URI path pattern.
* @param bool $raw If true, $pattern is taken as is, if false, $pattern is prefixed with #^ and suffixed with $#uD
*/
public function __construct(
private string $method,
string $pattern,
bool $raw = false
) {
$this->pattern = $raw ? $pattern : sprintf('#^%s$#uD', $pattern);
}
public function createInstance(Closure $handler): RouteInfo {
return RouteInfo::pattern($this->method, $this->pattern, $handler);
}
}