Skip to content

Commit 36fac51

Browse files
committed
Implement read & write filter list for stream_get_meta_data()
Implements a 22 year old TODO item.
1 parent 4c9bbaa commit 36fac51

4 files changed

Lines changed: 70 additions & 14 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ PHP NEWS
8484
. Fixed bug GH-20370 (User stream filters could violate typed property
8585
constraints). (alexandre-daubois)
8686
. Allowed filtered streams to be casted as fd for select. (Jakub Zelenka)
87+
. Added "read_filters" and "write_filters" keys to stream_get_meta_data().
88+
(ndossche)
8789

8890
- Zip:
8991
. Fixed ZipArchive callback being called after executor has shut down.

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ PHP 8.6 UPGRADE NOTES
5353
tcp_keepintvl and tcp_keepcnt that allow setting socket keepalive
5454
options.
5555
. Allowed casting casting filtered streams as file descriptor for select.
56+
. Added "read_filters" and "write_filters" keys to stream_get_meta_data()
57+
if the stream has read filters/write filters attached.
5658

5759
========================================
5860
3. Changes in SAPI modules

ext/standard/streamsfuncs.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,20 @@ PHP_FUNCTION(stream_copy_to_stream)
507507
}
508508
/* }}} */
509509

510+
static void stream_fill_filters_array(zval *array, const char *name, const php_stream_filter_chain *chain)
511+
{
512+
if (chain->head) {
513+
zval filters;
514+
array_init(&filters);
515+
516+
for (const php_stream_filter *filter = chain->head; filter != NULL; filter = filter->next) {
517+
add_next_index_string(&filters, filter->fops->label);
518+
}
519+
520+
add_assoc_zval(array, name, &filters);
521+
}
522+
}
523+
510524
/* {{{ Retrieves header/meta data from streams/file pointers */
511525
PHP_FUNCTION(stream_get_meta_data)
512526
{
@@ -535,20 +549,8 @@ PHP_FUNCTION(stream_get_meta_data)
535549

536550
add_assoc_string(return_value, "mode", stream->mode);
537551

538-
#if 0 /* TODO: needs updating for new filter API */
539-
if (stream->filterhead) {
540-
php_stream_filter *filter;
541-
542-
MAKE_STD_ZVAL(newval);
543-
array_init(newval);
544-
545-
for (filter = stream->filterhead; filter != NULL; filter = filter->next) {
546-
add_next_index_string(newval, filter->fops->label);
547-
}
548-
549-
add_assoc_zval(return_value, "filters", newval);
550-
}
551-
#endif
552+
stream_fill_filters_array(return_value, "read_filters", &stream->readfilters);
553+
stream_fill_filters_array(return_value, "write_filters", &stream->writefilters);
552554

553555
add_assoc_long(return_value, "unread_bytes", stream->writepos - stream->readpos);
554556

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
stream_get_meta_data() filters list
3+
--FILE--
4+
<?php
5+
6+
$fp = fopen(__FILE__, "r");
7+
stream_filter_append($fp, 'string.rot13', STREAM_FILTER_READ);
8+
stream_filter_append($fp, 'string.toupper', STREAM_FILTER_WRITE);
9+
stream_filter_append($fp, 'string.tolower', STREAM_FILTER_ALL);
10+
11+
var_dump(stream_get_meta_data($fp));
12+
13+
fclose($fp);
14+
15+
?>
16+
--EXPECTF--
17+
array(11) {
18+
["timed_out"]=>
19+
bool(false)
20+
["blocked"]=>
21+
bool(true)
22+
["eof"]=>
23+
bool(false)
24+
["wrapper_type"]=>
25+
string(9) "plainfile"
26+
["stream_type"]=>
27+
string(5) "STDIO"
28+
["mode"]=>
29+
string(1) "r"
30+
["read_filters"]=>
31+
array(2) {
32+
[0]=>
33+
string(12) "string.rot13"
34+
[1]=>
35+
string(14) "string.tolower"
36+
}
37+
["write_filters"]=>
38+
array(2) {
39+
[0]=>
40+
string(14) "string.toupper"
41+
[1]=>
42+
string(14) "string.tolower"
43+
}
44+
["unread_bytes"]=>
45+
int(0)
46+
["seekable"]=>
47+
bool(true)
48+
["uri"]=>
49+
string(%d) "%s"
50+
}

0 commit comments

Comments
 (0)