| 1 |
<?php |
| 2 |
|
| 3 |
namespace Laravel\SerializableClosure\Support; |
| 4 |
|
| 5 |
#[\AllowDynamicProperties] |
| 6 |
class ClosureStream |
| 7 |
{ |
| 8 |
/** |
| 9 |
* The stream protocol. |
| 10 |
*/ |
| 11 |
const STREAM_PROTO = 'laravel-serializable-closure'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Checks if this stream is registered. |
| 15 |
* |
| 16 |
* @var bool |
| 17 |
*/ |
| 18 |
protected static $isRegistered = false; |
| 19 |
|
| 20 |
/** |
| 21 |
* The stream content. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $content; |
| 26 |
|
| 27 |
/** |
| 28 |
* The stream content. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
protected $length; |
| 33 |
|
| 34 |
/** |
| 35 |
* The stream pointer. |
| 36 |
* |
| 37 |
* @var int |
| 38 |
*/ |
| 39 |
protected $pointer = 0; |
| 40 |
|
| 41 |
/** |
| 42 |
* Opens file or URL. |
| 43 |
* |
| 44 |
* @param string $path |
| 45 |
* @param string $mode |
| 46 |
* @param string $options |
| 47 |
* @param string|null $opened_path |
| 48 |
* @return bool |
| 49 |
*/ |
| 50 |
public function stream_open($path, $mode, $options, &$opened_path) |
| 51 |
{ |
| 52 |
$this->content = "<?php\nreturn ".substr($path, strlen(static::STREAM_PROTO.'://')).';'; |
| 53 |
$this->length = strlen($this->content); |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Read from stream. |
| 60 |
* |
| 61 |
* @param int $count |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function stream_read($count) |
| 65 |
{ |
| 66 |
$value = substr($this->content, $this->pointer, $count); |
| 67 |
|
| 68 |
$this->pointer += $count; |
| 69 |
|
| 70 |
return $value; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Tests for end-of-file on a file pointer. |
| 75 |
* |
| 76 |
* @return bool |
| 77 |
*/ |
| 78 |
public function stream_eof() |
| 79 |
{ |
| 80 |
return $this->pointer >= $this->length; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Change stream options. |
| 85 |
* |
| 86 |
* @param int $option |
| 87 |
* @param int $arg1 |
| 88 |
* @param int $arg2 |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
public function stream_set_option($option, $arg1, $arg2) |
| 92 |
{ |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Retrieve information about a file resource. |
| 98 |
* |
| 99 |
* @return array|bool |
| 100 |
*/ |
| 101 |
public function stream_stat() |
| 102 |
{ |
| 103 |
$stat = stat(__FILE__); |
| 104 |
// @phpstan-ignore-next-line |
| 105 |
$stat[7] = $stat['size'] = $this->length; |
| 106 |
|
| 107 |
return $stat; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Retrieve information about a file. |
| 112 |
* |
| 113 |
* @param string $path |
| 114 |
* @param int $flags |
| 115 |
* @return array|bool |
| 116 |
*/ |
| 117 |
public function url_stat($path, $flags) |
| 118 |
{ |
| 119 |
$stat = stat(__FILE__); |
| 120 |
// @phpstan-ignore-next-line |
| 121 |
$stat[7] = $stat['size'] = $this->length; |
| 122 |
|
| 123 |
return $stat; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Seeks to specific location in a stream. |
| 128 |
* |
| 129 |
* @param int $offset |
| 130 |
* @param int $whence |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public function stream_seek($offset, $whence = SEEK_SET) |
| 134 |
{ |
| 135 |
$crt = $this->pointer; |
| 136 |
|
| 137 |
switch ($whence) { |
| 138 |
case SEEK_SET: |
| 139 |
$this->pointer = $offset; |
| 140 |
break; |
| 141 |
case SEEK_CUR: |
| 142 |
$this->pointer += $offset; |
| 143 |
break; |
| 144 |
case SEEK_END: |
| 145 |
$this->pointer = $this->length + $offset; |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
if ($this->pointer < 0 || $this->pointer >= $this->length) { |
| 150 |
$this->pointer = $crt; |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Retrieve the current position of a stream. |
| 160 |
* |
| 161 |
* @return int |
| 162 |
*/ |
| 163 |
public function stream_tell() |
| 164 |
{ |
| 165 |
return $this->pointer; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Registers the stream. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public static function register() |
| 174 |
{ |
| 175 |
if (! static::$isRegistered) { |
| 176 |
static::$isRegistered = stream_wrapper_register(static::STREAM_PROTO, __CLASS__); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|