[HttpFoundation] factorize out the way output buffers should be closed

This commit is contained in:
Nicolas Grekas 2014-04-17 09:51:05 +02:00
parent f72eb34fc4
commit 469943cd1c
3 changed files with 37 additions and 31 deletions

View File

@ -70,17 +70,13 @@ class ExceptionController
*/ */
protected function getAndCleanOutputBuffering($startObLevel) protected function getAndCleanOutputBuffering($startObLevel)
{ {
// ob_get_level() never returns 0 on some Windows configurations, so if if (ob_get_level() <= $startObLevel) {
// the level is the same two times in a row, the loop should be stopped. return '';
$previousObLevel = null;
$currentContent = '';
while (($obLevel = ob_get_level()) > $startObLevel && $obLevel !== $previousObLevel) {
$previousObLevel = $obLevel;
$currentContent .= ob_get_clean();
} }
return $currentContent; Response::closeOutputBuffers($startObLevel + 1, true);
return ob_get_clean();
} }
/** /**

View File

@ -376,24 +376,7 @@ class Response
if (function_exists('fastcgi_finish_request')) { if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request(); fastcgi_finish_request();
} elseif ('cli' !== PHP_SAPI) { } elseif ('cli' !== PHP_SAPI) {
// ob_get_level() never returns 0 on some Windows configurations, so if static::closeOutputBuffers(0, true);
// the level is the same two times in a row, the loop should be stopped.
$previous = null;
$obStatus = ob_get_status(1);
while (($level = ob_get_level()) > 0 && $level !== $previous) {
$previous = $level;
if ($obStatus[$level - 1]) {
if (version_compare(PHP_VERSION, '5.4', '>=')) {
if (isset($obStatus[$level - 1]['flags']) && ($obStatus[$level - 1]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE)) {
ob_end_flush();
}
} else {
if (isset($obStatus[$level - 1]['del']) && $obStatus[$level - 1]['del']) {
ob_end_flush();
}
}
}
}
flush(); flush();
} }
@ -1247,7 +1230,36 @@ class Response
} }
/** /**
* Check if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9 * Cleans or flushes output buffers up to target level.
*
* Resulting level can be greater than target level if a non-removable buffer has been encountered.
*
* @param int $targetLevel The target output buffering level
* @param bool $flush Whether to flush or clean the buffers
*/
public static function closeOutputBuffers($targetLevel, $flush)
{
$status = ob_get_status(true);
$level = count($status);
while ($level-- > $targetLevel
&& (!empty($status[$level]['del'])
|| (isset($status[$level]['flags'])
&& ($status[$level]['flags'] & PHP_OUTPUT_HANDLER_REMOVABLE)
&& ($status[$level]['flags'] & ($flush ? PHP_OUTPUT_HANDLER_FLUSHABLE : PHP_OUTPUT_HANDLER_CLEANABLE))
)
)
) {
if ($flush) {
ob_end_flush();
} else {
ob_end_clean();
}
}
}
/**
* Checks if we need to remove Cache-Control for SSL encrypted downloads when using IE < 9
* *
* @link http://support.microsoft.com/kb/323308 * @link http://support.microsoft.com/kb/323308
*/ */

View File

@ -93,9 +93,7 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
} }
// let's clean up the output buffers that were created by the sub-request // let's clean up the output buffers that were created by the sub-request
while (ob_get_level() > $level) { Response::closeOutputBuffers($level, false);
ob_get_clean();
}
if (isset($options['alt'])) { if (isset($options['alt'])) {
$alt = $options['alt']; $alt = $options['alt'];