UnixPipes.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Process\Pipes;
  11. use Symfony\Component\Process\Process;
  12. /**
  13. * UnixPipes implementation uses unix pipes as handles.
  14. *
  15. * @author Romain Neutron <imprec@gmail.com>
  16. *
  17. * @internal
  18. */
  19. class UnixPipes extends AbstractPipes
  20. {
  21. private $ttyMode;
  22. private $ptyMode;
  23. private $disableOutput;
  24. public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
  25. {
  26. $this->ttyMode = (bool) $ttyMode;
  27. $this->ptyMode = (bool) $ptyMode;
  28. $this->disableOutput = (bool) $disableOutput;
  29. parent::__construct($input);
  30. }
  31. public function __destruct()
  32. {
  33. $this->close();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getDescriptors()
  39. {
  40. if ($this->disableOutput) {
  41. $nullstream = fopen('/dev/null', 'c');
  42. return array(
  43. array('pipe', 'r'),
  44. $nullstream,
  45. $nullstream,
  46. );
  47. }
  48. if ($this->ttyMode) {
  49. return array(
  50. array('file', '/dev/tty', 'r'),
  51. array('file', '/dev/tty', 'w'),
  52. array('file', '/dev/tty', 'w'),
  53. );
  54. }
  55. if ($this->ptyMode && Process::isPtySupported()) {
  56. return array(
  57. array('pty'),
  58. array('pty'),
  59. array('pty'),
  60. );
  61. }
  62. return array(
  63. array('pipe', 'r'),
  64. array('pipe', 'w'), // stdout
  65. array('pipe', 'w'), // stderr
  66. );
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getFiles()
  72. {
  73. return array();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function readAndWrite($blocking, $close = false)
  79. {
  80. $this->unblock();
  81. $w = $this->write();
  82. $read = $e = array();
  83. $r = $this->pipes;
  84. unset($r[0]);
  85. // let's have a look if something changed in streams
  86. set_error_handler(array($this, 'handleError'));
  87. if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
  88. restore_error_handler();
  89. // if a system call has been interrupted, forget about it, let's try again
  90. // otherwise, an error occurred, let's reset pipes
  91. if (!$this->hasSystemCallBeenInterrupted()) {
  92. $this->pipes = array();
  93. }
  94. return $read;
  95. }
  96. restore_error_handler();
  97. foreach ($r as $pipe) {
  98. // prior PHP 5.4 the array passed to stream_select is modified and
  99. // lose key association, we have to find back the key
  100. $read[$type = array_search($pipe, $this->pipes, true)] = '';
  101. do {
  102. $data = fread($pipe, self::CHUNK_SIZE);
  103. $read[$type] .= $data;
  104. } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
  105. if (!isset($read[$type][0])) {
  106. unset($read[$type]);
  107. }
  108. if ($close && feof($pipe)) {
  109. fclose($pipe);
  110. unset($this->pipes[$type]);
  111. }
  112. }
  113. return $read;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function areOpen()
  119. {
  120. return (bool) $this->pipes;
  121. }
  122. /**
  123. * Creates a new UnixPipes instance.
  124. *
  125. * @param Process $process
  126. * @param string|resource $input
  127. *
  128. * @return static
  129. */
  130. public static function create(Process $process, $input)
  131. {
  132. return new static($process->isTty(), $process->isPty(), $input, $process->isOutputDisabled());
  133. }
  134. }