Slim PHP 代码库中使用的缓冲函数的目的是什么?
What is the purpose of the buffer functions used in Slim PHP codebase?
出于教育目的,我正在学习 slim php 的代码库,通过阅读它我明白了很多。但是,我发现很难理解 App class.
的 'main' 运行 方法中使用的缓冲区的用途
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);
}
return $response;
}
我试图转储 ob_get_clean() 的值,但它始终为空。
这样做是为了始终 return 一个 PSR-7 Response。如果 echo
或 print_r()
在 routes/middleware 中,如果 outputBuffering
设置设置为 prepend
或者设置为 append
它将被追加。
出于教育目的,我正在学习 slim php 的代码库,通过阅读它我明白了很多。但是,我发现很难理解 App class.
的 'main' 运行 方法中使用的缓冲区的用途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);
}
return $response;
}
我试图转储 ob_get_clean() 的值,但它始终为空。
这样做是为了始终 return 一个 PSR-7 Response。如果 echo
或 print_r()
在 routes/middleware 中,如果 outputBuffering
设置设置为 prepend
或者设置为 append
它将被追加。