49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Phred\Http\Routing;
|
|
|
|
use FastRoute\RouteCollector;
|
|
use Phred\Http\Router;
|
|
|
|
/**
|
|
* Allows providers to register route callbacks that will be applied
|
|
* when the FastRoute dispatcher is built.
|
|
*/
|
|
final class RouteRegistry
|
|
{
|
|
/** @var list<callable(RouteCollector, Router):void> */
|
|
private static array $callbacks = [];
|
|
|
|
/** @var array<string,bool> */
|
|
private static array $loadedFiles = [];
|
|
|
|
public static function markAsLoaded(string $filePath): void
|
|
{
|
|
self::$loadedFiles[realpath($filePath) ?: $filePath] = true;
|
|
}
|
|
|
|
public static function isLoaded(string $filePath): bool
|
|
{
|
|
return isset(self::$loadedFiles[realpath($filePath) ?: $filePath]);
|
|
}
|
|
|
|
public static function add(callable $registrar): void
|
|
{
|
|
self::$callbacks[] = $registrar;
|
|
}
|
|
|
|
public static function clear(): void
|
|
{
|
|
self::$callbacks = [];
|
|
self::$loadedFiles = [];
|
|
}
|
|
|
|
public static function apply(RouteCollector $collector, Router $router): void
|
|
{
|
|
foreach (self::$callbacks as $cb) {
|
|
$cb($collector, $router);
|
|
}
|
|
}
|
|
}
|