CakeStreamWrapper.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. namespace TestApp\Http\Client\Adapter;
  4. class CakeStreamWrapper implements \ArrayAccess
  5. {
  6. private $_stream;
  7. private $_query = [];
  8. private $_data = [
  9. 'headers' => [
  10. 'HTTP/1.1 200 OK',
  11. ],
  12. ];
  13. public function stream_open($path, $mode, $options, &$openedPath)
  14. {
  15. if ($path === 'http://throw_exception/') {
  16. throw new \Exception();
  17. }
  18. $query = parse_url($path, PHP_URL_QUERY);
  19. if ($query) {
  20. parse_str($query, $this->_query);
  21. }
  22. $this->_stream = fopen('php://memory', 'rb+');
  23. fwrite($this->_stream, str_repeat('x', 20000));
  24. rewind($this->_stream);
  25. return true;
  26. }
  27. public function stream_close()
  28. {
  29. return fclose($this->_stream);
  30. }
  31. public function stream_read($count)
  32. {
  33. if (isset($this->_query['sleep'])) {
  34. sleep(1);
  35. }
  36. return fread($this->_stream, $count);
  37. }
  38. public function stream_eof()
  39. {
  40. return feof($this->_stream);
  41. }
  42. public function stream_set_option($option, $arg1, $arg2)
  43. {
  44. return false;
  45. }
  46. public function offsetExists($offset)
  47. {
  48. return isset($this->_data[$offset]);
  49. }
  50. public function offsetGet($offset)
  51. {
  52. return $this->_data[$offset];
  53. }
  54. public function offsetSet($offset, $value)
  55. {
  56. $this->_data[$offset] = $value;
  57. }
  58. public function offsetUnset($offset)
  59. {
  60. unset($this->_data[$offset]);
  61. }
  62. }