Hi Derick,

I'm leading up to be able to do this sort of thing, but want/need to
discuss/brainstorm it here before I do much more to it.

In a recent commit I added a context parameter to fopen.  The context
is intended to hold these kind of options, plus other data that is or
will be relevant to streams and/or wrappers.  A context may be shared
by multiple streams/wrappers, so common options can apply to a group
of streams.

At the moment, there are a couple of user-space APIs for manipulating
contexts:
file_context_create([array options]) and
file_context_set_params(array options).

I think that it would generally be useful to store context data on
3 levels:

1. The very general "context" level.  Here we set things like status
notification callbacks, or other stream/wrapper tuning parameters,
like default chunk size for network streams, or whatever.

2. The "common" wrapper level.  Here we set things that are common to
a family of wrappers.  So, "net" would store settings common to various
network wrappers (a bit like .netrc), "compress" would store settings
common to various zippers, "crypt" for common crypt settings etc.

3. The specific wrapper level: "net.http" stores http specific settings,
"crypt.des" stores des specific settings etc. etc.

ATM, file_context_set_params will only operate at the "context" level.
I think introducing a new API like this:
file_context_set_option(resource $context|resource $stream,
   string $wrapperkey, string $optionname, mixed $optionvalue);
would do that job just fine.

The reason for having a separate function is that it means that we
won't risk collisions between context level setting names and
user-registered wrapper names.

So, your example below would look like this:
$fp = fopen ("crypt.des://secretfile.des", "r");
file_context_set_option ($fp, "crypt", "key", "very secret key");

Although my feeling is that setting the options before opening
might make more sense (or might be the only option, particularly
for network wrappers):

$ctx = file_context_create();
file_context_set_option ($ctx, "net", "proxy", "myproxy:8080");
$fp = fopen ("http://www.php.net";, "r", false, $ctx);

Then we will need functions to retrieve those settings, both in 
the php core and in user land (for user-space wrappers).

PHPAPI int php_stream_context_get_option(
   php_stream_context *context,
   const char *wrapperkey,
   const char *optionname,
   zval **optionvalue);

This would really just be a wrapper around the zend_hash_find
function, so I might implement it as a macro.

Extension code would use it a bit like this:

php_stream *stream; // already filled in somewhere with a valid stream

if (stream->context) {
    zval *keyval = NULL;
    if (SUCCESS == php_stream_context_get_option(stream->context,
            "crypt", "key", &keyval)) {
        // it's safe to use keyval
    }
}

Any comments?

--Wez.

On 19/04/02, "Derick Rethans" <[EMAIL PROTECTED]> wrote:
> Hey Wez,
> 
> I wonder if it is possible to set options after a stream has been opened. 
> Something like:
> 
> $fp = fopen ("crypt.des://secretfile.des", "r");
> stream_option ($fp, "key", "very secret key");
> 
> And if it's there, is there an example in the source of it?
> 
> regards,
> Derick
> 
> 
> -- 
> PHP Development Mailing List <http://www.php.net/>
> To unsubscribe, visit: http://www.php.net/unsub.php




-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to