Skip to content

Commit

Permalink
optimize streamed response
Browse files Browse the repository at this point in the history
  • Loading branch information
hhxsv5 committed Jun 14, 2019
1 parent 27f4df6 commit 9b17c07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
22 changes: 3 additions & 19 deletions src/Illuminate/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Http\Request as IlluminateRequest;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class Laravel
{
Expand Down Expand Up @@ -144,10 +143,7 @@ public function handleDynamic(IlluminateRequest $request)

if ($this->isLumen()) {
$response = $this->app->dispatch($request);
if ($response instanceof StreamedResponse) {
$content = '';
$response = $response->sendContent();
} elseif ($response instanceof SymfonyResponse) {
if ($response instanceof SymfonyResponse) {
$content = $response->getContent();
} else {
$content = (string)$response;
Expand All @@ -156,25 +152,13 @@ public function handleDynamic(IlluminateRequest $request)
$this->reflectionApp->callTerminableMiddleware($response);
} else {
$response = $this->kernel->handle($request);
if ($response instanceof StreamedResponse) {
$content = '';
$response = $response->sendContent();
} else {
$content = $response->getContent();
}
$content = $response->getContent();
$this->kernel->terminate($request, $response);
}

// prefer content in response, secondly ob
if (strlen($content) === 0 && ob_get_length() > 0) {
if ($response instanceof StreamedResponse) {
$reflect = new \ReflectionClass(StreamedResponse::class);
$contentProperty = $reflect->getProperty('content');
$contentProperty->setAccessible(true);
$contentProperty->setValue($response, ob_get_contents());
} else {
$response->setContent(ob_get_contents());
}
$response->setContent(ob_get_contents());
}

ob_end_clean();
Expand Down
7 changes: 3 additions & 4 deletions src/Swoole/DynamicResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ public function gzip()
public function sendContent()
{
if ($this->laravelResponse instanceof StreamedResponse) {
$reflect = new \ReflectionClass(StreamedResponse::class);
$contentProperty = $reflect->getProperty('content');
$contentProperty->setAccessible(true);
$content = $contentProperty->getValue($this->laravelResponse);
ob_start();
$this->laravelResponse = $this->laravelResponse->sendContent();
$content = ob_get_clean();
} else {
$content = $this->laravelResponse->getContent();
}
Expand Down

0 comments on commit 9b17c07

Please sign in to comment.