Php 自定义流包装器的流上下文

Php 自定义流包装器的流上下文,php,stream,Php,Stream,在本段后面的代码片段中,我使用test_stream对象创建了一个名为test的流包装器。我正在尝试将流上下文与它一起使用,并且有几个问题。首先,代码如下: <?php class Test_Stream { public $context; public function __construct() { print_r(stream_context_get_options($this->context)); exit;

在本段后面的代码片段中,我使用test_stream对象创建了一个名为test的流包装器。我正在尝试将流上下文与它一起使用,并且有几个问题。首先,代码如下:

<?php
class Test_Stream {
    public $context;

    public function __construct()
    {
        print_r(stream_context_get_options($this->context));
        exit;
    }
}

$context = array(
    'test' => array('key' => 'value'),
    'otherwrapper' => array('key' =>'value')
);
$context = stream_context_create($context);

stream_wrapper_register('test', 'Test_Stream');

$fp = fopen('test://www.domain.tld/whatever', 'r', false, $context);

您知道在open上调用哪个协议,因此,在那里使用您的上下文:

<?php
class Test_Stream {
    public $context;

    public function stream_open($path, $mode, $options, &$opened_path ){
        var_dump(parse_url($path, PHP_URL_SCHEME));
        exit;
    }
}

$context = array(
    'test' => array('key' => 'value'),
    'otherwrapper' => array('key' =>'value')
);
$context = stream_context_create($context);
stream_wrapper_register('test', 'Test_Stream');
$fp = fopen('test://www.domain.tld/whatever', 'r', false, $context);


string(4) "test"