diff --git a/composer.json b/composer.json index 06fcd83..9db0a74 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,5 @@ { "name": "cloudtay/ripple-http", - "version": "v0.2", "license": "MIT", "authors": [ { @@ -10,10 +9,13 @@ ], "autoload": { "psr-4": { - "Ripple\\App\\Http\\": "src/" + "Ripple\\Http\\": "src/" } }, "require": { + "ext-sockets": "*", + "guzzlehttp/guzzle": "*", + "symfony/http-foundation": "*" }, "require-dev": { "ext-posix": "*", @@ -21,7 +23,7 @@ "ext-curl": "*", "ext-parallel": "*", "ext-zlib": "*", - "cloudtay/ripple": "dev-main", + "cloudtay/ripple": "^1.0.0-alpha", "phpunit/phpunit": "*", "friendsofphp/php-cs-fixer": "*" }, diff --git a/src/Client/Client.php b/src/Client.php similarity index 99% rename from src/Client/Client.php rename to src/Client.php index 6d14993..f51f2d8 100644 --- a/src/Client/Client.php +++ b/src/Client.php @@ -32,19 +32,21 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Client; +namespace Ripple\Http; use Closure; use Co\IO; use GuzzleHttp\Psr7\MultipartStream; use GuzzleHttp\Psr7\Response; use InvalidArgumentException; -use Ripple\Coroutine\Promise; +use Psr\Http\Message\RequestInterface; +use Ripple\Http\Client\Connection; +use Ripple\Http\Client\ConnectionPool; +use Ripple\Promise; use Ripple\Socket\SocketStream; use Ripple\Socket\Tunnel\Http; use Ripple\Socket\Tunnel\Socks5; use Ripple\Stream\Exception\ConnectionException; -use Psr\Http\Message\RequestInterface; use Throwable; use function Co\cancel; diff --git a/src/Client/Capture/ServerSentEvents.php b/src/Client/Capture/ServerSentEvents.php index a17f896..50cc725 100644 --- a/src/Client/Capture/ServerSentEvents.php +++ b/src/Client/Capture/ServerSentEvents.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Client\Capture; +namespace Ripple\Http\Client\Capture; use Closure; use GuzzleHttp\Psr7\Response; diff --git a/src/Client/Connection.php b/src/Client/Connection.php index 4ceeb17..3165ef5 100644 --- a/src/Client/Connection.php +++ b/src/Client/Connection.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Client; +namespace Ripple\Http\Client; use GuzzleHttp\Psr7\Response; use Ripple\Socket\SocketStream; diff --git a/src/Client/ConnectionPool.php b/src/Client/ConnectionPool.php index 1be4dbc..c54cf92 100644 --- a/src/Client/ConnectionPool.php +++ b/src/Client/ConnectionPool.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Client; +namespace Ripple\Http\Client; use Co\IO; use Ripple\Socket\SocketStream; diff --git a/src/Enum/Method.php b/src/Enum/Method.php index 5f6c686..e009498 100644 --- a/src/Enum/Method.php +++ b/src/Enum/Method.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Enum; +namespace Ripple\Http\Enum; enum Method { diff --git a/src/Enum/Status.php b/src/Enum/Status.php index c53f09a..c116aeb 100644 --- a/src/Enum/Status.php +++ b/src/Enum/Status.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Enum; +namespace Ripple\Http\Enum; enum Status { diff --git a/src/Guzzle.php b/src/Guzzle.php new file mode 100644 index 0000000..653f8fa --- /dev/null +++ b/src/Guzzle.php @@ -0,0 +1,102 @@ + PHP_SAPI === 'cli'], $config)); + $this->rippleHandler = new RippleHandler($httpClient); + } + + /*** @return Guzzle */ + public static function getInstance(): Guzzle + { + if (!isset(self::$instance)) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * @Author cclilshy + * @Date 2024/8/31 14:28 + * @return RippleHandler + */ + public function getHandler(): RippleHandler + { + return $this->rippleHandler; + } + + /** + * @return \Ripple\Http\Client + */ + public function getHttpClient(): \Ripple\Http\Client + { + return $this->getHandler()->getHttpClient(); + } + + /** + * @param array $config + * + * @return \GuzzleHttp\Client + */ + public static function newClient(array $config = []): Client + { + return new Client(array_merge(['handler' => self::getInstance()->getHandler()], $config)); + } +} diff --git a/src/Http.php b/src/Guzzle/RippleHandler.php similarity index 64% rename from src/Http.php rename to src/Guzzle/RippleHandler.php index 5605ad0..8f3b35a 100644 --- a/src/Http.php +++ b/src/Guzzle/RippleHandler.php @@ -32,57 +32,51 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http; +namespace Ripple\Http\Guzzle; -use Ripple\App\Http\Client\Client; -use Ripple\App\Http\Server\Server; -use Ripple\LibraryAbstract; -use Ripple\Stream\Exception\ConnectionException; -use Ripple\Plugins\Guzzle\Guzzle; -use Ripple\Utils\Output; +use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\Exception\TransferException; +use GuzzleHttp\Promise\Promise; +use GuzzleHttp\Promise\PromiseInterface; +use Psr\Http\Message\RequestInterface; +use Ripple\Http\Client; +use Throwable; -/** - * @Author cclilshy - * @Date 2024/8/16 09:35 - */ -class Http extends LibraryAbstract +class RippleHandler { - /** - * @var LibraryAbstract - */ - protected static LibraryAbstract $instance; - - /** - * @return Guzzle - */ - public function Guzzle(): Guzzle + public function __construct(private Client $httpClient) { - return Guzzle::getInstance(); } /** - * @param string $address - * @param mixed $context + * @param RequestInterface $request + * @param array $options * - * @return Server|false + * @return PromiseInterface */ - public function server(string $address, mixed $context = null): Server|false + public function __invoke(RequestInterface $request, array $options): PromiseInterface { - try { - return new Server($address, $context); - } catch (ConnectionException $e) { - Output::error($e->getMessage()); - return false; - } + $promise = new Promise(function () use ($request, $options, &$promise) { + try { + $response = $this->httpClient->request($request, $options); + $promise->resolve($response); + } catch (GuzzleException $exception) { + $promise->reject($exception); + } catch (Throwable $exception) { + $promise->reject(new TransferException($exception->getMessage())); + } + }); + + return $promise; } /** - * @param array $config - * + * @Author cclilshy + * @Date 2024/8/31 14:31 * @return Client */ - public function client(array $config): Client + public function getHttpClient(): Client { - return new Client($config); + return $this->httpClient; } } diff --git a/src/Server/Server.php b/src/Server.php similarity index 97% rename from src/Server/Server.php rename to src/Server.php index 5c6fc46..dc2f610 100644 --- a/src/Server/Server.php +++ b/src/Server.php @@ -32,13 +32,15 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server; +namespace Ripple\Http; use Closure; use Co\IO; use InvalidArgumentException; -use Ripple\App\Http\Enum\Status; -use Ripple\App\Http\Server\Exception\FormatException; +use Ripple\Http\Enum\Status; +use Ripple\Http\Server\Connection; +use Ripple\Http\Server\Exception\FormatException; +use Ripple\Http\Server\Request; use Ripple\Socket\SocketStream; use Ripple\Stream\Exception\ConnectionException; use Ripple\Utils\Output; diff --git a/src/Server/Chunk.php b/src/Server/Chunk.php index f5233ca..ddd97ab 100644 --- a/src/Server/Chunk.php +++ b/src/Server/Chunk.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server; +namespace Ripple\Http\Server; use function dechex; use function explode; diff --git a/src/Server/Connection.php b/src/Server/Connection.php index d4dd694..e5a40c0 100644 --- a/src/Server/Connection.php +++ b/src/Server/Connection.php @@ -32,11 +32,11 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server; +namespace Ripple\Http\Server; use Closure; -use Ripple\App\Http\Server\Exception\FormatException; -use Ripple\App\Http\Server\Upload\MultipartHandler; +use Ripple\Http\Server\Exception\FormatException; +use Ripple\Http\Server\Upload\MultipartHandler; use Ripple\Socket\SocketStream; use Ripple\Stream\Exception\RuntimeException; use Ripple\Utils\Output; diff --git a/src/Server/Exception/Exception.php b/src/Server/Exception/Exception.php index c87654b..08ba401 100644 --- a/src/Server/Exception/Exception.php +++ b/src/Server/Exception/Exception.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server\Exception; +namespace Ripple\Http\Server\Exception; /** * @Author cclilshy diff --git a/src/Server/Exception/FormatException.php b/src/Server/Exception/FormatException.php index 5e1b5d7..6a5ca5a 100644 --- a/src/Server/Exception/FormatException.php +++ b/src/Server/Exception/FormatException.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server\Exception; +namespace Ripple\Http\Server\Exception; /** * @Author cclilshy diff --git a/src/Server/Exception/TransferException.php b/src/Server/Exception/TransferException.php index 8e81f5d..31e793a 100644 --- a/src/Server/Exception/TransferException.php +++ b/src/Server/Exception/TransferException.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server\Exception; +namespace Ripple\Http\Server\Exception; /** * @Author cclilshy diff --git a/src/Server/Request.php b/src/Server/Request.php index 942d08d..e09c0fa 100644 --- a/src/Server/Request.php +++ b/src/Server/Request.php @@ -32,7 +32,7 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server; +namespace Ripple\Http\Server; use Ripple\Socket\SocketStream; diff --git a/src/Server/Response.php b/src/Server/Response.php index 7d0e365..88a4a1b 100644 --- a/src/Server/Response.php +++ b/src/Server/Response.php @@ -32,13 +32,13 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server; +namespace Ripple\Http\Server; use Closure; use Generator; use Ripple\Socket\SocketStream; +use Ripple\Stream; use Ripple\Stream\Exception\ConnectionException; -use Ripple\Stream\Stream; use Throwable; use function basename; diff --git a/src/Server/Upload/MultipartHandler.php b/src/Server/Upload/MultipartHandler.php index 54cb0dc..b6e1a0e 100644 --- a/src/Server/Upload/MultipartHandler.php +++ b/src/Server/Upload/MultipartHandler.php @@ -32,9 +32,9 @@ * 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。 */ -namespace Ripple\App\Http\Server\Upload; +namespace Ripple\Http\Server\Upload; -use Ripple\App\Http\Server\Exception\FormatException; +use Ripple\Http\Server\Exception\FormatException; use Symfony\Component\HttpFoundation\File\UploadedFile; use function array_pop; diff --git a/tests/HttpTest.php b/tests/HttpTest.php index f2ee4b2..5b6f5fc 100644 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -1,13 +1,12 @@ server('http://127.0.0.1:8008', $context); + $server = new Ripple\Http\Server('http://127.0.0.1:8008', $context); $server->onRequest(function (Request $request) { $url = \trim($request->SERVER['REQUEST_URI']); $method = \strtoupper($request->SERVER['REQUEST_METHOD']); @@ -107,7 +106,10 @@ public function test_httpServer(): void } } - Plugin::Guzzle()->getHttpClient()->getConnectionPool()->clearConnectionPool(); + Guzzle::getInstance() + ->getHttpClient() + ->getConnectionPool() + ->clearConnectionPool(); \gc_collect_cycles(); if ($baseMemory !== \memory_get_usage()) { @@ -125,7 +127,7 @@ public function test_httpServer(): void private function httpGet(): void { $hash = \md5(\uniqid()); - $client = Plugin::Guzzle()->newClient(); + $client = Guzzle::newClient(); $response = $client->get('http://127.0.0.1:8008/', [ 'query' => [ 'query' => $hash, @@ -143,7 +145,7 @@ private function httpGet(): void private function httpPost(): void { $hash = \md5(\uniqid()); - $client = Plugin::Guzzle()->newClient(); + $client = Guzzle::newClient(); $response = $client->post('http://127.0.0.1:8008/', [ 'json' => [ 'query' => $hash, @@ -160,7 +162,7 @@ private function httpPost(): void */ private function httpFile(): void { - $client = Plugin::Guzzle()->newClient(); + $client = Guzzle::newClient(); $path = \tempnam(\sys_get_temp_dir(), 'test'); \file_put_contents($path, \str_repeat('a', 81920)); $hash = \md5_file($path); @@ -212,7 +214,7 @@ private function httpClient(): void foreach ($urls as $url) { $list[] = async(function () use ($url) { try { - return [$url, Plugin::Guzzle()->newClient()->get($url, ['timeout' => 10])]; + return [$url, Guzzle::newClient()->get($url, ['timeout' => 10])]; } catch (Throwable $exception) { return [$url, $exception]; }