xref: /PHP-8.4/sapi/cli/tests/php_cli_server.inc (revision 5b501f28)
1<?php
2
3// TODO: Move address/port info in here?
4class CliServerInfo {
5    public function __construct(
6        public string $docRoot,
7        public $processHandle,
8    ) {}
9}
10
11function php_cli_server_start(
12    ?string $code = 'echo "Hello world";',
13    ?string $router = 'index.php',
14    array $cmd_args = []
15): CliServerInfo {
16    $php_executable = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
17    $error = null;
18
19    // Create dedicated doc root to avoid index.php clashes between tests.
20    $doc_root = __DIR__ . DIRECTORY_SEPARATOR . basename($_SERVER['PHP_SELF'], '.php');
21    @mkdir($doc_root);
22
23    if ($code) {
24        file_put_contents($doc_root . '/' . ($router ?: 'index.php'), '<?php ' . $code . ' ?>');
25    }
26
27    $cmd = [$php_executable, '-t', $doc_root, '-n', ...$cmd_args, '-S', 'localhost:0'];
28    if (!is_null($router)) {
29        $cmd[] = $router;
30    }
31
32    $output_file = tempnam(sys_get_temp_dir(), 'cli_server_output');
33    $output_file_fd = fopen($output_file, 'ab');
34    if ($output_file_fd === false) {
35        die(sprintf("Failed opening output file %s\n", $output_file));
36    }
37    register_shutdown_function(function () use ($output_file) {
38        unlink($output_file);
39    });
40
41    $descriptorspec = array(
42        0 => STDIN,
43        1 => $output_file_fd,
44        2 => $output_file_fd,
45    );
46    $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
47
48    // First, wait for the dev server to declare itself ready.
49    $bound = null;
50    for ($i = 0; $i < 60; $i++) {
51        usleep(50000); // 50ms per try
52        $status = proc_get_status($handle);
53        if (empty($status['running'])) {
54            echo "Server failed to start\n";
55            printf("Server output:\n%s\n", file_get_contents($output_file));
56            proc_terminate($handle);
57            exit(1);
58        }
59
60        $output = file_get_contents($output_file);
61        if (preg_match('@PHP \S* Development Server \(https?://(.*?:\d+)\) started@', $output, $matches)) {
62            $bound = $matches[1];
63            break;
64        }
65    }
66    if ($bound === null) {
67        echo "Server did not output startup message";
68        printf("Server output:\n%s\n", file_get_contents($output_file));
69        proc_terminate($handle);
70        exit(1);
71    }
72
73    // Now wait for a connection to succeed.
74    // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
75    //       it might not be listening yet...need to wait until fsockopen() call returns
76    $error = "Unable to connect to server\n";
77    for ($i=0; $i < 60; $i++) {
78        usleep(50000); // 50ms per try
79        $status = proc_get_status($handle);
80        $fp = @fsockopen("tcp://$bound");
81        // Failure, the server is no longer running
82        if (!($status && $status['running'])) {
83            $error = sprintf("Server stopped\nServer output:\n%s\n", file_get_contents($output_file));
84            break;
85        }
86        // Success, Connected to servers
87        if ($fp) {
88            $error = '';
89            break;
90        }
91    }
92
93    if ($error) {
94        echo $error;
95        proc_terminate($handle);
96        exit(1);
97    }
98
99    register_shutdown_function(
100        function($handle) use($router, $doc_root, $output_file) {
101            proc_terminate($handle);
102            $status = proc_get_status($handle);
103            if ($status['exitcode'] !== -1 && $status['exitcode'] !== 0
104                    && !($status['exitcode'] === 255 && PHP_OS_FAMILY == 'Windows')) {
105                printf("Server exited with non-zero status: %d\n", $status['exitcode']);
106                printf("Server output:\n%s\n", file_get_contents($output_file));
107            }
108            @unlink(__DIR__ . "/{$router}");
109            remove_directory($doc_root);
110        },
111        $handle
112    );
113
114    // Define the same "constants" we previously did.
115    $port = (int) substr($bound, strrpos($bound, ':') + 1);
116    define("PHP_CLI_SERVER_HOSTNAME", "localhost");
117    define("PHP_CLI_SERVER_PORT", $port);
118    define("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
119
120    return new CliServerInfo($doc_root, $handle);
121}
122
123function php_cli_server_connect() {
124    $timeout = 1.0;
125    $fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errstr, $timeout);
126    if (!$fp) {
127        die("connect failed");
128    }
129    return $fp;
130}
131
132function remove_directory($dir) {
133    if (is_dir($dir) === false) {
134        return;
135    }
136    $files = new RecursiveIteratorIterator(
137        new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
138        RecursiveIteratorIterator::CHILD_FIRST
139    );
140    foreach ($files as $fileinfo) {
141        $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
142        $todo($fileinfo->getRealPath());
143    }
144    @rmdir($dir);
145}
146
147?>
148