if (! $this->isSeekable()) { throw Exception\UnseekableStreamException::dueToConfiguration(); } $result = fseek($this->resource, $offset, $whence); if (0 !== $result) { throw Exception\UnseekableStreamException::dueToPhpError(); } } /** * {@inheritdoc} */ #[Override] public function rewind(): void { $this->seek(0); } /** * {@inheritdoc} */ #[Override] public function isWritable(): bool { if (! $this->resource) { return false; } $meta = stream_get_meta_data($this->resource); $mode = $meta['mode']; return str_contains($mode, 'x') || str_contains($mode, 'w') || str_contains($mode, 'c') || str_contains($mode, 'a') || str_contains($mode, '+'); } /** * {@inheritdoc} */ #[Override] public function write($string): int { if (! $this->resource) { throw Exception\UnwritableStreamException::dueToMissingResource(); } if (! $this->isWritable()) { throw Exception\UnwritableStreamException::dueToConfiguration(); } $result = fwrite($this->resource, $string); if (false === $result) { throw Exception\UnwritableStreamException::dueToPhpError(); } return $result; } /** * {@inheritdoc} */ #[Override] public function isReadable(): bool { if (! $this->resource) { return false; } $meta = stream_get_meta_data($this->resource); $mode = $meta['mode']; return str_contains($mode, 'r') || str_contains($mode, '+'); } /** * {@inheritdoc} */ #[Override] public function read(int $length): string { if (! $this->resource) { throw Exception\UnreadableStreamException::dueToMissingResource(); } if (! $this->isReadable()) { throw Exception\UnreadableStreamException::dueToConfiguration(); } $result = fread($this->resource, $length); if (false === $result) { throw Exception\UnreadableStreamException::dueToPhpError(); } return $result; } /** * {@inheritdoc} */ #[Override] public function getContents(): string { if (! $this->isReadable()) { throw Exception\UnreadableStreamException::dueToConfiguration(); } assert($this->resource !== null, 'Always true condition for psalm type safety'); $result = stream_get_contents($this->resource); if (false === $result) { throw Exception\UnreadableStreamException::dueToPhpError(); } return $result; } /** * {@inheritdoc} */ #[Override] public function getMetadata(?string $key = null) { $metadata = []; if (null !== $this->resource) { $metadata = stream_get_meta_data($this->resource); } if (null === $key) { return $metadata; } if (! array_key_exists($key, $metadata)) { return null; } return $metadata[$key]; } /** * Set the internal stream resource. * * @param string|object|resource $stream String stream target or stream resource. * @param string $mode Resource mode for stream target. * @throws Exception\InvalidArgumentException For invalid streams or resources. */ private function setStream($stream, string $mode = 'r'): void { $error = null; $resource = $stream; if (is_string($stream)) { try { $resource = fopen($stream, $mode); } catch (Throwable $error) { } if (! is_resource($resource)) { throw new Exception\RuntimeException( sprintf( 'Empty or non-existent stream identifier or file path provided: "%s"', $stream, ), 0, $error ); } } if (! $this->isValidStreamResourceType($resource)) { throw new Exception\InvalidArgumentException( 'Invalid stream provided; must be a string stream identifier or stream resource' ); } if ($stream !== $resource) { $this->stream = $stream; } $this->resource = $resource; } /** * Determine if a resource is one of the resource types allowed to instantiate a Stream * * @param mixed $resource Stream resource. * @psalm-assert-if-true resource $resource */ private function isValidStreamResourceType(mixed $resource): bool { if (is_resource($resource)) { return in_array(get_resource_type($resource), self::ALLOWED_STREAM_RESOURCE_TYPES, true); } return false; } }
The server returned a "500 - Whoops, looks like something went wrong."