123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- declare (strict_types = 1);
- namespace think;
- use Closure;
- use InvalidArgumentException;
- use LogicException;
- use think\exception\Handle;
- use Throwable;
- class Middleware
- {
-
- protected $queue = [];
-
- protected $app;
- public function __construct(App $app)
- {
- $this->app = $app;
- }
-
- public function import(array $middlewares = [], string $type = 'global'): void
- {
- foreach ($middlewares as $middleware) {
- $this->add($middleware, $type);
- }
- }
-
- public function add($middleware, string $type = 'global'): void
- {
- $middleware = $this->buildMiddleware($middleware, $type);
- if (!empty($middleware)) {
- $this->queue[$type][] = $middleware;
- $this->queue[$type] = array_unique($this->queue[$type], SORT_REGULAR);
- }
- }
-
- public function route($middleware): void
- {
- $this->add($middleware, 'route');
- }
-
- public function controller($middleware): void
- {
- $this->add($middleware, 'controller');
- }
-
- public function unshift($middleware, string $type = 'global')
- {
- $middleware = $this->buildMiddleware($middleware, $type);
- if (!empty($middleware)) {
- if (!isset($this->queue[$type])) {
- $this->queue[$type] = [];
- }
- array_unshift($this->queue[$type], $middleware);
- }
- }
-
- public function all(string $type = 'global'): array
- {
- return $this->queue[$type] ?? [];
- }
-
- public function pipeline(string $type = 'global')
- {
- return (new Pipeline())
- ->through(array_map(function ($middleware) {
- return function ($request, $next) use ($middleware) {
- [$call, $params] = $middleware;
- if (is_array($call) && is_string($call[0])) {
- $call = [$this->app->make($call[0]), $call[1]];
- }
- $response = call_user_func($call, $request, $next, ...$params);
- if (!$response instanceof Response) {
- throw new LogicException('The middleware must return Response instance');
- }
- return $response;
- };
- }, $this->sortMiddleware($this->queue[$type] ?? [])))
- ->whenException([$this, 'handleException']);
- }
-
- public function end(Response $response)
- {
- foreach ($this->queue as $queue) {
- foreach ($queue as $middleware) {
- [$call] = $middleware;
- if (is_array($call) && is_string($call[0])) {
- $instance = $this->app->make($call[0]);
- if (method_exists($instance, 'end')) {
- $instance->end($response);
- }
- }
- }
- }
- }
-
- public function handleException($passable, Throwable $e)
- {
-
- $handler = $this->app->make(Handle::class);
- $handler->report($e);
- return $handler->render($passable, $e);
- }
-
- protected function buildMiddleware($middleware, string $type): array
- {
- if (is_array($middleware)) {
- [$middleware, $params] = $middleware;
- }
- if ($middleware instanceof Closure) {
- return [$middleware, $params ?? []];
- }
- if (!is_string($middleware)) {
- throw new InvalidArgumentException('The middleware is invalid');
- }
-
- $alias = $this->app->config->get('middleware.alias', []);
- if (isset($alias[$middleware])) {
- $middleware = $alias[$middleware];
- }
- if (is_array($middleware)) {
- $this->import($middleware, $type);
- return [];
- }
- return [[$middleware, 'handle'], $params ?? []];
- }
-
- protected function sortMiddleware(array $middlewares)
- {
- $priority = $this->app->config->get('middleware.priority', []);
- uasort($middlewares, function ($a, $b) use ($priority) {
- $aPriority = $this->getMiddlewarePriority($priority, $a);
- $bPriority = $this->getMiddlewarePriority($priority, $b);
- return $bPriority - $aPriority;
- });
- return $middlewares;
- }
-
- protected function getMiddlewarePriority($priority, $middleware)
- {
- [$call] = $middleware;
- if (is_array($call) && is_string($call[0])) {
- $index = array_search($call[0], array_reverse($priority));
- return false === $index ? -1 : $index;
- }
- return -1;
- }
- }
|