1--TEST--
2Getting executable lines from custom wrappers
3--PHPDBG--
4r
5q
6--EXPECTF--
7[Successful compilation of %s]
8prompt> array(1) {
9  [5]=>
10  int(0)
11}
12[Script ended normally]
13prompt>
14--FILE--
15<?php
16
17/**
18 * This example demonstrates how phpdbg_get_executable() behaves differently
19 * when passed the 'files' option vs without, in the face of some mild abuse
20 * of stream wrappers.
21 */
22
23/**
24 * First, we define a stream wrapper that simply maps to a real file on disk.
25 */
26final class StreamWrapper
27{
28    public $context;
29    public function stream_open(
30        string $path,
31        string $mode,
32        int $options = 0,
33        ?string &$openedPath = null
34    ) : bool {
35        if ($mode[0] !== 'r') {
36            return false;
37        }
38
39        list($scheme, $path) = explode('://', $path, 2);
40
41        $stream = \fopen($path, $mode);
42
43        if ($stream === false) {
44            return false;
45        }
46
47        $this->stream = $stream;
48
49        /**
50         * The $openedPath reference variable is assigned, indicating the
51         * *actual* path that was opened. This affects the behaviour of
52         * constants like __FILE__.
53         */
54        $openedPath = \realpath($path);
55
56        return true;
57    }
58
59    public function stream_read(int $count) : string { return \fread($this->stream, $count); }
60    public function stream_close() : bool { return \fclose($this->stream); }
61    public function stream_eof() : bool { return \feof($this->stream); }
62    public function stream_stat() { return \fstat($this->stream); }
63    public function stream_set_option($option, $arg1, $arg2) { return false; }
64
65    private $stream = false;
66}
67
68stream_wrapper_register('wrapper', StreamWrapper::class);
69
70/**
71 * Next, we include a PHP file that contains executable lines, via the stream
72 * wrapper.
73 */
74$filename = __DIR__ . DIRECTORY_SEPARATOR . 'phpdbg_get_executable_stream_wrapper.inc';
75require 'wrapper://' . $filename;
76
77/**
78 * If we call phpdbg_get_executable() and pass no options, the realpath of the
79 * included file is present in the array, but indicates no executable lines.
80 */
81$x = phpdbg_get_executable();
82
83// We expect [5 => 0], but got an empty array ...
84var_dump($x[$filename]);
85
86?>
87