Drupal investigation

UnixPipes.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /** @var bool */
  22. private $ttyMode;
  23. /** @var bool */
  24. private $ptyMode;
  25. /** @var bool */
  26. private $disableOutput;
  27. public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
  28. {
  29. $this->ttyMode = (bool) $ttyMode;
  30. $this->ptyMode = (bool) $ptyMode;
  31. $this->disableOutput = (bool) $disableOutput;
  32. parent::__construct($input);
  33. }
  34. public function __destruct()
  35. {
  36. $this->close();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getDescriptors()
  42. {
  43. if ($this->disableOutput) {
  44. $nullstream = fopen('/dev/null', 'c');
  45. return array(
  46. array('pipe', 'r'),
  47. $nullstream,
  48. $nullstream,
  49. );
  50. }
  51. if ($this->ttyMode) {
  52. return array(
  53. array('file', '/dev/tty', 'r'),
  54. array('file', '/dev/tty', 'w'),
  55. array('file', '/dev/tty', 'w'),
  56. );
  57. }
  58. if ($this->ptyMode && Process::isPtySupported()) {
  59. return array(
  60. array('pty'),
  61. array('pty'),
  62. array('pty'),
  63. );
  64. }
  65. return array(
  66. array('pipe', 'r'),
  67. array('pipe', 'w'), // stdout
  68. array('pipe', 'w'), // stderr
  69. );
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getFiles()
  75. {
  76. return array();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function readAndWrite($blocking, $close = false)
  82. {
  83. $this->unblock();
  84. $w = $this->write();
  85. $read = $e = array();
  86. $r = $this->pipes;
  87. unset($r[0]);
  88. // let's have a look if something changed in streams
  89. if (($r || $w) && false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
  90. // if a system call has been interrupted, forget about it, let's try again
  91. // otherwise, an error occurred, let's reset pipes
  92. if (!$this->hasSystemCallBeenInterrupted()) {
  93. $this->pipes = array();
  94. }
  95. return $read;
  96. }
  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. }