Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * This file is part of the Environment package.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 namespace SebastianBergmann\Environment;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Utility class for HHVM/PHP environment handling.
|
Chris@0
|
15 */
|
Chris@0
|
16 class Runtime
|
Chris@0
|
17 {
|
Chris@0
|
18 /**
|
Chris@0
|
19 * @var string
|
Chris@0
|
20 */
|
Chris@0
|
21 private static $binary;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Returns true when Xdebug is supported or
|
Chris@0
|
25 * the runtime used is PHPDBG (PHP >= 7.0).
|
Chris@0
|
26 *
|
Chris@0
|
27 * @return bool
|
Chris@0
|
28 */
|
Chris@0
|
29 public function canCollectCodeCoverage()
|
Chris@0
|
30 {
|
Chris@0
|
31 return $this->hasXdebug() || $this->hasPHPDBGCodeCoverage();
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Returns the path to the binary of the current runtime.
|
Chris@0
|
36 * Appends ' --php' to the path when the runtime is HHVM.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @return string
|
Chris@0
|
39 */
|
Chris@0
|
40 public function getBinary()
|
Chris@0
|
41 {
|
Chris@0
|
42 // HHVM
|
Chris@0
|
43 if (self::$binary === null && $this->isHHVM()) {
|
Chris@0
|
44 if ((self::$binary = getenv('PHP_BINARY')) === false) {
|
Chris@0
|
45 self::$binary = PHP_BINARY;
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 self::$binary = escapeshellarg(self::$binary) . ' --php';
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 // PHP >= 5.4.0
|
Chris@0
|
52 if (self::$binary === null && defined('PHP_BINARY')) {
|
Chris@0
|
53 if (PHP_BINARY !== '') {
|
Chris@0
|
54 self::$binary = escapeshellarg(PHP_BINARY);
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 // PHP < 5.4.0
|
Chris@0
|
59 if (self::$binary === null) {
|
Chris@0
|
60 if (PHP_SAPI == 'cli' && isset($_SERVER['_'])) {
|
Chris@0
|
61 if (strpos($_SERVER['_'], 'phpunit') !== false) {
|
Chris@0
|
62 $file = file($_SERVER['_']);
|
Chris@0
|
63
|
Chris@0
|
64 if (strpos($file[0], ' ') !== false) {
|
Chris@0
|
65 $tmp = explode(' ', $file[0]);
|
Chris@0
|
66 self::$binary = escapeshellarg(trim($tmp[1]));
|
Chris@0
|
67 } else {
|
Chris@0
|
68 self::$binary = escapeshellarg(ltrim(trim($file[0]), '#!'));
|
Chris@0
|
69 }
|
Chris@0
|
70 } elseif (strpos(basename($_SERVER['_']), 'php') !== false) {
|
Chris@0
|
71 self::$binary = escapeshellarg($_SERVER['_']);
|
Chris@0
|
72 }
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 if (self::$binary === null) {
|
Chris@0
|
77 $possibleBinaryLocations = array(
|
Chris@0
|
78 PHP_BINDIR . '/php',
|
Chris@0
|
79 PHP_BINDIR . '/php-cli.exe',
|
Chris@0
|
80 PHP_BINDIR . '/php.exe'
|
Chris@0
|
81 );
|
Chris@0
|
82
|
Chris@0
|
83 foreach ($possibleBinaryLocations as $binary) {
|
Chris@0
|
84 if (is_readable($binary)) {
|
Chris@0
|
85 self::$binary = escapeshellarg($binary);
|
Chris@0
|
86 break;
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 if (self::$binary === null) {
|
Chris@0
|
92 self::$binary = 'php';
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 return self::$binary;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * @return string
|
Chris@0
|
100 */
|
Chris@0
|
101 public function getNameWithVersion()
|
Chris@0
|
102 {
|
Chris@0
|
103 return $this->getName() . ' ' . $this->getVersion();
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * @return string
|
Chris@0
|
108 */
|
Chris@0
|
109 public function getName()
|
Chris@0
|
110 {
|
Chris@0
|
111 if ($this->isHHVM()) {
|
Chris@0
|
112 return 'HHVM';
|
Chris@0
|
113 } elseif ($this->isPHPDBG()) {
|
Chris@0
|
114 return 'PHPDBG';
|
Chris@0
|
115 } else {
|
Chris@0
|
116 return 'PHP';
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * @return string
|
Chris@0
|
122 */
|
Chris@0
|
123 public function getVendorUrl()
|
Chris@0
|
124 {
|
Chris@0
|
125 if ($this->isHHVM()) {
|
Chris@0
|
126 return 'http://hhvm.com/';
|
Chris@0
|
127 } else {
|
Chris@0
|
128 return 'https://secure.php.net/';
|
Chris@0
|
129 }
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * @return string
|
Chris@0
|
134 */
|
Chris@0
|
135 public function getVersion()
|
Chris@0
|
136 {
|
Chris@0
|
137 if ($this->isHHVM()) {
|
Chris@0
|
138 return HHVM_VERSION;
|
Chris@0
|
139 } else {
|
Chris@0
|
140 return PHP_VERSION;
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * Returns true when the runtime used is PHP and Xdebug is loaded.
|
Chris@0
|
146 *
|
Chris@0
|
147 * @return bool
|
Chris@0
|
148 */
|
Chris@0
|
149 public function hasXdebug()
|
Chris@0
|
150 {
|
Chris@0
|
151 return ($this->isPHP() || $this->isHHVM()) && extension_loaded('xdebug');
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * Returns true when the runtime used is HHVM.
|
Chris@0
|
156 *
|
Chris@0
|
157 * @return bool
|
Chris@0
|
158 */
|
Chris@0
|
159 public function isHHVM()
|
Chris@0
|
160 {
|
Chris@0
|
161 return defined('HHVM_VERSION');
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * Returns true when the runtime used is PHP without the PHPDBG SAPI.
|
Chris@0
|
166 *
|
Chris@0
|
167 * @return bool
|
Chris@0
|
168 */
|
Chris@0
|
169 public function isPHP()
|
Chris@0
|
170 {
|
Chris@0
|
171 return !$this->isHHVM() && !$this->isPHPDBG();
|
Chris@0
|
172 }
|
Chris@0
|
173
|
Chris@0
|
174 /**
|
Chris@0
|
175 * Returns true when the runtime used is PHP with the PHPDBG SAPI.
|
Chris@0
|
176 *
|
Chris@0
|
177 * @return bool
|
Chris@0
|
178 */
|
Chris@0
|
179 public function isPHPDBG()
|
Chris@0
|
180 {
|
Chris@0
|
181 return PHP_SAPI === 'phpdbg' && !$this->isHHVM();
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 /**
|
Chris@0
|
185 * Returns true when the runtime used is PHP with the PHPDBG SAPI
|
Chris@0
|
186 * and the phpdbg_*_oplog() functions are available (PHP >= 7.0).
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return bool
|
Chris@0
|
189 */
|
Chris@0
|
190 public function hasPHPDBGCodeCoverage()
|
Chris@0
|
191 {
|
Chris@0
|
192 return $this->isPHPDBG() && function_exists('phpdbg_start_oplog');
|
Chris@0
|
193 }
|
Chris@0
|
194 }
|