6587aae85494e34dc48bd76f8a65d4d2f627e563
[yaffs-website] / PHP / Windows.php
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  * Windows utility for PHP sub-processes.
15  *
16  * @since Class available since Release 3.5.12
17  */
18 class PHPUnit_Util_PHP_Windows extends PHPUnit_Util_PHP_Default
19 {
20     /**
21      * @var string
22      */
23     private $tempFile;
24
25     /**
26      * {@inheritdoc}
27      *
28      * Reading from STDOUT or STDERR hangs forever on Windows if the output is
29      * too large.
30      *
31      * @see https://bugs.php.net/bug.php?id=51800
32      */
33     public function runJob($job, array $settings = array())
34     {
35         $runtime = new Runtime;
36
37         if (false === $stdout_handle = tmpfile()) {
38             throw new PHPUnit_Framework_Exception(
39                 'A temporary file could not be created; verify that your TEMP environment variable is writable'
40             );
41         }
42
43         $process = proc_open(
44             $runtime->getBinary() . $this->settingsToParameters($settings),
45             array(
46             0 => array('pipe', 'r'),
47             1 => $stdout_handle,
48             2 => array('pipe', 'w')
49             ),
50             $pipes
51         );
52
53         if (!is_resource($process)) {
54             throw new PHPUnit_Framework_Exception(
55                 'Unable to spawn worker process'
56             );
57         }
58
59         $this->process($pipes[0], $job);
60         fclose($pipes[0]);
61
62         $stderr = stream_get_contents($pipes[2]);
63         fclose($pipes[2]);
64
65         proc_close($process);
66
67         rewind($stdout_handle);
68         $stdout = stream_get_contents($stdout_handle);
69         fclose($stdout_handle);
70
71         $this->cleanup();
72
73         return array('stdout' => $stdout, 'stderr' => $stderr);
74     }
75
76     /**
77      * @param resource $pipe
78      * @param string   $job
79      *
80      * @throws PHPUnit_Framework_Exception
81      *
82      * @since  Method available since Release 3.5.12
83      */
84     protected function process($pipe, $job)
85     {
86         if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) ||
87             file_put_contents($this->tempFile, $job) === false) {
88             throw new PHPUnit_Framework_Exception(
89                 'Unable to write temporary file'
90             );
91         }
92
93         fwrite(
94             $pipe,
95             '<?php require_once ' . var_export($this->tempFile, true) .  '; ?>'
96         );
97     }
98
99     /**
100      * @since Method available since Release 3.5.12
101      */
102     protected function cleanup()
103     {
104         unlink($this->tempFile);
105     }
106 }