RequestStream.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Test\AppFramework\Http;
  3. /**
  4. * Copy of http://dk1.php.net/manual/en/stream.streamwrapper.example-1.php
  5. * Used to simulate php://input for Request tests
  6. */
  7. class RequestStream {
  8. protected int $position = 0;
  9. protected string $varname = '';
  10. /* @var resource */
  11. public $context;
  12. public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool {
  13. $url = parse_url($path);
  14. $this->varname = $url["host"] ?? '';
  15. $this->position = 0;
  16. return true;
  17. }
  18. public function stream_read(int $count): string {
  19. $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  20. $this->position += strlen($ret);
  21. return $ret;
  22. }
  23. public function stream_write(string $data): int {
  24. $left = substr($GLOBALS[$this->varname], 0, $this->position);
  25. $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
  26. $GLOBALS[$this->varname] = $left . $data . $right;
  27. $this->position += strlen($data);
  28. return strlen($data);
  29. }
  30. public function stream_tell(): int {
  31. return $this->position;
  32. }
  33. public function stream_eof(): bool {
  34. return $this->position >= strlen($GLOBALS[$this->varname]);
  35. }
  36. public function stream_seek(int $offset, int $whence = SEEK_SET): bool {
  37. switch ($whence) {
  38. case SEEK_SET:
  39. if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
  40. $this->position = $offset;
  41. return true;
  42. } else {
  43. return false;
  44. }
  45. break;
  46. case SEEK_CUR:
  47. if ($offset >= 0) {
  48. $this->position += $offset;
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. break;
  54. case SEEK_END:
  55. if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
  56. $this->position = strlen($GLOBALS[$this->varname]) + $offset;
  57. return true;
  58. } else {
  59. return false;
  60. }
  61. break;
  62. default:
  63. return false;
  64. }
  65. }
  66. public function stream_stat(): array {
  67. $size = strlen($GLOBALS[$this->varname]);
  68. $time = time();
  69. $data = [
  70. 'dev' => 0,
  71. 'ino' => 0,
  72. 'mode' => 0777,
  73. 'nlink' => 1,
  74. 'uid' => 0,
  75. 'gid' => 0,
  76. 'rdev' => '',
  77. 'size' => $size,
  78. 'atime' => $time,
  79. 'mtime' => $time,
  80. 'ctime' => $time,
  81. 'blksize' => -1,
  82. 'blocks' => -1,
  83. ];
  84. return array_values($data) + $data;
  85. //return false;
  86. }
  87. public function stream_metadata(string $path, int $option, $var): bool {
  88. if ($option == STREAM_META_TOUCH) {
  89. $url = parse_url($path);
  90. $varname = $url["host"] ?? '';
  91. if (!isset($GLOBALS[$varname])) {
  92. $GLOBALS[$varname] = '';
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98. }