src/ClosureStream.php source

1 <?php
2
3 declare(strict_types=1);
4
5 namespace Serializor;
6
7 /**
8 * @internal
9 */
10 class ClosureStream
11 {
12 const STREAM_PROTO = 'serializor';
13
14 const STAT_BASE = [
15 0 => 51,
16 1 => 4405873,
17 2 => 33204,
18 3 => 1,
19 4 => 1011,
20 5 => 1011,
21 6 => 0,
22 7 => 2145,
23 8 => 1725454294,
24 9 => 1725454294,
25 10 => 1725454294,
26 11 => 4096,
27 12 => 8,
28 'dev' => 51,
29 'ino' => 4405873,
30 'mode' => 33204,
31 'nlink' => 1,
32 'uid' => 1011,
33 'gid' => 1011,
34 'rdev' => 0,
35 'size' => 2145,
36 'atime' => 1725454294,
37 'mtime' => 1725454294,
38 'ctime' => 1725454294,
39 'blksize' => 4096,
40 'blocks' => 8,
41 ];
42
43 protected static $isRegistered = false;
44
45 protected $content;
46
47 protected $length;
48
49 protected $pointer = 0;
50
51 public $context;
52
53 function stream_open($path, $mode, $options, &$opened_path)
54 {
55 $this->content = "<?php\n" . substr($path, strlen(static::STREAM_PROTO . '://')) . ";";
56 $this->length = strlen($this->content);
57 return true;
58 }
59
60 public function stream_read($count)
61 {
62 $value = substr($this->content, $this->pointer, $count);
63 $this->pointer += $count;
64 return $value;
65 }
66
67 public function stream_eof()
68 {
69 return $this->pointer >= $this->length;
70 }
71
72 public function stream_set_option($option, $arg1, $arg2)
73 {
74 return false;
75 }
76
77 public function stream_stat()
78 {
79 $stat = self::STAT_BASE;
80 $stat[7] = $stat['size'] = $this->length;
81 return $stat;
82 }
83
84 public function url_stat($path, $flags)
85 {
86 $stat = self::STAT_BASE;
87 $stat[7] = $stat['size'] = $this->length;
88 return $stat;
89 }
90
91 public function stream_seek($offset, $whence = SEEK_SET)
92 {
93 $crt = $this->pointer;
94
95 switch ($whence) {
96 case SEEK_SET:
97 $this->pointer = $offset;
98 break;
99 case SEEK_CUR:
100 $this->pointer += $offset;
101 break;
102 case SEEK_END:
103 $this->pointer = $this->length + $offset;
104 break;
105 }
106
107 if ($this->pointer < 0 || $this->pointer >= $this->length) {
108 $this->pointer = $crt;
109 return false;
110 }
111
112 return true;
113 }
114
115 public function stream_tell()
116 {
117 return $this->pointer;
118 }
119
120 public static function register()
121 {
122 if (!static::$isRegistered) {
123 static::$isRegistered = stream_wrapper_register(static::STREAM_PROTO, __CLASS__);
124 }
125 }
126 }
127