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 function stream_open(
29        string $path,
30        string $mode,
31        int $options = 0,
32        string &$openedPath = null
33    ) : bool {
34        if ($mode[0] !== 'r') {
35            return false;
36        }
37
38        list($scheme, $path) = explode('://', $path, 2);
39
40        $stream = \fopen($path, $mode);
41
42        if ($stream === false) {
43            return false;
44        }
45
46        $this->stream = $stream;
47
48        /**
49         * The $openedPath reference variable is assigned, indicating the
50         * *actual* path that was opened. This affects the behaviour of
51         * constants like __FILE__.
52         */
53        $openedPath = \realpath($path);
54
55        return true;
56    }
57
58    public function stream_read(int $count) : string { return \fread($this->stream, $count); }
59    public function stream_close() : bool { return \fclose($this->stream); }
60    public function stream_eof() : bool { return \feof($this->stream); }
61    public function stream_stat() { return \fstat($this->stream); }
62    public function stream_set_option($option, $arg1, $arg2) { return false; }
63
64    private $stream = false;
65}
66
67stream_wrapper_register('wrapper', StreamWrapper::class);
68
69/**
70 * Next, we include a PHP file that contains executable lines, via the stream
71 * wrapper.
72 */
73$filename = __DIR__ . DIRECTORY_SEPARATOR . 'phpdbg_get_executable_stream_wrapper.inc';
74require 'wrapper://' . $filename;
75
76/**
77 * If we call phpdbg_get_executable() and pass no options, the realpath of the
78 * included file is present in the array, but indicates no executable lines.
79 */
80$x = phpdbg_get_executable();
81
82// We expect [5 => 0], but got an empty array ...
83var_dump($x[$filename]);
84
85?>
86