ClosureStream.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /* ===========================================================================
  3. * Copyright (c) 2018-2019 Zindex Software
  4. *
  5. * Licensed under the MIT License
  6. * =========================================================================== */
  7. namespace Opis\Closure;
  8. /**
  9. * @internal
  10. */
  11. class ClosureStream
  12. {
  13. const STREAM_PROTO = 'closure';
  14. protected static $isRegistered = false;
  15. protected $content;
  16. protected $length;
  17. protected $pointer = 0;
  18. function stream_open($path, $mode, $options, &$opened_path)
  19. {
  20. $this->content = "<?php\nreturn " . substr($path, strlen(static::STREAM_PROTO . '://')) . ";";
  21. $this->length = strlen($this->content);
  22. return true;
  23. }
  24. public function stream_read($count)
  25. {
  26. $value = substr($this->content, $this->pointer, $count);
  27. $this->pointer += $count;
  28. return $value;
  29. }
  30. public function stream_eof()
  31. {
  32. return $this->pointer >= $this->length;
  33. }
  34. public function stream_stat()
  35. {
  36. $stat = stat(__FILE__);
  37. $stat[7] = $stat['size'] = $this->length;
  38. return $stat;
  39. }
  40. public function url_stat($path, $flags)
  41. {
  42. $stat = stat(__FILE__);
  43. $stat[7] = $stat['size'] = $this->length;
  44. return $stat;
  45. }
  46. public function stream_seek($offset, $whence = SEEK_SET)
  47. {
  48. $crt = $this->pointer;
  49. switch ($whence) {
  50. case SEEK_SET:
  51. $this->pointer = $offset;
  52. break;
  53. case SEEK_CUR:
  54. $this->pointer += $offset;
  55. break;
  56. case SEEK_END:
  57. $this->pointer = $this->length + $offset;
  58. break;
  59. }
  60. if ($this->pointer < 0 || $this->pointer >= $this->length) {
  61. $this->pointer = $crt;
  62. return false;
  63. }
  64. return true;
  65. }
  66. public function stream_tell()
  67. {
  68. return $this->pointer;
  69. }
  70. public static function register()
  71. {
  72. if (!static::$isRegistered) {
  73. static::$isRegistered = stream_wrapper_register(static::STREAM_PROTO, __CLASS__);
  74. }
  75. }
  76. }