generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Add StringStream
#85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add StringStream
#85
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5527322
Add `StreamMock`
vjik f2c176a
phpdoc
vjik de3e7f8
Apply fixes from StyleCI
StyleCIBot 1b612de
rename
vjik 9333177
Update docs/guide/en/README.md
vjik 6dbbf12
Update docs/guide/en/README.md
vjik de89fef
test
vjik ca98981
improve
vjik 1b0401e
improve
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Test\Support\HttpMessage; | ||
|
|
||
| use Closure; | ||
| use Psr\Http\Message\StreamInterface; | ||
| use RuntimeException; | ||
| use LogicException; | ||
|
|
||
| use function is_array; | ||
| use function strlen; | ||
|
|
||
| /** | ||
| * A test-specific implementation of PSR-7 stream. | ||
| * | ||
| * Allows creating stream instances with configurable behavior for testing HTTP message handling. | ||
| * | ||
| * @psalm-type MetadataClosure = Closure(StringStream): array<string, mixed> | ||
| */ | ||
| final class StringStream implements StreamInterface | ||
| { | ||
| private bool $closed = false; | ||
| private bool $detached = false; | ||
|
|
||
| /** | ||
| * @param string $content Initial stream content. | ||
| * @param int $position Initial position of the stream pointer. | ||
| * @param bool $readable Whether the stream is readable. | ||
| * @param bool $writable Whether the stream is writable. | ||
| * @param bool $seekable Whether the stream is seekable. | ||
| * @param array|Closure|null $metadata Custom metadata as an array or a closure that receives | ||
| * the stream instance and returns an array. | ||
| * | ||
| * @psalm-param MetadataClosure|array|null $metadata | ||
| */ | ||
| public function __construct( | ||
| private string $content = '', | ||
| private int $position = 0, | ||
| private bool $readable = true, | ||
| private bool $writable = true, | ||
| private bool $seekable = true, | ||
| private Closure|array|null $metadata = null, | ||
| ) { | ||
| $size = strlen($this->content); | ||
| if ($this->position < 0 || $this->position > $size) { | ||
| throw new LogicException( | ||
| sprintf('Position %d is out of valid range [0, %d].', $this->position, $size) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| return $this->content; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the stream has been closed. | ||
| */ | ||
| public function isClosed(): bool | ||
| { | ||
| return $this->closed; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the stream has been detached. | ||
| */ | ||
| public function isDetached(): bool | ||
| { | ||
| return $this->detached; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the current position of the stream pointer. | ||
| */ | ||
| public function getPosition(): int | ||
| { | ||
| return $this->position; | ||
| } | ||
|
|
||
| public function close(): void | ||
| { | ||
| $this->closed = true; | ||
| } | ||
|
|
||
| public function detach() | ||
| { | ||
| $this->detached = true; | ||
| $this->close(); | ||
| return null; | ||
| } | ||
|
|
||
| public function getSize(): ?int | ||
| { | ||
| return $this->getContentSize(); | ||
| } | ||
|
|
||
| public function tell(): int | ||
| { | ||
| if ($this->closed) { | ||
| throw new RuntimeException('Stream is closed.'); | ||
| } | ||
|
|
||
| return $this->position; | ||
| } | ||
|
|
||
| public function eof(): bool | ||
| { | ||
| return $this->closed || $this->position >= $this->getContentSize(); | ||
| } | ||
|
|
||
| public function isSeekable(): bool | ||
| { | ||
| return $this->seekable && !$this->closed; | ||
| } | ||
|
|
||
| public function seek(int $offset, int $whence = SEEK_SET): void | ||
| { | ||
| if (!$this->seekable) { | ||
| throw new RuntimeException('Stream is not seekable.'); | ||
| } | ||
|
|
||
| if ($this->closed) { | ||
| throw new RuntimeException('Stream is closed.'); | ||
| } | ||
|
|
||
| $size = $this->getContentSize(); | ||
|
|
||
| $newPosition = match ($whence) { | ||
| SEEK_SET => $offset, | ||
| SEEK_CUR => $this->position + $offset, | ||
| SEEK_END => $size + $offset, | ||
| default => throw new RuntimeException('Invalid whence value.'), | ||
| }; | ||
|
|
||
| if ($newPosition < 0 || $newPosition > $size) { | ||
| throw new RuntimeException('Invalid seek position.'); | ||
| } | ||
|
|
||
| $this->position = $newPosition; | ||
| } | ||
|
|
||
| public function rewind(): void | ||
| { | ||
| $this->seek(0); | ||
| } | ||
|
|
||
| public function isWritable(): bool | ||
| { | ||
| return $this->writable && !$this->closed; | ||
| } | ||
|
|
||
| public function write(string $string): int | ||
| { | ||
| if (!$this->writable) { | ||
| throw new RuntimeException('Stream is not writable.'); | ||
| } | ||
|
|
||
| if ($this->closed) { | ||
| throw new RuntimeException('Stream is closed.'); | ||
| } | ||
|
|
||
| $size = strlen($string); | ||
| $this->content = substr($this->content, 0, $this->position) | ||
| . $string | ||
| . substr($this->content, $this->position + $size); | ||
| $this->position = min($this->position + $size, $this->getContentSize()); | ||
|
|
||
| return $size; | ||
| } | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public function isReadable(): bool | ||
| { | ||
| return $this->readable && !$this->closed; | ||
| } | ||
|
|
||
| public function read(int $length): string | ||
| { | ||
| if (!$this->readable) { | ||
| throw new RuntimeException('Stream is not readable.'); | ||
| } | ||
|
|
||
| if ($this->closed) { | ||
| throw new RuntimeException('Stream is closed.'); | ||
| } | ||
|
|
||
| if ($length < 0) { | ||
| throw new RuntimeException('Length cannot be negative.'); | ||
| } | ||
|
|
||
| if ($this->position >= $this->getContentSize()) { | ||
|
Check warning on line 193 in src/HttpMessage/StringStream.php
|
||
| return ''; | ||
|
Check warning on line 194 in src/HttpMessage/StringStream.php
|
||
| } | ||
|
|
||
| $result = substr($this->content, $this->position, $length); | ||
| $this->position += strlen($result); | ||
|
|
||
| return $result; | ||
| } | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public function getContents(): string | ||
| { | ||
| return $this->read( | ||
| $this->getContentSize() - $this->position, | ||
|
Check warning on line 206 in src/HttpMessage/StringStream.php
|
||
| ); | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public function getMetadata(?string $key = null) | ||
| { | ||
| if ($this->closed) { | ||
| return $key === null ? [] : null; | ||
| } | ||
|
|
||
| $metadata = match (true) { | ||
| is_array($this->metadata) => $this->metadata, | ||
| $this->metadata instanceof Closure => ($this->metadata)($this), | ||
| default => [ | ||
| 'eof' => $this->eof(), | ||
| 'seekable' => $this->isSeekable(), | ||
| ], | ||
| }; | ||
|
|
||
| if ($key === null) { | ||
| return $metadata; | ||
| } | ||
|
|
||
| return $metadata[$key] ?? null; | ||
| } | ||
|
|
||
| private function getContentSize(): int | ||
| { | ||
| return strlen($this->content); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.