diff --git a/src/Client.php b/src/Client.php index f1202ae..b736e35 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,143 +1,43 @@ withMethod($method) - ->coroutine( - $this->matchHost($path), - $this->paramEncode($params) - ); - } + + private CoroutineClient|Curl $abstracts; /** - * @param $url - * @param array|string $data - * @return ResponseInterface - * @throws Exception 使用swoole协程方式请求 + * @param string $host + * @param int $port + * @param bool $isSsl */ - private function coroutine($url, array|string $data = []): ResponseInterface + public function __construct(string $host, int $port, bool $isSsl = false) { - try { - $client = $this->generate_client($data, ...$url); - if ($client->statusCode < 0) { - throw new Exception($client->errMsg); - } - return (new Response())->withStatus($client->getStatusCode()) - ->withHeaders($client->getHeaders()) - ->withBody(new Stream($client->getBody())); - } catch (\Throwable $exception) { - Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); - return (new Response())->withStatus(-1)->withHeaders([]) - ->withBody(new Stream(jTraceEx($exception))); - } - } - - - /** - * @param $data - * @param $host - * @param $isHttps - * @param $path - * @return SwowClient - */ - private function generate_client($data, $host, $isHttps, $path): SwowClient - { - if ($isHttps || $this->isSSL()) { - $client = new SwowClient($host, 443, true); + if (Context::inCoroutine()) { + $this->abstracts = new CoroutineClient($host, $port, $isSsl); } else { - $client = new SwowClient($host, $this->getPort(), false); + $this->abstracts = new Curl($host, $port, $isSsl); } - $client->set($this->settings()); - if (!empty($this->getAgent())) { - $this->withAddedHeader('User-Agent', $this->getAgent()); - } - $client->setHeaders($this->getHeader()); - $client->setMethod(strtoupper($this->getMethod())); - $client->execute($this->setParams($client, $path, $data)); - $client->close(); - return $client; } /** - * @param SwowClient $client - * @param $path - * @param $data - * @return string + * @param string $name + * @param array $arguments + * @return mixed */ - private function setParams(SwowClient $client, $path, $data): string + public function __call(string $name, array $arguments) { - $content = $this->getData()->getContents(); - if (!empty($content)) { - $client->setData($content); - } - if ($this->isGet()) { - if (!empty($data)) $path .= '?' . $data; - } else { - $data = $this->mergeParams($data); - if (!empty($data)) { - $client->setData($data); - } - } - return $path; + return $this->abstracts->{$name}(...$arguments); } - /** - * @return array - */ - #[Pure] private function settings(): array - { - $sslCert = $this->getSslCertFile(); - $sslKey = $this->getSslKeyFile(); - $sslCa = $this->getCa(); - - $params = []; - if ($this->getConnectTimeout() > 0) { - $params['timeout'] = $this->getConnectTimeout(); - } - if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { - return $params; - } - - $params['ssl_host_name'] = $this->getHost(); - $params['ssl_cert_file'] = $this->getSslCertFile(); - $params['ssl_key_file'] = $this->getSslKeyFile(); - $params['ssl_verify_peer'] = true; - $params['ssl_cafile'] = $sslCa; - - return $params; - } } diff --git a/src/CoroutineClient.php b/src/CoroutineClient.php new file mode 100644 index 0000000..83b566c --- /dev/null +++ b/src/CoroutineClient.php @@ -0,0 +1,143 @@ +withMethod($method) + ->coroutine( + $this->matchHost($path), + $this->paramEncode($params) + ); + } + + + /** + * @param $url + * @param array|string $data + * @return ResponseInterface + * @throws Exception 使用swoole协程方式请求 + */ + private function coroutine($url, array|string $data = []): ResponseInterface + { + try { + $client = $this->generate_client($data, ...$url); + if ($client->statusCode < 0) { + throw new Exception($client->errMsg); + } + return (new Response())->withStatus($client->getStatusCode()) + ->withHeaders($client->getHeaders()) + ->withBody(new Stream($client->getBody())); + } catch (\Throwable $exception) { + Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); + return (new Response())->withStatus(-1)->withHeaders([]) + ->withBody(new Stream(jTraceEx($exception))); + } + } + + + /** + * @param $data + * @param $host + * @param $isHttps + * @param $path + * @return SwowClient + */ + private function generate_client($data, $host, $isHttps, $path): SwowClient + { + if ($isHttps || $this->isSSL()) { + $client = new SwowClient($host, 443, true); + } else { + $client = new SwowClient($host, $this->getPort(), false); + } + $client->set($this->settings()); + if (!empty($this->getAgent())) { + $this->withAddedHeader('User-Agent', $this->getAgent()); + } + $client->setHeaders($this->getHeader()); + $client->setMethod(strtoupper($this->getMethod())); + $client->execute($this->setParams($client, $path, $data)); + $client->close(); + return $client; + } + + + /** + * @param SwowClient $client + * @param $path + * @param $data + * @return string + */ + private function setParams(SwowClient $client, $path, $data): string + { + $content = $this->getData()->getContents(); + if (!empty($content)) { + $client->setData($content); + } + if ($this->isGet()) { + if (!empty($data)) $path .= '?' . $data; + } else { + $data = $this->mergeParams($data); + if (!empty($data)) { + $client->setData($data); + } + } + return $path; + } + + /** + * @return array + */ + #[Pure] private function settings(): array + { + $sslCert = $this->getSslCertFile(); + $sslKey = $this->getSslKeyFile(); + $sslCa = $this->getCa(); + + $params = []; + if ($this->getConnectTimeout() > 0) { + $params['timeout'] = $this->getConnectTimeout(); + } + if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { + return $params; + } + + $params['ssl_host_name'] = $this->getHost(); + $params['ssl_cert_file'] = $this->getSslCertFile(); + $params['ssl_key_file'] = $this->getSslKeyFile(); + $params['ssl_verify_peer'] = true; + $params['ssl_cafile'] = $sslCa; + + return $params; + } +}