Default.php (1935B)
1 <?php 2 /* 3 * This file is part of PHPUnit. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 use SebastianBergmann\Environment\Runtime; 12 13 /** 14 * Default utility for PHP sub-processes. 15 * 16 * @since Class available since Release 3.5.12 17 */ 18 class PHPUnit_Util_PHP_Default extends PHPUnit_Util_PHP 19 { 20 /** 21 * Runs a single job (PHP code) using a separate PHP process. 22 * 23 * @param string $job 24 * @param array $settings 25 * 26 * @return array 27 * 28 * @throws PHPUnit_Framework_Exception 29 */ 30 public function runJob($job, array $settings = array()) 31 { 32 $runtime = new Runtime; 33 34 $process = proc_open( 35 $runtime->getBinary() . $this->settingsToParameters($settings), 36 array( 37 0 => array('pipe', 'r'), 38 1 => array('pipe', 'w'), 39 2 => array('pipe', 'w') 40 ), 41 $pipes 42 ); 43 44 if (!is_resource($process)) { 45 throw new PHPUnit_Framework_Exception( 46 'Unable to spawn worker process' 47 ); 48 } 49 50 $this->process($pipes[0], $job); 51 fclose($pipes[0]); 52 53 $stdout = stream_get_contents($pipes[1]); 54 fclose($pipes[1]); 55 56 $stderr = stream_get_contents($pipes[2]); 57 fclose($pipes[2]); 58 59 proc_close($process); 60 $this->cleanup(); 61 62 return array('stdout' => $stdout, 'stderr' => $stderr); 63 } 64 65 /** 66 * @param resource $pipe 67 * @param string $job 68 * 69 * @throws PHPUnit_Framework_Exception 70 * 71 * @since Method available since Release 3.5.12 72 */ 73 protected function process($pipe, $job) 74 { 75 fwrite($pipe, $job); 76 } 77 78 /** 79 * @since Method available since Release 3.5.12 80 */ 81 protected function cleanup() 82 { 83 } 84 }