xref: /PHP-8.2/sapi/cli/tests/php_cli_server.inc (revision d7d0d19d)
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
38    $descriptorspec = array(
39        0 => STDIN,
40        1 => $output_file_fd,
41        2 => $output_file_fd,
42    );
43    $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
44
45    // First, wait for the dev server to declare itself ready.
46    $bound = null;
47    for ($i = 0; $i < 60; $i++) {
48        usleep(50000); // 50ms per try
49        $status = proc_get_status($handle);
50        if (empty($status['running'])) {
51            echo "Server failed to start\n";
52            printf("Server output:\n%s\n", file_get_contents($output_file));
53            proc_terminate($handle);
54            exit(1);
55        }
56
57        $output = file_get_contents($output_file);
58        if (preg_match('@PHP \S* Development Server \(https?://(.*?:\d+)\) started@', $output, $matches)) {
59            $bound = $matches[1];
60            break;
61        }
62    }
63    if ($bound === null) {
64        echo "Server did not output startup message";
65        printf("Server output:\n%s\n", file_get_contents($output_file));
66        proc_terminate($handle);
67        exit(1);
68    }
69
70    // Now wait for a connection to succeed.
71    // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
72    //       it might not be listening yet...need to wait until fsockopen() call returns
73    $error = "Unable to connect to server\n";
74    for ($i=0; $i < 60; $i++) {
75        usleep(50000); // 50ms per try
76        $status = proc_get_status($handle);
77        $fp = @fsockopen("tcp://$bound");
78        // Failure, the server is no longer running
79        if (!($status && $status['running'])) {
80            $error = sprintf("Server stopped\nServer output:\n%s\n", file_get_contents($output_file));
81            break;
82        }
83        // Success, Connected to servers
84        if ($fp) {
85            $error = '';
86            break;
87        }
88    }
89
90    if ($error) {
91        echo $error;
92        proc_terminate($handle);
93        exit(1);
94    }
95
96    register_shutdown_function(
97        function($handle) use($router, $doc_root, $output_file) {
98            proc_terminate($handle);
99            $status = proc_get_status($handle);
100            if ($status['exitcode'] !== -1 && $status['exitcode'] !== 0
101                    && !($status['exitcode'] === 255 && PHP_OS_FAMILY == 'Windows')) {
102                printf("Server exited with non-zero status: %d\n", $status['exitcode']);
103                printf("Server output:\n%s\n", file_get_contents($output_file));
104            }
105            @unlink(__DIR__ . "/{$router}");
106            remove_directory($doc_root);
107        },
108        $handle
109    );
110
111    // Define the same "constants" we previously did.
112    $port = (int) substr($bound, strrpos($bound, ':') + 1);
113    define("PHP_CLI_SERVER_HOSTNAME", "localhost");
114    define("PHP_CLI_SERVER_PORT", $port);
115    define("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
116
117    return new CliServerInfo($doc_root, $handle);
118}
119
120function php_cli_server_connect() {
121    $timeout = 1.0;
122    $fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT, $errno, $errstr, $timeout);
123    if (!$fp) {
124        die("connect failed");
125    }
126    return $fp;
127}
128
129function remove_directory($dir) {
130    if (is_dir($dir) === false) {
131        return;
132    }
133    $files = new RecursiveIteratorIterator(
134        new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
135        RecursiveIteratorIterator::CHILD_FIRST
136    );
137    foreach ($files as $fileinfo) {
138        $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
139        $todo($fileinfo->getRealPath());
140    }
141    @rmdir($dir);
142}
143
144?>
145