改名
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
|
||||
|
||||
use Swoole\Coroutine;
|
||||
|
||||
abstract class BaseContext
|
||||
{
|
||||
protected static array $pool = [];
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
use Closure;
|
||||
use Kiri\Kiri;
|
||||
|
||||
class HandlerManager
|
||||
{
|
||||
@@ -43,4 +42,20 @@ class HandlerManager
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function dump(): array
|
||||
{
|
||||
$array = [];
|
||||
foreach (static::$handlers as $path => $handlers) {
|
||||
$array[] = [
|
||||
'path' => $path,
|
||||
'method' => implode(',', array_keys($handlers))
|
||||
];
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
/**
|
||||
* Class HttpService
|
||||
* @package Http\Abstracts
|
||||
*/
|
||||
abstract class HttpService extends Component
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function write($message, string $category = 'app')
|
||||
{
|
||||
$logger = Kiri::app()->getLogger();
|
||||
$logger->write($message, $category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if (method_exists($this, $name)) {
|
||||
return $this->{$name}();
|
||||
}
|
||||
$handler = 'get' . ucfirst($name);
|
||||
if (method_exists($this, $handler)) {
|
||||
return $this->{$handler}();
|
||||
}
|
||||
if (property_exists($this, $name)) {
|
||||
return $this->$name;
|
||||
}
|
||||
$message = sprintf('method %s::%s not exists.', static::class, $name);
|
||||
throw new Exception($message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Kiri\Abstracts\BaseObject;
|
||||
|
||||
|
||||
/**
|
||||
* Class MiddlewareManager
|
||||
* @package Http\Route
|
||||
*/
|
||||
class MiddlewareManager extends BaseObject
|
||||
{
|
||||
|
||||
private static array $_middlewares = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @param array|string $middlewares
|
||||
* @return bool
|
||||
*/
|
||||
public static function add($class, $method, array|string $middlewares): bool
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
if (!isset(static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method] = [];
|
||||
}
|
||||
if (is_string($middlewares) && !in_array($middlewares, static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method][] = $middlewares;
|
||||
} else {
|
||||
foreach ($middlewares as $middleware) {
|
||||
if (in_array($middleware, static::$_middlewares[$class . '::' . $method])) {
|
||||
continue;
|
||||
}
|
||||
static::$_middlewares[$class . '::' . $method][] = $middleware;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $handler
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($handler): mixed
|
||||
{
|
||||
if ($handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
[$class, $method] = [$handler[0], $handler[1]];
|
||||
if (!static::hasMiddleware($class, $method)) {
|
||||
return null;
|
||||
}
|
||||
return static::$_middlewares[$class . '::' . $method];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasMiddleware($class, $method): bool
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return isset(static::$_middlewares[$class . '::' . $method]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user