0
0

Pipes.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\process\pipes;
  12. abstract class Pipes
  13. {
  14. /** @var array */
  15. public $pipes = [];
  16. /** @var string */
  17. protected $inputBuffer = '';
  18. /** @var resource|null */
  19. protected $input;
  20. /** @var bool */
  21. private $blocked = true;
  22. const CHUNK_SIZE = 16384;
  23. /**
  24. * 返回用于 proc_open 描述符的数组
  25. * @return array
  26. */
  27. abstract public function getDescriptors();
  28. /**
  29. * 返回一个数组的索引由其相关的流,以防这些管道使用的临时文件的文件名。
  30. * @return string[]
  31. */
  32. abstract public function getFiles();
  33. /**
  34. * 文件句柄和管道中读取数据。
  35. * @param bool $blocking 是否使用阻塞调用
  36. * @param bool $close 是否要关闭管道,如果他们已经到达 EOF。
  37. * @return string[]
  38. */
  39. abstract public function readAndWrite($blocking, $close = false);
  40. /**
  41. * 返回当前状态如果有打开的文件句柄或管道。
  42. * @return bool
  43. */
  44. abstract public function areOpen();
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function close()
  49. {
  50. foreach ($this->pipes as $pipe) {
  51. fclose($pipe);
  52. }
  53. $this->pipes = [];
  54. }
  55. /**
  56. * 检查系统调用已被中断
  57. * @return bool
  58. */
  59. protected function hasSystemCallBeenInterrupted()
  60. {
  61. $lastError = error_get_last();
  62. return isset($lastError['message']) && false !== stripos($lastError['message'], 'interrupted system call');
  63. }
  64. protected function unblock()
  65. {
  66. if (!$this->blocked) {
  67. return;
  68. }
  69. foreach ($this->pipes as $pipe) {
  70. stream_set_blocking($pipe, 0);
  71. }
  72. if (null !== $this->input) {
  73. stream_set_blocking($this->input, 0);
  74. }
  75. $this->blocked = false;
  76. }
  77. }