/vendor/phpunit/phpunit/src/Util/PHP/Windows.php

https://gitlab.com/judielsm/Handora · PHP · 104 lines · 56 code · 15 blank · 33 comment · 3 complexity · 9b7716f980215ed5ec73e92f1ed65ee6 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  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. use SebastianBergmann\Environment\Runtime;
  11. /**
  12. * Windows utility for PHP sub-processes.
  13. *
  14. * @since Class available since Release 3.5.12
  15. */
  16. class PHPUnit_Util_PHP_Windows extends PHPUnit_Util_PHP_Default
  17. {
  18. /**
  19. * @var string
  20. */
  21. private $tempFile;
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * Reading from STDOUT or STDERR hangs forever on Windows if the output is
  26. * too large.
  27. *
  28. * @see https://bugs.php.net/bug.php?id=51800
  29. */
  30. public function runJob($job, array $settings = array())
  31. {
  32. $runtime = new Runtime;
  33. if (false === $stdout_handle = tmpfile()) {
  34. throw new PHPUnit_Framework_Exception(
  35. 'A temporary file could not be created; verify that your TEMP environment variable is writable'
  36. );
  37. }
  38. $process = proc_open(
  39. $runtime->getBinary() . $this->settingsToParameters($settings),
  40. array(
  41. 0 => array('pipe', 'r'),
  42. 1 => $stdout_handle,
  43. 2 => array('pipe', 'w')
  44. ),
  45. $pipes
  46. );
  47. if (!is_resource($process)) {
  48. throw new PHPUnit_Framework_Exception(
  49. 'Unable to spawn worker process'
  50. );
  51. }
  52. $this->process($pipes[0], $job);
  53. fclose($pipes[0]);
  54. $stderr = stream_get_contents($pipes[2]);
  55. fclose($pipes[2]);
  56. proc_close($process);
  57. rewind($stdout_handle);
  58. $stdout = stream_get_contents($stdout_handle);
  59. fclose($stdout_handle);
  60. $this->cleanup();
  61. return array('stdout' => $stdout, 'stderr' => $stderr);
  62. }
  63. /**
  64. * @param resource $pipe
  65. * @param string $job
  66. * @throws PHPUnit_Framework_Exception
  67. * @since Method available since Release 3.5.12
  68. */
  69. protected function process($pipe, $job)
  70. {
  71. if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) ||
  72. file_put_contents($this->tempFile, $job) === false) {
  73. throw new PHPUnit_Framework_Exception(
  74. 'Unable to write temporary file'
  75. );
  76. }
  77. fwrite(
  78. $pipe,
  79. '<?php require_once ' . var_export($this->tempFile, true) . '; ?>'
  80. );
  81. }
  82. /**
  83. * @since Method available since Release 3.5.12
  84. */
  85. protected function cleanup()
  86. {
  87. unlink($this->tempFile);
  88. }
  89. }