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
63    private $stream = false;
64}
65
66stream_wrapper_register('wrapper', StreamWrapper::class);
67
68/**
69 * Next, we include a PHP file that contains executable lines, via the stream
70 * wrapper.
71 */
72$filename = __DIR__ . '/phpdbg_get_executable_stream_wrapper.inc';
73require 'wrapper://' . $filename;
74
75/**
76 * If we call phpdbg_get_executable() and pass no options, the realpath of the
77 * included file is present in the array, but indicates no executable lines.
78 */
79$x = phpdbg_get_executable();
80
81// We expect [5 => 0], but got an empty array ...
82var_dump($x[$filename]);
83
84?>
85