Runtime.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/environment.
  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. use const PHP_BINARY;
  12. use const PHP_BINDIR;
  13. use const PHP_MAJOR_VERSION;
  14. use const PHP_SAPI;
  15. use const PHP_VERSION;
  16. use function array_map;
  17. use function array_merge;
  18. use function escapeshellarg;
  19. use function explode;
  20. use function extension_loaded;
  21. use function ini_get;
  22. use function is_readable;
  23. use function parse_ini_file;
  24. use function php_ini_loaded_file;
  25. use function php_ini_scanned_files;
  26. use function phpversion;
  27. use function sprintf;
  28. use function strrpos;
  29. final class Runtime
  30. {
  31. private static string $rawBinary;
  32. private static bool $initialized = false;
  33. /**
  34. * Returns true when Xdebug or PCOV is available or
  35. * the runtime used is PHPDBG.
  36. */
  37. public function canCollectCodeCoverage(): bool
  38. {
  39. return $this->hasXdebug() || $this->hasPCOV() || $this->hasPHPDBGCodeCoverage();
  40. }
  41. /**
  42. * Returns true when Zend OPcache is loaded, enabled,
  43. * and is configured to discard comments.
  44. */
  45. public function discardsComments(): bool
  46. {
  47. if (!$this->isOpcacheActive()) {
  48. return false;
  49. }
  50. if (ini_get('opcache.save_comments') !== '0') {
  51. return false;
  52. }
  53. return true;
  54. }
  55. /**
  56. * Returns true when Zend OPcache is loaded, enabled,
  57. * and is configured to perform just-in-time compilation.
  58. */
  59. public function performsJustInTimeCompilation(): bool
  60. {
  61. if (PHP_MAJOR_VERSION < 8) {
  62. return false;
  63. }
  64. if (!$this->isOpcacheActive()) {
  65. return false;
  66. }
  67. if (ini_get('opcache.jit_buffer_size') === '0') {
  68. return false;
  69. }
  70. $jit = ini_get('opcache.jit');
  71. if (($jit === 'disable') || ($jit === 'off')) {
  72. return false;
  73. }
  74. if (strrpos($jit, '0') === 3) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. /**
  80. * Returns the raw path to the binary of the current runtime.
  81. */
  82. public function getRawBinary(): string
  83. {
  84. if (self::$initialized) {
  85. return self::$rawBinary;
  86. }
  87. if (PHP_BINARY !== '') {
  88. self::$rawBinary = PHP_BINARY;
  89. self::$initialized = true;
  90. return self::$rawBinary;
  91. }
  92. // @codeCoverageIgnoreStart
  93. $possibleBinaryLocations = [
  94. PHP_BINDIR . '/php',
  95. PHP_BINDIR . '/php-cli.exe',
  96. PHP_BINDIR . '/php.exe',
  97. ];
  98. foreach ($possibleBinaryLocations as $binary) {
  99. if (is_readable($binary)) {
  100. self::$rawBinary = $binary;
  101. self::$initialized = true;
  102. return self::$rawBinary;
  103. }
  104. }
  105. self::$rawBinary = 'php';
  106. self::$initialized = true;
  107. return self::$rawBinary;
  108. // @codeCoverageIgnoreEnd
  109. }
  110. /**
  111. * Returns the escaped path to the binary of the current runtime.
  112. */
  113. public function getBinary(): string
  114. {
  115. return escapeshellarg($this->getRawBinary());
  116. }
  117. public function getNameWithVersion(): string
  118. {
  119. return $this->getName() . ' ' . $this->getVersion();
  120. }
  121. public function getNameWithVersionAndCodeCoverageDriver(): string
  122. {
  123. if ($this->hasPCOV()) {
  124. return sprintf(
  125. '%s with PCOV %s',
  126. $this->getNameWithVersion(),
  127. phpversion('pcov'),
  128. );
  129. }
  130. if ($this->hasXdebug()) {
  131. return sprintf(
  132. '%s with Xdebug %s',
  133. $this->getNameWithVersion(),
  134. phpversion('xdebug'),
  135. );
  136. }
  137. return $this->getNameWithVersion();
  138. }
  139. public function getName(): string
  140. {
  141. if ($this->isPHPDBG()) {
  142. // @codeCoverageIgnoreStart
  143. return 'PHPDBG';
  144. // @codeCoverageIgnoreEnd
  145. }
  146. return 'PHP';
  147. }
  148. public function getVendorUrl(): string
  149. {
  150. return 'https://www.php.net/';
  151. }
  152. public function getVersion(): string
  153. {
  154. return PHP_VERSION;
  155. }
  156. /**
  157. * Returns true when the runtime used is PHP and Xdebug is loaded.
  158. */
  159. public function hasXdebug(): bool
  160. {
  161. return $this->isPHP() && extension_loaded('xdebug');
  162. }
  163. /**
  164. * Returns true when the runtime used is PHP without the PHPDBG SAPI.
  165. */
  166. public function isPHP(): bool
  167. {
  168. return !$this->isPHPDBG();
  169. }
  170. /**
  171. * Returns true when the runtime used is PHP with the PHPDBG SAPI.
  172. */
  173. public function isPHPDBG(): bool
  174. {
  175. return PHP_SAPI === 'phpdbg';
  176. }
  177. /**
  178. * Returns true when the runtime used is PHP with the PHPDBG SAPI
  179. * and the phpdbg_*_oplog() functions are available (PHP >= 7.0).
  180. */
  181. public function hasPHPDBGCodeCoverage(): bool
  182. {
  183. return $this->isPHPDBG();
  184. }
  185. /**
  186. * Returns true when the runtime used is PHP with PCOV loaded and enabled.
  187. */
  188. public function hasPCOV(): bool
  189. {
  190. return $this->isPHP() && extension_loaded('pcov') && ini_get('pcov.enabled');
  191. }
  192. /**
  193. * Parses the loaded php.ini file (if any) as well as all
  194. * additional php.ini files from the additional ini dir for
  195. * a list of all configuration settings loaded from files
  196. * at startup. Then checks for each php.ini setting passed
  197. * via the `$values` parameter whether this setting has
  198. * been changed at runtime. Returns an array of strings
  199. * where each string has the format `key=value` denoting
  200. * the name of a changed php.ini setting with its new value.
  201. *
  202. * @return string[]
  203. */
  204. public function getCurrentSettings(array $values): array
  205. {
  206. $diff = [];
  207. $files = [];
  208. if ($file = php_ini_loaded_file()) {
  209. $files[] = $file;
  210. }
  211. if ($scanned = php_ini_scanned_files()) {
  212. $files = array_merge(
  213. $files,
  214. array_map(
  215. 'trim',
  216. explode(",\n", $scanned),
  217. ),
  218. );
  219. }
  220. foreach ($files as $ini) {
  221. $config = parse_ini_file($ini, true);
  222. foreach ($values as $value) {
  223. $set = ini_get($value);
  224. if (empty($set)) {
  225. continue;
  226. }
  227. if ((!isset($config[$value]) || ($set !== $config[$value]))) {
  228. $diff[$value] = sprintf('%s=%s', $value, $set);
  229. }
  230. }
  231. }
  232. return $diff;
  233. }
  234. private function isOpcacheActive(): bool
  235. {
  236. if (!extension_loaded('Zend OPcache')) {
  237. return false;
  238. }
  239. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && ini_get('opcache.enable_cli') === '1') {
  240. return true;
  241. }
  242. if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' && ini_get('opcache.enable') === '1') {
  243. return true;
  244. }
  245. return false;
  246. }
  247. }