Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Sebastian Bergmann nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @category   Testing
38
 * @package    PHPUnit
39
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link       http://www.phpunit.de/
43
 * @since      File available since Release 3.4.0
44
 */
45
 
46
require_once 'PHPUnit/Util/Filter.php';
47
 
48
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
49
 
50
/**
51
 * Utility methods for PHP sub-processes.
52
 *
53
 * @category   Testing
54
 * @package    PHPUnit
55
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
56
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
57
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58
 * @version    Release: 3.4.15
59
 * @link       http://www.phpunit.de/
60
 * @since      Class available since Release 3.4.0
61
 */
62
class PHPUnit_Util_PHP
63
{
64
    /**
65
     * Path to the PHP interpreter that is to be used.
66
     *
67
     * @var    string $phpBinary
68
     */
69
    protected static $phpBinary = NULL;
70
 
71
    /**
72
     * Descriptor specification for proc_open().
73
     *
74
     * @var    array
75
     */
76
    protected static $descriptorSpec = array(
77
 
78
      1 => array('pipe', 'w'),
79
      2 => array('pipe', 'w')
80
    );
81
 
82
    /**
83
     * Returns the path to a PHP interpreter.
84
     *
85
     * PHPUnit_Util_PHP::$phpBinary contains the path to the PHP
86
     * interpreter.
87
     *
88
     * When not set, the following assumptions will be made:
89
     *
90
     *   1. When the PHP CLI/CGI binary configured with the PEAR Installer
91
     *      (php_bin configuration value) is readable, it will be used.
92
     *
93
     *   2. When PHPUnit is run using the CLI SAPI and the $_SERVER['_']
94
     *      variable does not contain the string "PHPUnit", $_SERVER['_']
95
     *      is assumed to contain the path to the current PHP interpreter
96
     *      and that will be used.
97
     *
98
     *   3. When PHPUnit is run using the CLI SAPI and the $_SERVER['_']
99
     *      variable contains the string "PHPUnit", the file that $_SERVER['_']
100
     *      points to is assumed to be the PHPUnit TextUI CLI wrapper script
101
     *      "phpunit" and the binary set up using #! on that file's first
102
     *      line of code is assumed to contain the path to the current PHP
103
     *      interpreter and that will be used.
104
     *
105
     *   4. The current PHP interpreter is assumed to be in the $PATH and
106
     *      to be invokable through "php".
107
     *
108
     * @return string
109
     */
110
    public static function getPhpBinary()
111
    {
112
        if (self::$phpBinary === NULL) {
113
            if (is_readable('/usr/bin/php')) {
114
                self::$phpBinary = '/usr/bin/php';
115
            }
116
 
117
            else if (PHP_SAPI == 'cli' && isset($_SERVER['_']) &&
118
                     strpos($_SERVER['_'], 'phpunit') !== FALSE) {
119
                $file            = file($_SERVER['_']);
120
                $tmp             = explode(' ', $file[0]);
121
                self::$phpBinary = trim($tmp[1]);
122
            }
123
 
124
            if (!is_readable(self::$phpBinary)) {
125
                self::$phpBinary = 'php';
126
            } else {
127
                self::$phpBinary = escapeshellarg(self::$phpBinary);
128
            }
129
        }
130
 
131
        return self::$phpBinary;
132
    }
133
 
134
    /**
135
     * Runs a single job (PHP code) using a separate PHP process.
136
     *
137
     * @param  string $job
138
     * @return string
139
     */
140
    public static function runJob($job)
141
    {
142
        $process = proc_open(
143
          self::getPhpBinary(), self::$descriptorSpec, $pipes
144
        );
145
 
146
        if (is_resource($process)) {
147
            fwrite($pipes[0], $job);
148
            fclose($pipes[0]);
149
 
150
            $stdout = stream_get_contents($pipes[1]);
151
            fclose($pipes[1]);
152
 
153
            $stderr = stream_get_contents($pipes[2]);
154
            fclose($pipes[2]);
155
 
156
            proc_close($process);
157
 
158
            return array('stdout' => $stdout, 'stderr' => $stderr);
159
        }
160
    }
161
}
162
?>