Skip to content

Commit

Permalink
Merge e9b9578 into f6884a7
Browse files Browse the repository at this point in the history
  • Loading branch information
Rezouce committed Nov 18, 2017
2 parents f6884a7 + e9b9578 commit 03c5cbd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 130 deletions.
20 changes: 18 additions & 2 deletions Slim/App.php
Expand Up @@ -292,11 +292,29 @@ public function run($silent = false)
$response = $this->container->get('response');

try {
ob_start();
$response = $this->process($this->container->get('request'), $response);
} catch (InvalidMethodException $e) {
$response = $this->processInvalidMethod($e->getRequest(), $response);
} finally {
$output = ob_get_clean();
}

if (!empty($output) && $response->getBody()->isWritable()) {
$outputBuffering = $this->container->get('settings')['outputBuffering'];
if ($outputBuffering === 'prepend') {
// prepend output buffer content
$body = new Http\Body(fopen('php://temp', 'r+'));
$body->write($output . $response->getBody());
$response = $response->withBody($body);
} elseif ($outputBuffering === 'append') {
// append output buffer content
$response->getBody()->write($output);
}
}

$response = $this->finalize($response);

if (!$silent) {
$this->respond($response);
}
Expand Down Expand Up @@ -374,8 +392,6 @@ public function process(ServerRequestInterface $request, ResponseInterface $resp
$response = $this->handlePhpError($e, $request, $response);
}

$response = $this->finalize($response);

return $response;
}

Expand Down
14 changes: 1 addition & 13 deletions Slim/Handlers/Error.php
Expand Up @@ -55,19 +55,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$this->writeToErrorLog($exception);

$body = new Body(fopen('php://temp', 'r+'));

if ($this->outputBuffering === 'prepend') {
// prepend output buffer content
$body->write(ob_get_clean() . $output);
} elseif ($this->outputBuffering === 'append') {
// append output buffer content
$body->write($output . ob_get_clean());
} else {
// outputBuffering is false or some other unknown setting
// delete anything in the output buffer.
ob_get_clean();
$body->write($output);
}
$body->write($output);

return $response
->withStatus(500)
Expand Down
32 changes: 1 addition & 31 deletions Slim/Route.php
Expand Up @@ -8,12 +8,9 @@
*/
namespace Slim;

use Exception;
use Throwable;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\Exception\SlimException;
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Interfaces\InvocationStrategyInterface;
use Slim\Interfaces\RouteInterface;
Expand Down Expand Up @@ -335,22 +332,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
/** @var InvocationStrategyInterface $handler */
$handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();

// invoke route callable
if ($this->outputBuffering === false) {
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
} else {
try {
ob_start();
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
$output = ob_get_clean();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
throw $e;
// @codeCoverageIgnoreEnd
} catch (Exception $e) {
throw $e;
}
}
$newResponse = $handler($this->callable, $request, $response, $this->arguments);

if ($newResponse instanceof ResponseInterface) {
// if route callback returns a ResponseInterface, then use it
Expand All @@ -362,18 +344,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
}
}

if (!empty($output) && $response->getBody()->isWritable()) {
if ($this->outputBuffering === 'prepend') {
// prepend output buffer content
$body = new Http\Body(fopen('php://temp', 'r+'));
$body->write($output . $response->getBody());
$response = $response->withBody($body);
} elseif ($this->outputBuffering === 'append') {
// append output buffer content
$response->getBody()->write($output);
}
}

return $response;
}
}
34 changes: 4 additions & 30 deletions tests/AppTest.php
Expand Up @@ -2258,26 +2258,13 @@ public function testExceptionOutputBufferingAppend()
$app = $this->appFactory();
$app->getContainer()['settings']['outputBuffering'] = 'append';
$app->get("/foo", function ($request, $response, $args) {
$test = [1,2,3];
var_dump($test);
echo 'output buffer test';
throw new \Exception("oops");
});

$expectedOutput = <<<end
array(3) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
}
end;

$resOut = $app->run(true);
$output = (string)$resOut->getBody();
$strPos = strpos($output, $expectedOutput);
$this->assertNotFalse($strPos);
$this->assertStringEndsWith('output buffer test', $output);
}

public function testExceptionOutputBufferingPrepend()
Expand All @@ -2290,26 +2277,13 @@ public function testExceptionOutputBufferingPrepend()
$app = $this->appFactory();
$app->getContainer()['settings']['outputBuffering'] = 'prepend';
$app->get("/foo", function ($request, $response, $args) {
$test = [1,2,3];
var_dump($test);
echo 'output buffer test';
throw new \Exception("oops");
});

$expectedOutput = <<<end
array(3) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
}
end;

$resOut = $app->run(true);
$output = (string)$resOut->getBody();
$strPos = strpos($output, $expectedOutput);
$this->assertNotFalse($strPos);
$this->assertStringStartsWith('output buffer test', $output);
}

protected function skipIfPhp70()
Expand Down
54 changes: 0 additions & 54 deletions tests/RouteTest.php
Expand Up @@ -275,33 +275,6 @@ public function testInvokeWhenReturningAResponse()
$this->assertEquals('foo', (string)$response->getBody());
}

/**
* Ensure that anything echo'd in a route callable is added to the response
* object that is returned by __invoke().
*/
public function testInvokeWhenEchoingOutput()
{
$callable = function ($req, $res, $args) {
echo "foo";
return $res->withStatus(201);
};
$route = new Route(['GET'], '/', $callable);

$env = Environment::mock();
$uri = Uri::createFromString('https://example.com:80');
$headers = new Headers();
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$response = new Response;

$response = $route->__invoke($request, $response);

$this->assertEquals('foo', (string)$response->getBody());
$this->assertEquals(201, $response->getStatusCode());
}

/**
* Ensure that if a string is returned by a route callable, then it is
* added to the response object that is returned by __invoke().
Expand All @@ -327,33 +300,6 @@ public function testInvokeWhenReturningAString()
$this->assertEquals('foo', (string)$response->getBody());
}

/**
* Ensure that if `outputBuffering` property is set to `prepend` correct response
* body is returned by __invoke().
*/
public function testInvokeWhenPrependingOutputBuffer()
{
$callable = function ($req, $res, $args) {
echo 'foo';
return $res->write('bar');
};
$route = new Route(['GET'], '/', $callable);
$route->setOutputBuffering('prepend');

$env = Environment::mock();
$uri = Uri::createFromString('https://example.com:80');
$headers = new Headers();
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$response = new Response;

$response = $route->__invoke($request, $response);

$this->assertEquals('foobar', (string)$response->getBody());
}

/**
* @expectedException \Exception
*/
Expand Down

0 comments on commit 03c5cbd

Please sign in to comment.