Merge pull request #2633 from kaiwangchen/simulate_complete_exception_handler
Simulate a complete custom exception handler by redirecting uncaught events
diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php
index c962fda..67c94cf 100644
--- a/system/core/CodeIgniter.php
+++ b/system/core/CodeIgniter.php
@@ -73,6 +73,7 @@
* ------------------------------------------------------
*/
set_error_handler('_exception_handler');
+ register_shutdown_function('_shutdown_handler');
if ( ! is_php('5.4'))
{
diff --git a/system/core/Common.php b/system/core/Common.php
index 7f296b1..edfad99 100644
--- a/system/core/Common.php
+++ b/system/core/Common.php
@@ -577,6 +577,34 @@
}
}
+// ------------------------------------------------------------------------
+
+if ( ! function_exists('_shutdown_handler'))
+{
+ /**
+ * Shutdown Handler
+ *
+ * This is the shutdown handler that is declared at the top
+ * of CodeIgniter.php. The main reason we use this is to simulate
+ * a complete custom exception handler.
+ *
+ * E_STRICT is purposivly neglected because such events may have
+ * been caught. Duplication or none? None is preferred for now.
+ *
+ * @link http://insomanic.me.uk/post/229851073/php-trick-catching-fatal-errors-e-error-with-a
+ * @return void
+ */
+ function _shutdown_handler()
+ {
+ $last_error = function_exists('error_get_last') ? error_get_last() : NULL;
+ if (isset($last_error) &&
+ ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING)))
+ {
+ _exception_handler($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']);
+ }
+ }
+}
+
// --------------------------------------------------------------------
if ( ! function_exists('remove_invisible_characters'))
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 3e36d88..c99afc1 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -436,6 +436,7 @@
- Added support for HTTP-Only cookies with new config option *cookie_httponly* (default FALSE).
- Renamed method ``_call_hook()`` to ``call_hook()`` in the :doc:`Hooks Library <general/hooks>`.
- ``$config['time_reference']`` now supports all timezone strings supported by PHP.
+ - Made the exception handler complete by registering a shutdown handler to redirect critical events such as PHP parse errors to it, so that they can be displayed and logged without the help of the standard PHP error handler.
Bug fixes for 3.0