ClosureStream.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /* ===========================================================================
  3. * Copyright (c) 2018-2021 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_set_option($option, $arg1, $arg2)
  35. {
  36. return false;
  37. }
  38. public function stream_stat()
  39. {
  40. $stat = stat(__FILE__);
  41. $stat[7] = $stat['size'] = $this->length;
  42. return $stat;
  43. }
  44. public function url_stat($path, $flags)
  45. {
  46. $stat = stat(__FILE__);
  47. $stat[7] = $stat['size'] = $this->length;
  48. return $stat;
  49. }
  50. public function stream_seek($offset, $whence = SEEK_SET)
  51. {
  52. $crt = $this->pointer;
  53. switch ($whence) {
  54. case SEEK_SET:
  55. $this->pointer = $offset;
  56. break;
  57. case SEEK_CUR:
  58. $this->pointer += $offset;
  59. break;
  60. case SEEK_END:
  61. $this->pointer = $this->length + $offset;
  62. break;
  63. }
  64. if ($this->pointer < 0 || $this->pointer >= $this->length) {
  65. $this->pointer = $crt;
  66. return false;
  67. }
  68. return true;
  69. }
  70. public function stream_tell()
  71. {
  72. return $this->pointer;
  73. }
  74. public static function register()
  75. {
  76. if (!static::$isRegistered) {
  77. static::$isRegistered = stream_wrapper_register(static::STREAM_PROTO, __CLASS__);
  78. }
  79. }
  80. }