Files
kiri-core/http-handler/Abstracts/HandlerManager.php
T

71 lines
1.2 KiB
PHP
Raw Normal View History

2021-09-17 18:55:08 +08:00
<?php
namespace Http\Handler\Abstracts;
use Closure;
class HandlerManager
{
private static array $handlers = [];
/**
2021-09-18 10:38:38 +08:00
* @param string $path
* @param string $method
* @param \Http\Handler\Handler|Closure $handler
2021-09-17 18:55:08 +08:00
*/
2021-09-18 10:38:38 +08:00
public static function add(string $path, string $method, \Http\Handler\Handler|Closure $handler)
2021-09-17 18:55:08 +08:00
{
if (!isset(static::$handlers[$path])) {
static::$handlers[$path] = [];
}
static::$handlers[$path][$method] = $handler;
}
/**
* @param $path
* @param $method
* @return null|int|array|Closure
*/
public static function get($path, $method): null|int|\Http\Handler\Handler|Closure
{
if (!isset(static::$handlers[$path])) {
return null;
}
$array = static::$handlers[$path][$method] ?? null;
if (is_null($array)) {
return 405;
}
return $array;
}
2021-09-18 16:54:39 +08:00
2021-09-24 18:59:43 +08:00
/**
* @return array
*/
public static function getHandlers(): array
{
return static::$handlers;
}
2021-09-18 16:54:39 +08:00
/**
* @return array
*/
public static function dump(): array
{
$array = [];
foreach (static::$handlers as $path => $handlers) {
$array[] = [
'path' => $path,
'method' => implode(',', array_keys($handlers))
];
}
return $array;
}
2021-09-17 18:55:08 +08:00
}