Skip to content

Commit

Permalink
Merge pull request #9 from harikt/callback-stream
Browse files Browse the repository at this point in the history
Add callbackstream
  • Loading branch information
harikt committed Feb 17, 2017
2 parents 7252a0c + c7b2507 commit 5412d41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/AssetResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
namespace Hkt\Psr7Asset;

use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Stream;
use Zend\Diactoros\CallbackStream;
use SplFileObject;

/**
*
Expand Down Expand Up @@ -95,9 +96,18 @@ protected function isValidAsset()
*/
protected function ok(ResponseInterface $response)
{
$path = $this->data->asset->path;
$callable = function () use ($path) {
$file = new SplFileObject($path);
while (! $file->eof()) {
echo $file->fgets();
}

return '';
};
return $response
->withStatus(200)
->withBody(new Stream($this->data->asset->path))
->withBody(new CallbackStream($callable))
->withHeader('Content-Length', (string) filesize($this->data->asset->path))
->withHeader('Content-Type', $this->data->asset->type)
;
Expand Down
6 changes: 5 additions & 1 deletion tests/src/AssetResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public function test__invoke_Ok()
$actual = $response->getHeaderLine('Content-Type');
$this->assertSame($type, $actual);

$actual = (string) $response->getBody();
ob_start();
(string) $response->getBody();
$actual = ob_get_contents();
ob_end_clean();

$expect = file_get_contents($path);
$this->assertSame($expect, $actual);
}
Expand Down

0 comments on commit 5412d41

Please sign in to comment.