|
| 1 | +<?php |
| 2 | + |
| 3 | +const WORKER_ARGV_VALUE = 'RUN_WORKER'; |
| 4 | + |
| 5 | +function phpt_notify($worker = null) |
| 6 | +{ |
| 7 | + ServerClientProxyTestCase::getInstance()->notify($worker); |
| 8 | +} |
| 9 | + |
| 10 | +function phpt_wait($worker = null) |
| 11 | +{ |
| 12 | + ServerClientProxyTestCase::getInstance()->wait($worker); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * This is a singleton to let the wait/notify functions work |
| 17 | + * I know it's horrible, but it's a means to an end |
| 18 | + */ |
| 19 | +class ServerClientProxyTestCase |
| 20 | +{ |
| 21 | + private $isWorker = false; |
| 22 | + |
| 23 | + private $workerHandles = []; |
| 24 | + |
| 25 | + private $workerStdIn = []; |
| 26 | + |
| 27 | + private $workerStdOut = []; |
| 28 | + |
| 29 | + private static $instance; |
| 30 | + |
| 31 | + public static function getInstance($isWorker = false) |
| 32 | + { |
| 33 | + if (!isset(self::$instance)) { |
| 34 | + self::$instance = new self($isWorker); |
| 35 | + } |
| 36 | + |
| 37 | + return self::$instance; |
| 38 | + } |
| 39 | + |
| 40 | + public function __construct($isWorker = false) |
| 41 | + { |
| 42 | + if (!isset(self::$instance)) { |
| 43 | + self::$instance = $this; |
| 44 | + } |
| 45 | + |
| 46 | + $this->isWorker = $isWorker; |
| 47 | + } |
| 48 | + |
| 49 | + private function spawnWorkerProcess($worker, $code) |
| 50 | + { |
| 51 | + if (defined("PHP_WINDOWS_VERSION_MAJOR")) { |
| 52 | + $ini = php_ini_loaded_file(); |
| 53 | + $cmd = sprintf('%s %s "%s" %s', PHP_BINARY, $ini ? "-n -c $ini" : "", __FILE__, WORKER_ARGV_VALUE); |
| 54 | + } else { |
| 55 | + $cmd = sprintf('%s "%s" %s %s', PHP_BINARY, __FILE__, WORKER_ARGV_VALUE, $worker); |
| 56 | + } |
| 57 | + $this->workerHandle[$worker] = proc_open($cmd, [['pipe', 'r'], ['pipe', 'w'], STDERR], $pipes); |
| 58 | + $this->workerStdIn[$worker] = $pipes[0]; |
| 59 | + $this->workerStdOut[$worker] = $pipes[1]; |
| 60 | + |
| 61 | + fwrite($this->workerStdIn[$worker], $code . "\n---\n"); |
| 62 | + } |
| 63 | + |
| 64 | + private function cleanupWorkerProcess($worker) |
| 65 | + { |
| 66 | + fclose($this->workerStdIn[$worker]); |
| 67 | + fclose($this->workerStdOut[$worker]); |
| 68 | + proc_close($this->workerHandle[$worker]); |
| 69 | + } |
| 70 | + |
| 71 | + private function stripPhpTagsFromCode($code) |
| 72 | + { |
| 73 | + return preg_replace('/^\s*<\?(?:php)?|\?>\s*$/i', '', $code); |
| 74 | + } |
| 75 | + |
| 76 | + public function runWorker() |
| 77 | + { |
| 78 | + $code = ''; |
| 79 | + |
| 80 | + while (1) { |
| 81 | + $line = fgets(STDIN); |
| 82 | + |
| 83 | + if (trim($line) === "---") { |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + $code .= $line; |
| 88 | + } |
| 89 | + |
| 90 | + eval($code); |
| 91 | + } |
| 92 | + |
| 93 | + public function run($testCode, array $workerCodes) |
| 94 | + { |
| 95 | + foreach ($workerCodes as $worker => $code) { |
| 96 | + $this->spawnWorkerProcess($worker, $this->stripPhpTagsFromCode($code)); |
| 97 | + } |
| 98 | + eval($this->stripPhpTagsFromCode($testCode)); |
| 99 | + foreach ($workerCodes as $worker => $code) { |
| 100 | + $this->cleanupWorkerProcess($worker); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public function wait($worker) |
| 105 | + { |
| 106 | + fgets($this->isWorker ? STDIN : $this->workerStdOut[$worker]); |
| 107 | + } |
| 108 | + |
| 109 | + public function notify($worker) |
| 110 | + { |
| 111 | + fwrite($this->isWorker ? STDOUT : $this->workerStdIn[$worker], "\n"); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +if (isset($argv[1]) && $argv[1] === WORKER_ARGV_VALUE) { |
| 116 | + ServerClientProxyTestCase::getInstance(true)->runWorker(); |
| 117 | +} |
0 commit comments