Skip to content

Instantly share code, notes, and snippets.

@kemo
Last active July 16, 2018 06:47
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kemo/2881489 to your computer and use it in GitHub Desktop.
Save kemo/2881489 to your computer and use it in GitHub Desktop.
Kohana 2 ob_end_clean() hotfix (PHP 5.4+)
diff --git a/system/core/Kohana.php b/system/core/Kohana.php
index 271f917..287c271 100644
--- a/system/core/Kohana.php
+++ b/system/core/Kohana.php
@@ -722,7 +722,7 @@ final class Kohana {
if (ob_get_level() >= self::$buffer_level)
{
// Set the close function
- $close = ($flush === TRUE) ? 'ob_end_flush' : 'ob_end_clean';
+ $close = ($flush === TRUE) ? 'ob_end_flush' : 'Kohana::_ob_end_clean';
while (ob_get_level() > self::$buffer_level)
{
@@ -731,7 +731,7 @@ final class Kohana {
}
// Store the Kohana output buffer
- ob_end_clean();
+ Kohana::_ob_end_clean();
}
}
@@ -2173,6 +2173,31 @@ final class Kohana {
return $written;
}
+
+ /**
+ * Ends the current output buffer with callback in mind
+ * PHP doesn't pass the output to the callback defined in ob_start() since 5.4
+ *
+ * @param callback $callback
+ * @return boolean
+ */
+ protected static function _ob_end_clean($callback = NULL)
+ {
+ // Pre-5.4 ob_end_clean() will pass the buffer to the callback anyways
+ if (version_compare(PHP_VERSION, '5.4', '<'))
+ return ob_end_clean();
+
+ $output = ob_get_contents();
+
+ if ($callback === NULL)
+ {
+ $handlers = ob_list_handlers();
+ $callback = isset($handlers[ob_get_level() - 1]) ? $handlers[ob_get_level() - 1] : NULL;
+ }
+
+ return is_callable($callback)
+ ? ob_end_clean() AND call_user_func($callback, $output)
+ : ob_end_clean();
+ }
} // End Kohana
@@ -2309,4 +2334,4 @@ class Kohana_404_Exception extends Kohana_Exception {
header('HTTP/1.1 404 File Not Found');
}
-} // End Kohana 404 Exception
\ No newline at end of file
+} // End Kohana 404 Exception
@ischerbin
Copy link

Hi!

Kohana 2 doesn't have get method in Arr helper. Line 45 ($callback = arr::get(ob_list_handlers(), ob_get_level() - 1);) cause error "Call to undefined method arr::get() in...".
Changing it to:
$_arr = ob_list_handlers();
$callback = $_arr[ob_get_level() - 1];
Works for me.

Anyway thnx for such a useful patch!

@hdkhanhkhtn
Copy link

Mothod get of helper Arr

public static function get($array, $key, $default = NULL)
{
return isset($array[$key]) ? $array[$key] : $default;
}

see more: http://kohanaframework.org/3.2/guide/api/Kohana_Arr#get

@jacobsvante
Copy link

Thank you for this dist. Did wonders to our crappy legacy code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment