xref: /PHP-8.4/ext/standard/tests/filters/read.phpt (revision 48981f4f)
1--TEST--
2stream filter - reading
3--FILE--
4<?php
5echo "-TEST\n";
6class strtoupper_filter extends php_user_filter {
7    function filter($in, $out, &$consumed, $closing): int
8    {
9        $output = 0;
10        while ($bucket = stream_bucket_make_writeable($in)) {
11            $bucket->data = strtoupper($bucket->data);
12            $consumed += $bucket->datalen;
13            stream_bucket_append($out, $bucket);
14            $output = 1;
15        }
16        if ($closing) {
17            $bucket = stream_bucket_new($this->stream, "\n===close===\n");
18            stream_bucket_append($out, $bucket);
19            $output = 1;
20        }
21        return $output ? PSFS_PASS_ON : PSFS_FEED_ME;
22    }
23}
24stream_filter_register("strtoupper", "strtoupper_filter")
25   or die("Failed to register filter");
26
27if ($f = fopen(__FILE__, "rb")) {
28    stream_filter_append($f, "strtoupper");
29    while (!feof($f)) {
30        echo fread($f, 8192);
31    }
32    fclose($f);
33}
34echo "Done\n";
35?>
36--EXPECTF--
37%sTEST
38<?PHP
39ECHO "-TEST\N";
40CLASS STRTOUPPER_FILTER EXTENDS PHP_USER_FILTER {
41    FUNCTION FILTER($IN, $OUT, &$CONSUMED, $CLOSING): INT
42    {
43        $OUTPUT = 0;
44        WHILE ($BUCKET = STREAM_BUCKET_MAKE_WRITEABLE($IN)) {
45            $BUCKET->DATA = STRTOUPPER($BUCKET->DATA);
46            $CONSUMED += $BUCKET->DATALEN;
47            STREAM_BUCKET_APPEND($OUT, $BUCKET);
48            $OUTPUT = 1;
49        }
50        IF ($CLOSING) {
51            $BUCKET = STREAM_BUCKET_NEW($THIS->STREAM, "\N===CLOSE===\N");
52            STREAM_BUCKET_APPEND($OUT, $BUCKET);
53            $OUTPUT = 1;
54        }
55        RETURN $OUTPUT ? PSFS_PASS_ON : PSFS_FEED_ME;
56    }
57}
58STREAM_FILTER_REGISTER("STRTOUPPER", "STRTOUPPER_FILTER")
59   OR DIE("FAILED TO REGISTER FILTER");
60
61IF ($F = FOPEN(__FILE__, "RB")) {
62    STREAM_FILTER_APPEND($F, "STRTOUPPER");
63    WHILE (!FEOF($F)) {
64        ECHO FREAD($F, 8192);
65    }
66    FCLOSE($F);
67}
68ECHO "DONE\N";
69?>
70
71===close===
72Done
73