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