菜谱项目

UnixPipes.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 $haveReadSupport;
  24. public function __construct($ttyMode, $ptyMode, $input, $haveReadSupport)
  25. {
  26. $this->ttyMode = (bool) $ttyMode;
  27. $this->ptyMode = (bool) $ptyMode;
  28. $this->haveReadSupport = (bool) $haveReadSupport;
  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->haveReadSupport) {
  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. if (($r || $w) && false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
  87. // if a system call has been interrupted, forget about it, let's try again
  88. // otherwise, an error occurred, let's reset pipes
  89. if (!$this->hasSystemCallBeenInterrupted()) {
  90. $this->pipes = array();
  91. }
  92. return $read;
  93. }
  94. foreach ($r as $pipe) {
  95. // prior PHP 5.4 the array passed to stream_select is modified and
  96. // lose key association, we have to find back the key
  97. $read[$type = array_search($pipe, $this->pipes, true)] = '';
  98. do {
  99. $data = fread($pipe, self::CHUNK_SIZE);
  100. $read[$type] .= $data;
  101. } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
  102. if (!isset($read[$type][0])) {
  103. unset($read[$type]);
  104. }
  105. if ($close && feof($pipe)) {
  106. fclose($pipe);
  107. unset($this->pipes[$type]);
  108. }
  109. }
  110. return $read;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function haveReadSupport()
  116. {
  117. return $this->haveReadSupport;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function areOpen()
  123. {
  124. return (bool) $this->pipes;
  125. }
  126. }