middleware = $middleware; } /** * @throws ConfigException * @throws Exception * 初始化函数路径 */ public function init() { $this->dir = Config::get('http.namespace', $this->dir); $this->response = Snowflake::app()->get('response'); } /** * @param bool $useTree */ public function setUseTree(bool $useTree): void { $this->useTree = $useTree ? ROUTER_TREE : ROUTER_HASH; } /** * @param $path * @param $handler * @param string $method * @return ?Node * @throws Exception */ public function addRoute($path, $handler, string $method = 'any'): ?Node { if (!isset($this->nodes[$method])) { $this->nodes[$method] = []; } if ($handler instanceof Closure) { $handler = Closure::bind($handler, di(Controller::class)); } return $this->tree($path, $handler, $method); } /** * @param $path * @param $handler * @param string $method * @return ?Node */ private function hash($path, $handler, string $method = 'any'): ?Node { $path = $this->resolve($path); $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); return $this->nodes[$method][$path]->bindHandler($handler); } /** * @param $path * @return string */ #[Pure] private function resolve($path): string { $paths = array_column($this->groupTacks, 'prefix'); if (empty($paths)) { return '/' . ltrim($path, '/'); } $paths = '/' . implode('/', $paths); if ($path != '/') { return $paths . '/' . ltrim($path, '/'); } return $paths . '/'; } /** * @param $path * @param $handler * @param string $method * @return Node * @throws Exception */ private function tree($path, $handler, string $method = 'any'): Node { [$first, $explode] = $this->split($path); if (!isset($this->nodes[$method]['/'])) { $this->nodes[$method]['/'] = $this->NodeInstance('/', 0, $method); } $parent = $this->nodes[$method]['/']; if (!empty($first) && !empty($explode)) { $parent = $this->bindNode($parent, $explode, $method); } $parent->path = $path; return $parent->bindHandler($handler); } /** * @param Node $parent * @param array $explode * @param $method * @return Node * @throws Exception */ private function bindNode(Node $parent, array $explode, $method): Node { $a = 0; foreach ($explode as $value) { ++$a; $search = $parent->findNode($value); if ($search === null) { $parent = $parent->addChild($this->NodeInstance($value, $a, $method)); } else { $parent = $search; } var_dump($parent->path, $value); } return $parent; } /** * @param $route * @param $handler * @return Node|null * @throws */ public function socket($route, $handler): ?Node { return $this->addRoute($route, $handler, 'socket'); } /** * @param $route * @param $handler * @return Node|null * @throws */ public function post($route, $handler): ?Node { return $this->addRoute($route, $handler, 'POST'); } /** * @param $route * @param $handler * @return Node|null * @throws */ public function get($route, $handler): ?Node { return $this->addRoute($route, $handler, 'GET'); } /** * @param $port * @param callable $callback * @return mixed * @throws */ public function addRpcService($port, callable $callback): mixed { return call_user_func($callback, new Actuator($port)); } /** * @param $route * @param $handler * @return Node|null * @throws */ public function options($route, $handler): ?Node { return $this->addRoute($route, $handler, 'OPTIONS'); } /** * @param $route * @param $handler * @return Any * @throws */ public function any($route, $handler): Any { $nodes = []; foreach ($this->methods as $method) { $nodes[] = $this->addRoute($route, $handler, $method); } return new Any($nodes); } /** * @param $route * @param $handler * @return Node|null * @throws */ public function delete($route, $handler): ?Node { return $this->addRoute($route, $handler, 'DELETE'); } /** * @param $route * @param $handler * @return Node|null * @throws Exception */ public function head($route, $handler): ?Node { return $this->addRoute($route, $handler, 'HEAD'); } /** * @param $route * @param $handler * @return Node|null * @throws */ public function put($route, $handler): ?Node { return $this->addRoute($route, $handler, 'PUT'); } /** * @param $value * @param int $index * @param string $method * @return Node * @throws */ public function NodeInstance($value, int $index = 0, string $method = 'GET'): Node { $node = new Node(); $node->childes = []; $node->path = $value; $node->index = $index; $node->method = $method; $node->namespace = $this->loadNamespace($method); $name = array_column($this->groupTacks, 'middleware'); if ($this->middleware instanceof \Closure) { $node->addMiddleware([$this->middleware]); } if (is_array($name)) { $node->addMiddleware($this->resolve_middleware($name)); } return $node; } /** * @param string|array $middleware * @return array * @throws NotFindClassException * @throws ReflectionException */ private function resolve_middleware(string|array $middleware): array { if (is_string($middleware)) { $middleware = [$middleware]; } $array = []; foreach ($middleware as $value) { if (is_array($value)) { foreach ($value as $item) { $array[] = $this->getMiddlewareInstance($item); } } else { $array[] = $this->getMiddlewareInstance($value); } } return array_filter($array); } /** * @param $value * @return Closure|array|null * @throws NotFindClassException * @throws ReflectionException */ private function getMiddlewareInstance($value): null|Closure|array { if (!is_string($value)) { return $value; } $value = Snowflake::createObject($value); if (!($value instanceof Middleware)) { return null; } return [$value, 'onHandler']; } /** * @param $method * @return array */ private function loadNamespace($method): array { $name = array_column($this->groupTacks, 'namespace'); array_unshift($name, $this->dir); return array_filter($name); } /** * @param array $config * @param callable $callback * 路由分组 * @param null $stdClass */ public function group(array $config, callable $callback, $stdClass = null) { $this->groupTacks[] = $config; if ($stdClass) { $callback($stdClass); } else { $callback($this); } array_pop($this->groupTacks); } /** * @return string */ public function addPrefix(): string { $prefix = array_column($this->groupTacks, 'prefix'); $prefix = array_filter($prefix); if (empty($prefix)) { return ''; } return '/' . implode('/', $prefix); } /** * @param array|null $explode * @param $method * @return Node|null * 查找指定路由 * @throws RequestException */ public function tree_search(?array $explode, $method): ?Node { if (!isset($this->nodes[$method])) { return null; } $parent = $this->nodes[$method]['/']; while ($value = array_shift($explode)) { $node = $parent->findNode($value); var_dump($node); if (!$node) { throw new RequestException('