PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/sebastian/environment/src/Runtime.php

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