1
0

RequestStream.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 $position;
  9. protected $varname;
  10. function stream_open($path, $mode, $options, &$opened_path) {
  11. $url = parse_url($path);
  12. $this->varname = $url["host"];
  13. $this->position = 0;
  14. return true;
  15. }
  16. function stream_read($count) {
  17. $ret = substr($GLOBALS[$this->varname], $this->position, $count);
  18. $this->position += strlen($ret);
  19. return $ret;
  20. }
  21. function stream_write($data) {
  22. $left = substr($GLOBALS[$this->varname], 0, $this->position);
  23. $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
  24. $GLOBALS[$this->varname] = $left . $data . $right;
  25. $this->position += strlen($data);
  26. return strlen($data);
  27. }
  28. function stream_tell() {
  29. return $this->position;
  30. }
  31. function stream_eof() {
  32. return $this->position >= strlen($GLOBALS[$this->varname]);
  33. }
  34. function stream_seek($offset, $whence) {
  35. switch ($whence) {
  36. case SEEK_SET:
  37. if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
  38. $this->position = $offset;
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. break;
  44. case SEEK_CUR:
  45. if ($offset >= 0) {
  46. $this->position += $offset;
  47. return true;
  48. } else {
  49. return false;
  50. }
  51. break;
  52. case SEEK_END:
  53. if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
  54. $this->position = strlen($GLOBALS[$this->varname]) + $offset;
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. break;
  60. default:
  61. return false;
  62. }
  63. }
  64. public function stream_stat() {
  65. $size = strlen($GLOBALS[$this->varname]);
  66. $time = time();
  67. $data = array(
  68. 'dev' => 0,
  69. 'ino' => 0,
  70. 'mode' => 0777,
  71. 'nlink' => 1,
  72. 'uid' => 0,
  73. 'gid' => 0,
  74. 'rdev' => '',
  75. 'size' => $size,
  76. 'atime' => $time,
  77. 'mtime' => $time,
  78. 'ctime' => $time,
  79. 'blksize' => -1,
  80. 'blocks' => -1,
  81. );
  82. return array_values($data) + $data;
  83. //return false;
  84. }
  85. function stream_metadata($path, $option, $var) {
  86. if($option == STREAM_META_TOUCH) {
  87. $url = parse_url($path);
  88. $varname = $url["host"];
  89. if(!isset($GLOBALS[$varname])) {
  90. $GLOBALS[$varname] = '';
  91. }
  92. return true;
  93. }
  94. return false;
  95. }
  96. }