Skip to content

Commit cd0f032

Browse files
committed
added stream transport implementation
1 parent 8718fc3 commit cd0f032

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

src/stream/Request.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace hiqdev\hiart\stream;
1212

1313
use hiqdev\hiart\AbstractRequest;
14+
use yii\helpers\Inflector;
1415

1516
/**
1617
* PHP stream request implementation.
@@ -21,7 +22,70 @@ class Request extends AbstractRequest
2122
{
2223
protected $workerClass = RequestWorker::class;
2324

24-
protected function createWorker()
25+
public $defaultOptions = [
26+
'http' => [
27+
'ignore_errors' => true,
28+
],
29+
'ssl' => [
30+
'verify_peer' => false,
31+
],
32+
];
33+
34+
public function send($options = [])
2535
{
36+
$this->build();
37+
38+
try {
39+
$context = stream_context_create($this->prepareContextOptions($options));
40+
$stream = fopen($this->getFullUri(), 'rb', false, $context);
41+
$responseContent = stream_get_contents($stream);
42+
// see http://php.net/manual/en/reserved.variables.httpresponseheader.php
43+
$responseHeaders = $http_response_header;
44+
fclose($stream);
45+
} catch (\Exception $e) {
46+
throw new Exception($e->getMessage(), $e->getCode(), $e);
47+
}
48+
49+
return new $this->responseClass($this, $responseContent, $responseHeaders);
50+
}
51+
52+
protected function prepareContextOptions($options)
53+
{
54+
$requestOptions = [
55+
'http' => [
56+
'method' => $this->method,
57+
'header' => $this->headers,
58+
],
59+
];
60+
61+
if (isset($this->body)) {
62+
$requestOptions['http']['content'] = $this->body;
63+
}
64+
65+
$dbOptions = $this->convertContextOptions($this->getDb()->requestOptions);
66+
$sendOptions = $this->convertContextOptions($options);
67+
68+
return ArrayHelper::merge($this->defaultOptions, $dbOptions, $requestOptions, $sendOptions);
69+
}
70+
71+
/**
72+
* Converts raw options to stream context options.
73+
* @param array $options raw options.
74+
* @return array stream context options.
75+
*/
76+
protected function convertContextOptions(array $options)
77+
{
78+
$contextOptions = [];
79+
foreach ($options as $key => $value) {
80+
$section = 'http';
81+
if (strpos($key, 'ssl') === 0) {
82+
$section = 'ssl';
83+
$key = substr($key, 3);
84+
}
85+
$key = Inflector::underscore($key);
86+
$contextOptions[$section][$key] = $value;
87+
}
88+
89+
return $contextOptions;
2690
}
2791
}

src/stream/Response.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,24 @@
1919
*/
2020
class Response extends AbstractResponse
2121
{
22-
/**
23-
* @var ResponseWorker
24-
*/
25-
protected $worker;
22+
protected $rawData;
23+
24+
protected $headers;
25+
26+
public function __construct(Request $request, $rawData, array $headers)
27+
{
28+
$this->request = $request;
29+
$this->rawData = $rawData;
30+
$this->headers = $headers;
31+
}
32+
33+
public function getRawData()
34+
{
35+
return $this->rawData;
36+
}
37+
38+
public function getHeader($name)
39+
{
40+
return isset($this->headers[$name]) ? $this->headers[$name] : null;
41+
}
2642
}

0 commit comments

Comments
 (0)