public
Last active

Kohana 2 ob_end_clean() hotfix (PHP 5.4+)

  • Download Gist
fix.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
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

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!

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.