Skip to content

Commit

Permalink
feat(Response): Add rawStream method for temporary resources
Browse files Browse the repository at this point in the history
- Introduce rawStream() method to create a temporary resource
- Useful for storing the stream and ensures its closure after use
- Throws LogicException if resource creation fails
- Enhances the flexibility of handling streams
  • Loading branch information
guanguans committed Nov 15, 2024
1 parent 963c535 commit e3ded5b
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/Foundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ public function body(): string
return (string) $this->getBody();
}

/**
* Get the body as a stream.
*/
public function stream(): StreamInterface
{
$stream = $this->getBody();

if ($stream->isSeekable()) {
$stream->rewind();
}

return $stream;
}

/**
* Get the body of the response as a PHP resource.
* Useful for storing the file.
* Make sure to close the [guzzle://stream] resource after you have used it.
*
* @throws \InvalidArgumentException
*
* @return resource
*/
public function resource()
{
return StreamWrapper::getResource($this->getBody());
}

/**
* Get the JSON decoded body of the response as an array or scalar value.
*
Expand Down Expand Up @@ -205,32 +233,6 @@ public function dataUrl(): string
return \sprintf('data:%s;base64,%s', $this->getHeaderLine('Content-Type'), base64_encode($this->body()));
}

/**
* Get the body as a stream.
*/
public function stream(): StreamInterface
{
$stream = $this->getBody();

if ($stream->isSeekable()) {
$stream->rewind();
}

return $stream;
}

/**
* Get the body of the response as a PHP resource.
*
* @throws \InvalidArgumentException
*
* @return resource
*/
public function resource()
{
return StreamWrapper::getResource($this->getBody());
}

/**
* Save the response to resource or file.
*
Expand Down

0 comments on commit e3ded5b

Please sign in to comment.