On Tuesday, 4 September 2018 15:27:59 UTC+2, Woody Gilk wrote:
>
> Thanks for bringing this up, Martijn. I agree that bit is rather 
> awkward and should be clarified and tested better. 
>
> I think the docblock was intended to read: 
>
> > The resource MUST be readable and may be writable. 
>
> However, even if that was the case, how would it be enforced? I don't 
> know of a way to check if a resource is readable. 
>

If the word “stream” in the docblock referred to a stream-type resource 
(cf. resource types <https://secure.php.net/manual/en/resource.php>) you 
should be able to use stream_get_meta_data() 
<https://secure.php.net/manual/en/function.stream-get-meta-data.php> and 
check whether the mode allows reading. The fact that stream may have two 
different meanings there is confusing in and of itself. Though even then 
figuring out exactly when to error out is frustrating:

if (false === is_resource($resource) ||
    'stream' !== get_resource_type($resource) ||
    true === array_key_exists('stream_type', $metadata = 
stream_get_meta_data($resource)) && $metadata['stream_type'] === 'dir' ||
    false === array_key_exists('mode', $metadata) ||
    in_array($metadata['mode'], ['r', 'r+', 'w+', 'a+', 'x+', 'c+'])
) {
    throw new \InvalidArgumentException('resource must be readable and of 
type stream.');
}

(N.b. the in_array() 
<https://secure.php.net/manual/en/function.in-array.php> check displayed 
there isn’t completely accurate. E.g. mode may contain t or b flags. Doing 
a proper mode check is left as an exercise to the reader ;-) )

This may require an errata to PSR-17. 
>

I agree. If only to standardise on an exception (RuntimeException? 
InvalidArgumentException?) to throw when the implementation cannot use the 
provided resource.

But preferably the errata can also clarify what sort of resources are 
expected to be handled for interop reasons. As a consumer of the interface 
I would like to know that I can always feed it a resource from fopen(). 
Whether an implementation also supports zlib type resources (from gzopen() 
<https://secure.php.net/manual/en/function.gzopen.php>) is up to them and 
not guaranteed by the interface. Or is setting a minimum implementation 
requirement outside of the scope for the PSR?

I would love to hear if there is anything I can do to help this take shape. 
I am willing to put some time into getting this clarified for myself and 
everyone else.

Regards,

Martijn van der Ven
https://vanderven.se/martijn/

-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/489d4691-34d2-436a-9030-bcecea56be6f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to