Skip to content

Commit

Permalink
Bumping to minimum PHP version 8.2.
Browse files Browse the repository at this point in the history
Using PSR-17 message factories in place of hardcoding Capsule PSR-7 dependencies.
Defaulting to Capsule for PSR-17 dependencies.
  • Loading branch information
brentscheffler committed May 12, 2024
1 parent d69df44 commit 2e89653
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 61 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.2",
"ext-json": "*",
"psr/http-client": "^1.0",
"nimbly/capsule": "^2.0"
Expand Down
23 changes: 8 additions & 15 deletions src/Body/JsonBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nimbly\Shuttle\Body;

use JsonSerializable;
use Nimbly\Shuttle\EncodingException;

/**
Expand All @@ -12,26 +13,18 @@
class JsonBody extends BufferBody
{
/**
* @inheritDoc
* @param JsonSerializable|array $data
* @param string $content_type Defaults to "application/json"
*/
protected string $content_type = "application/json";

/**
* @param array $data
* @param string|null $content_type
*/
public function __construct(array $data, ?string $content_type = null)
public function __construct(JsonSerializable|array $data, string $content_type = "application/json")
{
$json = \json_encode($data, JSON_UNESCAPED_SLASHES);
$buffer = \json_encode($data, JSON_UNESCAPED_SLASHES);

if( $json === false ){
if( $buffer === false ){
throw new EncodingException("Failed to encode body as JSON.");
}

$this->buffer = $json;

if( $content_type ){
$this->content_type = $content_type;
}
$this->buffer = $buffer;
$this->content_type = $content_type;
}
}
2 changes: 1 addition & 1 deletion src/Body/MultipartFormBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MultipartFormBody extends BufferStream implements BodyInterface
public function __construct(array $parts)
{
// Create a random boundary name for each multipart request.
$this->boundary = \uniqid("Capsule") . "Z";
$this->boundary = \uniqid("Shuttle") . "Z";

/**
* @var string $name
Expand Down
19 changes: 17 additions & 2 deletions src/Handler/StreamContextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StreamContextHandler implements HandlerInterface
use RawResponseTrait;

/**
* Default stream handler options.
* Default stream handler HTTP options.
*
* @var array{follow_location:int,ignore_errors:bool,request_fulluri:bool,max_redirects:int,timeout:int}
*/
Expand All @@ -25,16 +25,25 @@ class StreamContextHandler implements HandlerInterface
"timeout" => 120,
];

/**
* Default stream handler SSL options.
*
* @var array
*/
protected array $ssl_options = [];

/**
* @param array<string,mixed> $options Array of HTTP stream context key => value pairs. See http://php.net/manual/en/context.http.php for list of available options.
*/
public function __construct(
array $options = [])
array $options = [],
array $ssl_options = [])
{
/**
* @psalm-suppress PropertyTypeCoercion
*/
$this->options = \array_merge($this->options, $options);
$this->ssl_options = \array_merge($this->ssl_options, $ssl_options);
}

/**
Expand All @@ -53,6 +62,12 @@ public function execute(RequestInterface $request, ResponseInterface $response):
"content" => $request->getBody()->getContents(),
],
$this->options
),
"ssl" => \array_merge(
$this->ssl_options,
[
"peer_name" => $request->getUri()->getHost()
]
)
]
);
Expand Down
7 changes: 7 additions & 0 deletions src/RawResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

trait RawResponseTrait
{
/**
* Process and parse a raw string HTTP response.
*
* @param string $raw_response
* @param ResponseInterface $response
* @return ResponseInterface
*/
protected function parseRawResponse(string $raw_response, ResponseInterface $response): ResponseInterface
{
$lines = \explode("\n", $raw_response);
Expand Down
52 changes: 11 additions & 41 deletions src/Shuttle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,33 @@

class Shuttle implements ClientInterface
{
const SHUTTLE_USER_AGENT = "Shuttle/1.0";
const SHUTTLE_USER_AGENT = "Shuttle/2.0";

protected HandlerInterface $handler;
protected RequestFactoryInterface $requestFactory;
protected ResponseFactoryInterface $responseFactory;
protected StreamFactoryInterface $streamFactory;
protected UriFactoryInterface $uriFactory;
protected Closure $middleware;

/**
* @param HandlerInterface|null $handler
* @param HandlerInterface $handler
* @param string|null $base_url
* @param array<string,string> $headers
* @param array<MiddlewareInterface> $middleware
* @param string $http_version
* @param RequestFactoryInterface|null $requestFactory
* @param ResponseFactoryInterface|null $responseFactory
* @param StreamFactoryInterface|null $streamFactory
* @param UriFactoryInterface|null $uriFactory
* @param RequestFactoryInterface $requestFactory
* @param ResponseFactoryInterface $responseFactory
* @param StreamFactoryInterface $streamFactory
* @param UriFactoryInterface $uriFactory
*/
public function __construct(
?HandlerInterface $handler = null,
protected HandlerInterface $handler = new CurlHandler,
protected ?string $base_url = null,
protected array $headers = [],
array $middleware = [],
protected string $http_version = "1.1",
?RequestFactoryInterface $requestFactory = null,
?ResponseFactoryInterface $responseFactory = null,
?StreamFactoryInterface $streamFactory = null,
?UriFactoryInterface $uriFactory = null,
protected RequestFactoryInterface $requestFactory = new RequestFactory,
protected ResponseFactoryInterface $responseFactory = new ResponseFactory,
protected StreamFactoryInterface $streamFactory = new StreamFactory,
protected UriFactoryInterface $uriFactory = new UriFactory,
)
{
if( empty($handler) ){
$handler = new CurlHandler;
}

if( empty($requestFactory) ){
$requestFactory = new RequestFactory;
}

if( empty($responseFactory) ){
$responseFactory = new ResponseFactory;
}

if( empty($streamFactory) ){
$streamFactory = new StreamFactory;
}

if( empty($uriFactory) ){
$uriFactory = new UriFactory;
}

$this->handler = $handler;
$this->requestFactory = $requestFactory;
$this->responseFactory = $responseFactory;
$this->uriFactory = $uriFactory;
$this->streamFactory = $streamFactory;
$this->middleware = $this->compileMiddleware(
$middleware,
function(RequestInterface $request): ResponseInterface {
Expand Down
5 changes: 4 additions & 1 deletion tests/ShuttleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class ShuttleTest extends TestCase
{
public function test_default_user_agent_prefix(): void
{
$this->assertEquals("Shuttle/1.0", Shuttle::SHUTTLE_USER_AGENT);
$this->assertEquals(
"Shuttle/2.0",
Shuttle::SHUTTLE_USER_AGENT
);
}

public function test_shuttle_creates_default_handler(): void
Expand Down

0 comments on commit 2e89653

Please sign in to comment.