middlewaresSorted) { $this->sortAllRegisteredMiddlewares(); $this->middlewaresSorted = true; } if (\Swoole\Coroutine::getCid() > 0) { return $this->createTestResponse( $client->{$method}(...$args) ); } $response = null; $exception = null; \Swoole\Coroutine\run(function () use ($client, $method, $args, &$response, &$exception): void { try { $response = $this->createTestResponse( $client->{$method}(...$args) ); } catch (\Throwable $throwable) { $exception = $throwable; } }); if ($exception !== null) { throw $exception; } if ($response === null) { throw new \RuntimeException('Test response not initialized.'); } return $response; } /** * 将所有已注册的 PriorityMiddleware 对象转为字符串类名 */ private function sortAllRegisteredMiddlewares(): void { $reflection = new \ReflectionClass(MiddlewareManager::class); $prop = $reflection->getProperty('container'); $prop->setAccessible(true); $container = $prop->getValue(); foreach ($container as $server => $paths) { foreach ($paths as $path => $methods) { foreach ($methods as $method => $middlewares) { $container[$server][$path][$method] = MiddlewareManager::sortMiddlewares($middlewares); } } } $prop->setValue(null, $container); } }