Skip to content

Commit

Permalink
Building stream context in a distinct method.
Browse files Browse the repository at this point in the history
Using root namespace for global functions.
  • Loading branch information
brentscheffler committed Jun 19, 2019
1 parent 90bd79b commit 6674e01
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/Handler/StreamContextHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class StreamContextHandler extends HandlerAbstract
*/
public function __construct(array $options = [])
{
$this->options = array_merge($this->options, $options);
$this->options = \array_merge($this->options, $options);
}

/**
Expand All @@ -54,25 +54,32 @@ public function setDebug(bool $debug): HandlerAbstract
*/
public function execute(RequestInterface $request): Response
{
// Set the context options
$contextOptions = array_merge($this->options, [
$stream = $this->buildStream($request, ['http' => $this->buildHttpContext($request)]);

return $this->createResponse($stream);
}

/**
* Build the HTTP context options.
*
* @param RequestInterface $request
* @return array<string, string>
*/
private function buildHttpContext(RequestInterface $request): array
{
return \array_merge($this->options, [
'protocol_version' => $request->getProtocolVersion(),
'method' => $request->getMethod(),
'header' => $this->buildRequestHeaders($request->getHeaders()),
'content' => $request->getBody() ? $request->getBody()->getContents() : null,
'content' => $request->getBody() ? $request->getBody()->getContents() : null
]);

// Build the HTTP stream
$stream = $this->buildStream($request, ['http' => $contextOptions]);

return $this->createResponse($stream);
}

/**
* Build the request headers.
*
* @param array $requestHeaders
* @return array
* @return array<string>
*/
private function buildRequestHeaders(array $requestHeaders): array
{
Expand All @@ -95,19 +102,19 @@ private function buildRequestHeaders(array $requestHeaders): array
* @throws RequestException
* @return StreamInterface
*/
public function buildStream(RequestInterface $request, array $contextOptions)
private function buildStream(RequestInterface $request, array $contextOptions)
{
if( $this->debug ){
$params = [
"notification" => [$this, "debug"]
];
}

$context = stream_context_create($contextOptions, $params ?? []);
$context = \stream_context_create($contextOptions, $params ?? []);

if( ($stream = @fopen((string) $request->getUri(), 'r', false, $context)) === false ){
if( ($stream = @\fopen((string) $request->getUri(), 'r', false, $context)) === false ){

$error = error_get_last();
$error = \error_get_last();

throw new RequestException($request, $error["message"] ?? "Failed to open stream", $error["code"] ?? -1);

Expand All @@ -128,16 +135,16 @@ private function createResponse(StreamInterface $stream): Response
$response = $response->withBody($stream);

// Grab the headers from the Stream meta data
$headers = $response->getBody()->getMetadata('wrapper_data') ?? [];
$headers = $stream->getMetadata('wrapper_data') ?? [];

// Process the headers
foreach( $headers as $header ){
if( preg_match("/^HTTP\/([\d\.]+) ([\d]{3})(?: ([\w\h]+))?\R?+$/i", trim($header), $httpResponse) ){
if( \preg_match("/^HTTP\/([\d\.]+) ([\d]{3})(?: ([\w\h]+))?\R?+$/i", \trim($header), $httpResponse) ){
$response = $response->withStatus((int) $httpResponse[2], $httpResponse[3] ?? "");
$response = $response->withProtocolVersion($httpResponse[1]);
}

elseif( preg_match("/^([\w\-]+)\: (\N+)\R?+$/", trim($header), $httpHeader) ){
elseif( \preg_match("/^([\w\-]+)\: (\N+)\R?+$/", \trim($header), $httpHeader) ){
$response = $response->withAddedHeader($httpHeader[1], $httpHeader[2]);
}
}
Expand All @@ -156,7 +163,7 @@ private function createResponse(StreamInterface $stream): Response
* @param int $bytes_max
* @return void
*/
private function debug($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max): void
private function debug(int $notification_code, int $severity, string $message, int $message_code, int $bytes_transferred, int $bytes_max): void
{
switch( $notification_code ){

Expand Down Expand Up @@ -205,7 +212,7 @@ private function debug($notification_code, $severity, $message, $message_code, $
$debug = "Foo";
}

$preamble = json_encode([
$preamble = \json_encode([
"noitification_code" => $notification_code,
"severity" => $severity,
"message" => $message,
Expand Down

0 comments on commit 6674e01

Please sign in to comment.