7

我正在编写一个自定义流包装器,以用作使用内置http://流包装器的 HTTP 客户端类的单元测试中的存根。

具体来说,我需要'wrapper_data'通过调用stream_get_meta_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;
    }
}
4

1 回答 1

15

stream_get_meta_dataext/standard/streamfunc.c中实现。相关部分是

if (stream->wrapperdata) {
    MAKE_STD_ZVAL(newval);
    MAKE_COPY_ZVAL(&stream->wrapperdata, newval);

    add_assoc_zval(return_value, "wrapper_data", newval);
}

即无论zval stream->wrapperdata 持有什么,都被$retval["wrapper_data"]“复制”到/引用。
您的自定义包装代码由main/streams/userspace.c “处理” user_wrapper_opener。你有

/* set wrapper data to be a reference to our object */
stream->wrapperdata = us->object;

us->object“是”为流实例化的自定义包装器的实例。除此之外,我还没有找到影响stream->wrapperdata用户空间脚本的方法。但是,如果您只需要and ,
您可以实现Iterator / IteratorAggregate和/或ArrayAccess。 例如foreach($metadata['wrapper_data'] ...)$metadata['wrapper_data'][$i]

<?php
function test() {
    stream_wrapper_register("mock", "CustomHttpStreamWrapper") or die("Failed to register protocol");
    $fp = fopen("mock://myvar", "r+");
    $md = stream_get_meta_data($fp);

    echo "Iterator / IteratorAggregate\n";
    foreach($md['wrapper_data'] as $e) {
        echo $e, "\n";
    }

    echo "\nArrayAccess\n";
    echo $md['wrapper_data'][0], "\n";

    echo "\nvar_dump\n";
    echo var_dump($md['wrapper_data']);
}

class CustomHttpStreamWrapper implements IteratorAggregate, ArrayAccess  {
    public $context;
    public $position = 0;
    public $bodyData = 'test body data';

    protected $foo = array('HTTP/1.1 200 OK', 'Content-Length: 438', 'foo: bar', 'ham: eggs');
    /* IteratorAggregate */
    public function getIterator() {
        return new ArrayIterator($this->foo);
    }
    /* ArrayAccess */
    public function offsetExists($offset) { return array_key_exists($offset, $this->foo); }
    public function offsetGet($offset ) { return $this->foo[$offset]; }
    public function offsetSet($offset, $value) { $this->foo[$offset] = $value; }
    public function offsetUnset($offset) { unset($this->foo[$offset]); }

    /* StreamWrapper */
    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;
    }
}

test();

印刷

Iterator / IteratorAggregate
HTTP/1.1 200 OK
Content-Length: 438
foo: bar
ham: eggs

ArrayAccess
HTTP/1.1 200 OK

var_dump
object(CustomHttpStreamWrapper)#1 (4) {
  ["context"]=>
  resource(5) of type (stream-context)
  ["position"]=>
  int(0)
  ["bodyData"]=>
  string(14) "test body data"
  ["foo":protected]=>
  array(4) {
    [0]=>
    string(15) "HTTP/1.1 200 OK"
    [1]=>
    string(19) "Content-Length: 438"
    [2]=>
    string(8) "foo: bar"
    [3]=>
    string(9) "ham: eggs"
  }
}
于 2012-07-31T08:15:21.950 回答