Skip to content

Commit

Permalink
pre-release: v1.0.0-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Oct 31, 2024
1 parent b2b6b0e commit d8ba194
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 72 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "cloudtay/ripple-http",
"version": "v0.2",
"license": "MIT",
"authors": [
{
Expand All @@ -10,18 +9,21 @@
],
"autoload": {
"psr-4": {
"Ripple\\App\\Http\\": "src/"
"Ripple\\Http\\": "src/"
}
},
"require": {
"ext-sockets": "*",
"guzzlehttp/guzzle": "*",
"symfony/http-foundation": "*"
},
"require-dev": {
"ext-posix": "*",
"ext-pcntl": "*",
"ext-curl": "*",
"ext-parallel": "*",
"ext-zlib": "*",
"cloudtay/ripple": "dev-main",
"cloudtay/ripple": "^1.0.0-alpha",
"phpunit/phpunit": "*",
"friendsofphp/php-cs-fixer": "*"
},
Expand Down
8 changes: 5 additions & 3 deletions src/Client/Client.php → src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Capture/ServerSentEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Client\Capture;
namespace Ripple\Http\Client\Capture;

use Closure;
use GuzzleHttp\Psr7\Response;
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Client;
namespace Ripple\Http\Client;

use GuzzleHttp\Psr7\Response;
use Ripple\Socket\SocketStream;
Expand Down
2 changes: 1 addition & 1 deletion src/Client/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Client;
namespace Ripple\Http\Client;

use Co\IO;
use Ripple\Socket\SocketStream;
Expand Down
2 changes: 1 addition & 1 deletion src/Enum/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Enum;
namespace Ripple\Http\Enum;

enum Method
{
Expand Down
2 changes: 1 addition & 1 deletion src/Enum/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Enum;
namespace Ripple\Http\Enum;

enum Status
{
Expand Down
102 changes: 102 additions & 0 deletions src/Guzzle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php declare(strict_types=1);
/*
* Copyright (c) 2023-2024.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* 特此免费授予任何获得本软件及相关文档文件(“软件”)副本的人,不受限制地处理
* 本软件,包括但不限于使用、复制、修改、合并、出版、发行、再许可和/或销售
* 软件副本的权利,并允许向其提供本软件的人做出上述行为,但须符合以下条件:
*
* 上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。
*
* 本软件按“原样”提供,不提供任何形式的保证,无论是明示或暗示的,
* 包括但不限于适销性、特定目的的适用性和非侵权性的保证。在任何情况下,
* 无论是合同诉讼、侵权行为还是其他方面,作者或版权持有人均不对
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\Http;

use GuzzleHttp\Client;
use Ripple\Http\Guzzle\RippleHandler;

use function array_merge;

use const PHP_SAPI;

/**
* @Author cclilshy
* @Date 2024/8/16 09:37
*/
class Guzzle
{
/*** @var Guzzle */
protected static Guzzle $instance;

/*** @var RippleHandler */
protected RippleHandler $rippleHandler;

/**
*
*/
protected function __construct()
{
$config = [];
$httpClient = new \Ripple\Http\Client(array_merge(['pool' => 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));
}
}
66 changes: 30 additions & 36 deletions src/Http.php → src/Guzzle/RippleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
readonly 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;
}
}
8 changes: 5 additions & 3 deletions src/Server/Server.php → src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Server;
namespace Ripple\Http\Server;

use function dechex;
use function explode;
Expand Down
6 changes: 3 additions & 3 deletions src/Server/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Exception/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Server\Exception;
namespace Ripple\Http\Server\Exception;

/**
* @Author cclilshy
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Exception/FormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Server\Exception;
namespace Ripple\Http\Server\Exception;

/**
* @Author cclilshy
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Exception/TransferException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Server\Exception;
namespace Ripple\Http\Server\Exception;

/**
* @Author cclilshy
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

namespace Ripple\App\Http\Server;
namespace Ripple\Http\Server;

use Ripple\Socket\SocketStream;

Expand Down
4 changes: 2 additions & 2 deletions src/Server/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Server/Upload/MultipartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit d8ba194

Please sign in to comment.