Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace SebastianBergmann\Environment; Chris@0: Chris@0: /** Chris@0: * Utility class for HHVM/PHP environment handling. Chris@0: */ Chris@0: class Runtime Chris@0: { Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: private static $binary; Chris@0: Chris@0: /** Chris@0: * Returns true when Xdebug is supported or Chris@0: * the runtime used is PHPDBG (PHP >= 7.0). Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function canCollectCodeCoverage() Chris@0: { Chris@0: return $this->hasXdebug() || $this->hasPHPDBGCodeCoverage(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the path to the binary of the current runtime. Chris@0: * Appends ' --php' to the path when the runtime is HHVM. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getBinary() Chris@0: { Chris@0: // HHVM Chris@0: if (self::$binary === null && $this->isHHVM()) { Chris@0: if ((self::$binary = getenv('PHP_BINARY')) === false) { Chris@0: self::$binary = PHP_BINARY; Chris@0: } Chris@0: Chris@0: self::$binary = escapeshellarg(self::$binary) . ' --php'; Chris@0: } Chris@0: Chris@0: // PHP >= 5.4.0 Chris@0: if (self::$binary === null && defined('PHP_BINARY')) { Chris@0: if (PHP_BINARY !== '') { Chris@0: self::$binary = escapeshellarg(PHP_BINARY); Chris@0: } Chris@0: } Chris@0: Chris@0: // PHP < 5.4.0 Chris@0: if (self::$binary === null) { Chris@0: if (PHP_SAPI == 'cli' && isset($_SERVER['_'])) { Chris@0: if (strpos($_SERVER['_'], 'phpunit') !== false) { Chris@0: $file = file($_SERVER['_']); Chris@0: Chris@0: if (strpos($file[0], ' ') !== false) { Chris@0: $tmp = explode(' ', $file[0]); Chris@0: self::$binary = escapeshellarg(trim($tmp[1])); Chris@0: } else { Chris@0: self::$binary = escapeshellarg(ltrim(trim($file[0]), '#!')); Chris@0: } Chris@0: } elseif (strpos(basename($_SERVER['_']), 'php') !== false) { Chris@0: self::$binary = escapeshellarg($_SERVER['_']); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (self::$binary === null) { Chris@0: $possibleBinaryLocations = array( Chris@0: PHP_BINDIR . '/php', Chris@0: PHP_BINDIR . '/php-cli.exe', Chris@0: PHP_BINDIR . '/php.exe' Chris@0: ); Chris@0: Chris@0: foreach ($possibleBinaryLocations as $binary) { Chris@0: if (is_readable($binary)) { Chris@0: self::$binary = escapeshellarg($binary); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (self::$binary === null) { Chris@0: self::$binary = 'php'; Chris@0: } Chris@0: Chris@0: return self::$binary; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getNameWithVersion() Chris@0: { Chris@0: return $this->getName() . ' ' . $this->getVersion(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: if ($this->isHHVM()) { Chris@0: return 'HHVM'; Chris@0: } elseif ($this->isPHPDBG()) { Chris@0: return 'PHPDBG'; Chris@0: } else { Chris@0: return 'PHP'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getVendorUrl() Chris@0: { Chris@0: if ($this->isHHVM()) { Chris@0: return 'http://hhvm.com/'; Chris@0: } else { Chris@0: return 'https://secure.php.net/'; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getVersion() Chris@0: { Chris@0: if ($this->isHHVM()) { Chris@0: return HHVM_VERSION; Chris@0: } else { Chris@0: return PHP_VERSION; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true when the runtime used is PHP and Xdebug is loaded. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function hasXdebug() Chris@0: { Chris@0: return ($this->isPHP() || $this->isHHVM()) && extension_loaded('xdebug'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true when the runtime used is HHVM. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isHHVM() Chris@0: { Chris@0: return defined('HHVM_VERSION'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true when the runtime used is PHP without the PHPDBG SAPI. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isPHP() Chris@0: { Chris@0: return !$this->isHHVM() && !$this->isPHPDBG(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true when the runtime used is PHP with the PHPDBG SAPI. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isPHPDBG() Chris@0: { Chris@0: return PHP_SAPI === 'phpdbg' && !$this->isHHVM(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true when the runtime used is PHP with the PHPDBG SAPI Chris@0: * and the phpdbg_*_oplog() functions are available (PHP >= 7.0). Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function hasPHPDBGCodeCoverage() Chris@0: { Chris@0: return $this->isPHPDBG() && function_exists('phpdbg_start_oplog'); Chris@0: } Chris@0: }