【发布时间】:2012-07-28 21:27:07
【问题描述】:
我正在编写一个自定义流包装器,用作一个使用内置 http:// 流包装器的 HTTP 客户端类的单元测试中的存根。
具体来说,我需要通过在自定义流包装器创建的流上调用stream_get_meta_data 来控制'wrapper_data' 键中返回的值。不幸的是,关于自定义流包装器的文档很糟糕,而且 API 看起来不直观。
自定义包装器中的什么方法控制元wrapper_data 响应?
使用底部的类,当我 var_dump(stream_get_meta_data($stream)); 使用自定义包装器创建的流时,我只能得到以下结果...
array(10) {
'wrapper_data' =>
class CustomHttpStreamWrapper#5 (3) {
public $context =>
resource(13) of type (stream-context)
public $position =>
int(0)
public $bodyData =>
string(14) "test body data"
}
...
但我需要诱使包装器在元数据检索时产生类似以下内容的内容,以便我可以测试客户端类对真实 http:// 流包装器返回的数据的解析...
array(10) {
'wrapper_data' => Array(
[0] => HTTP/1.1 200 OK
[1] => Content-Length: 438
)
...
这是我目前用于自定义包装器的代码:
class CustomHttpStreamWrapper {
public $context;
public $position = 0;
public $bodyData = 'test body data';
public function stream_open($path, $mode, $options, &$opened_path) {
return true;
}
public function stream_read($count) {
$this->position += strlen($this->bodyData);
if ($this->position > strlen($this->bodyData)) {
return false;
}
return $this->bodyData;
}
public function stream_eof() {
return $this->position >= strlen($this->bodyData);
}
public function stream_stat() {
return array('wrapper_data' => array('test'));
}
public function stream_tell() {
return $this->position;
}
}
【问题讨论】:
-
streamWrapper::stream_metadata怎么样?可能会有所帮助,尽管docs 似乎另有说法。 -
我尊重你的好意!
标签: php stream stream-wrapper