/** * @param \DIServer\Interfaces\IHandler $handler * * @return \Closure */ private function _createPipeClosure(\DIServer\Interfaces\IHandler $handler) { $pipeline = new Pipeline(); $middlewareClasses = $handler->GetMiddlewares(); $middlewareHandlers = []; foreach ($middlewareClasses as $middlewareClass) { $refClass = new \ReflectionClass($middlewareClass); if (!$refClass->isSubclassOf(IMiddleware::class)) { Log::Warning("Try to load {$middlewareClass} in " . get_class($handler) . " but is not instance of IMiddleware"); continue; } $middlewareHandlers[] = Container::BuildWithClass($middlewareClass); } return $pipeline->Through($middlewareHandlers)->Prepared(function ($request) use($handler) { //最后一层封装为Handler的默认Handle方法 return Container::CallMethod($handler, 'Handle', ['request' => $request]); }); }
public function __construct() { $pipeline = new Pipeline(); $middlewareClasses = $this->GetMiddlewares(); $middlewareHandlers = []; foreach ($middlewareClasses as $middlewareClass) { $refClass = new \ReflectionClass($middlewareClass); if (!$refClass->isSubclassOf(IMiddleware::class)) { Log::Debug("Try to load {$middlewareClass} in " . get_class($this) . " but is not instance of IMiddleware"); continue; } $middlewareHandlers[] = Container::BuildWithClass($middlewareClass); } $this->dispatchRequestClosure = $pipeline->Through($middlewareHandlers)->Prepared(function ($request) { //最后一层封装为Handler的默认Handle方法 return Container::CallMethod($this, 'Handle', ['request' => $request]); }); }