Create method cleanOutputBuffers() and use it

This commit is contained in:
Matias Griese
2022-03-23 10:49:19 +02:00
parent 62d9db7650
commit c9271c80a7
2 changed files with 27 additions and 15 deletions

View File

@@ -341,6 +341,25 @@ class Grav extends Container
}
}
/**
* Clean any output buffers. Useful when exiting from the application.
*
* Please use $grav->close() and $grav->redirect() instead of calling this one!
*
* @return void
*/
public function cleanOutputBuffers(): void
{
/** @var Config $config */
$config = $this['config'];
$gzip_enabled = (int) $config->get('system.cache.gzip');
// Make sure nothing extra gets written to the response.
while (ob_get_level() > 2 + $gzip_enabled) {
ob_end_clean();
}
}
/**
* Terminates Grav request with a response.
*
@@ -351,11 +370,7 @@ class Grav extends Container
*/
public function close(ResponseInterface $response): void
{
$gzip_enabled = (int) Grav::instance()['config']->get('system.cache.gzip');
// Make sure nothing extra gets written to the response.
while (ob_get_level() > 2 + $gzip_enabled) {
ob_end_clean();
}
$this->cleanOutputBuffers();
// Close the session.
if (isset($this['session'])) {
@@ -401,7 +416,7 @@ class Grav extends Container
/**
* @param ResponseInterface $response
* @return never-return
* @deprecated 1.7 Do not use
* @deprecated 1.7 Use $grav->close() instead.
*/
public function exit(ResponseInterface $response): void
{

View File

@@ -668,20 +668,17 @@ abstract class Utils
*/
public static function download($file, $force_download = true, $sec = 0, $bytes = 1024, array $options = [])
{
$grav = Grav::instance();
if (file_exists($file)) {
// fire download event
Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file, 'options' => &$options]));
$grav->fireEvent('onBeforeDownload', new Event(['file' => $file, 'options' => &$options]));
$file_parts = static::pathinfo($file);
$mimetype = $options['mime'] ?? static::getMimeByExtension($file_parts['extension']);
$size = filesize($file); // File size
$gzip_enabled = (int) Grav::instance()['config']->get('system.cache.gzip');
// clean all buffers
while (ob_get_level() > 2 + $gzip_enabled) {
ob_end_clean();
}
$grav->cleanOutputBuffers();
// required for IE, otherwise Content-Disposition may be ignored
if (ini_get('zlib.output_compression')) {
@@ -716,8 +713,8 @@ abstract class Utils
$new_length = $size;
header('Content-Length: ' . $size);
if (Grav::instance()['config']->get('system.cache.enabled')) {
$expires = $options['expires'] ?? Grav::instance()['config']->get('system.pages.expires');
if ($grav['config']->get('system.cache.enabled')) {
$expires = $options['expires'] ?? $grav['config']->get('system.pages.expires');
if ($expires > 0) {
$expires_date = gmdate('D, d M Y H:i:s T', time() + $expires);
header('Cache-Control: max-age=' . $expires);