withOriginalWrapper(function () use ($path, $mode, $options, &$openedPath) { $handle = self::$previousWrapper === null ? $this->openNatively($path, $mode, $options) : $this->openThroughPreviousWrapper(self::$previousWrapper, $path, $mode, $options, $openedPath); if ($handle !== false && $this->isRead($mode, $options)) { self::$activeRecorder?->linkRead($path); } return $handle; }); if ($handle === false) { return false; } $this->handle = $handle; return true; } public function dir_closedir(): bool { $this->forward('dir_closedir', function (mixed $resource): bool { closedir($resource); return true; }); return true; } public function dir_opendir(string $path, int $options): bool { $handle = $this->withOriginalWrapper(fn (): mixed => self::$previousWrapper === null ? opendir($path, $this->context) : $this->openDirectoryThroughPreviousWrapper(self::$previousWrapper, $path, $options)); if ($handle === false) { return false; } $this->handle = $handle; return true; } public function dir_readdir(): string|false { $entry = $this->forward('dir_readdir', fn (mixed $resource): string|false => readdir($resource)); return is_string($entry) ? $entry : false; } public function dir_rewinddir(): bool { return $this->forward('dir_rewinddir', function (mixed $resource): bool { rewinddir($resource); return true; }) === true; } public function mkdir(string $path, int $mode, int $options): bool { return $this->withOriginalWrapper(fn (): bool => mkdir($path, $mode, ($options & STREAM_MKDIR_RECURSIVE) !== 0, $this->context)); } public function rename(string $pathFrom, string $pathTo): bool { return $this->withOriginalWrapper(fn (): bool => rename($pathFrom, $pathTo, $this->context)); } public function rmdir(string $path): bool { return $this->withOriginalWrapper(fn (): bool => rmdir($path, $this->context)); } /** * @return resource|false */ public function stream_cast(int $castAs): mixed { return $this->forward('stream_cast', fn (mixed $resource): mixed => $resource, $castAs); } public function stream_close(): void { $this->forward('stream_close', fn (mixed $resource): bool => fclose($resource)); } public function stream_eof(): bool { return $this->forward('stream_eof', fn (mixed $resource): bool => feof($resource)) === true; } public function stream_flush(): bool { return $this->forward('stream_flush', fn (mixed $resource): bool => fflush($resource)) === true; } /** * @param int<0, 7> $operation */ public function stream_lock(int $operation): bool { if (($operation & ~LOCK_NB) === 0) { return true; } return $this->forward('stream_lock', fn (mixed $resource): bool => flock($resource, $operation), $operation) === true; } public function stream_metadata(string $path, int $option, mixed $value): bool { return $this->withOriginalWrapper(fn (): bool => match ($option) { STREAM_META_TOUCH => is_array($value) && is_int($value[0] ?? null) && is_int($value[1] ?? null) ? touch($path, $value[0], $value[1]) : touch($path), STREAM_META_OWNER_NAME, STREAM_META_OWNER => (is_string($value) || is_int($value)) && chown($path, $value), STREAM_META_GROUP_NAME, STREAM_META_GROUP => (is_string($value) || is_int($value)) && chgrp($path, $value), STREAM_META_ACCESS => is_int($value) && chmod($path, $value), default => false, }); } /** * @param int<1, max> $count */ public function stream_read(int $count): string|false { $data = $this->forward('stream_read', fn (mixed $resource): string|false => fread($resource, $count), $count); return is_string($data) ? $data : false; } public function stream_seek(int $offset, int $whence = SEEK_SET): bool { return $this->forward('stream_seek', fn (mixed $resource): bool => fseek($resource, $offset, $whence) === 0, $offset, $whence) === true; } public function stream_set_option(int $option, int $arg1, ?int $arg2): bool { return $this->forward('stream_set_option', fn (mixed $resource): bool => match ($option) { STREAM_OPTION_BLOCKING => stream_set_blocking($resource, (bool) $arg1), STREAM_OPTION_READ_TIMEOUT => stream_set_timeout($resource, $arg1, $arg2 ?? 0), STREAM_OPTION_WRITE_BUFFER => stream_set_write_buffer($resource, $arg1) === 0, STREAM_OPTION_READ_BUFFER => stream_set_read_buffer($resource, $arg1) === 0, default => false, }, $option, $arg1, $arg2) === true; } /** * @return array|false */ public function stream_stat(): array|false { $stat = $this->forward('stream_stat', fn (mixed $resource): array|false => fstat($resource)); return is_array($stat) ? $stat : false; } public function stream_tell(): int|false { $position = $this->forward('stream_tell', fn (mixed $resource): int|false => ftell($resource)); return is_int($position) ? $position : false; } /** * @param int<0, max> $newSize */ public function stream_truncate(int $newSize): bool { return $this->forward('stream_truncate', fn (mixed $resource): bool => ftruncate($resource, $newSize), $newSize) === true; } public function stream_write(string $data): int|false { $written = $this->forward('stream_write', fn (mixed $resource): int|false => fwrite($resource, $data), $data); return is_int($written) ? $written : false; } public function unlink(string $path): bool { return $this->withOriginalWrapper(fn (): bool => unlink($path, $this->context)); } /** * @return array|false */ public function url_stat(string $path, int $flags): array|false { return $this->withOriginalWrapper(function () use ($path, $flags): array|false { set_error_handler(static fn (): bool => true); try { return ($flags & STREAM_URL_STAT_LINK) !== 0 ? lstat($path) : stat($path); } finally { restore_error_handler(); } }); } /** * @return resource|false */ private function openNatively(string $path, string $mode, int $options): mixed { return ($options & (STREAM_REPORT_ERRORS | self::STREAM_OPEN_FOR_INCLUDE)) !== 0 ? fopen($path, $mode, ($options & STREAM_USE_PATH) !== 0, $this->context) : @fopen($path, $mode, ($options & STREAM_USE_PATH) !== 0, $this->context); } /** * @param class-string $wrapperClass */ private function openThroughPreviousWrapper(string $wrapperClass, string $path, string $mode, int $options, ?string &$openedPath): object|false { $wrapper = new $wrapperClass; if (property_exists($wrapper, 'context')) { $wrapper->context = $this->context; } if (! is_callable([$wrapper, 'stream_open']) || $wrapper->stream_open($path, $mode, $options, $openedPath) !== true) { return false; } return $wrapper; } /** * @param class-string $wrapperClass */ private function openDirectoryThroughPreviousWrapper(string $wrapperClass, string $path, int $options): object|false { $wrapper = new $wrapperClass; if (property_exists($wrapper, 'context')) { $wrapper->context = $this->context; } if (! is_callable([$wrapper, 'dir_opendir']) || $wrapper->dir_opendir($path, $options) !== true) { return false; } return $wrapper; } private function isRead(string $mode, int $options): bool { if (($options & self::STREAM_OPEN_FOR_INCLUDE) !== 0) { return false; } return str_contains($mode, 'r') || str_contains($mode, '+'); } /** * @param Closure(resource): mixed $native */ private function forward(string $method, Closure $native, mixed ...$arguments): mixed { if (! is_object($this->handle)) { return $native($this->handle); } if (! is_callable([$this->handle, $method])) { return false; } return $this->handle->{$method}(...$arguments); } /** * @template T * * @param Closure(): T $callback * @return T */ private function withOriginalWrapper(Closure $callback): mixed { self::disable(); try { return $callback(); } finally { self::enable(); } } }