Bolt\Exception\LowlevelException::catchFatalErrors PHP Method

catchFatalErrors() public static method

Callback for register_shutdown_function() to handle fatal errors.
public static catchFatalErrors ( Silex\Application $app, boolean $flush = true )
$app Silex\Application
$flush boolean
    public static function catchFatalErrors(Application $app, $flush = true)
    {
        if (self::$screen !== null) {
            echo self::$screen;
            return;
        }
        // Get last error, if any
        $error = error_get_last();
        // Let Whoops handle AJAX requested fatal errors
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
            return;
        }
        if ($error['type'] === E_ERROR || $error['type'] === E_PARSE) {
            $html = self::$html;
            // Flush the early error data buffered output from catchFatalErrorsEarly()
            if ($flush) {
                Response::closeOutputBuffers(0, false);
            }
            // Detect if we're being called from a core, an extension or vendor
            $isBoltCoreError = strpos($error['file'], $app['resources']->getPath('rootpath/src'));
            $isVendorError = strpos($error['file'], $app['resources']->getPath('rootpath/vendor'));
            $isExtensionError = strpos($error['file'], $app['resources']->getPath('extensions'));
            // Assemble error trace
            $errorblock = '<code style="display:block; white-space: pre-wrap;">Error: ' . $error['message'] . '</code><br>';
            $errorblock .= '<code>File:  ' . $error['file'] . '</code><br>';
            $errorblock .= '<code>Line:  ' . $error['line'] . '</code><br><br>';
            if ($isBoltCoreError === 0) {
                $html = str_replace('%error_title%', 'PHP Fatal error: Bolt core', $html);
                $html = str_replace('%info%', '', $html);
                $message = $errorblock;
            } elseif ($isVendorError === 0) {
                $html = str_replace('%error_title%', 'PHP Fatal error: Vendor library', $html);
                $html = str_replace('%info%', '', $html);
                $message = $errorblock;
            } elseif ($isExtensionError === 0) {
                $base = str_replace($app['resources']->getPath('extensions'), '', $error['file']);
                $parts = explode(DIRECTORY_SEPARATOR, ltrim($base, '/'));
                $package = $parts[1] . '/' . $parts[2];
                $html = str_replace('%error_title%', 'PHP Fatal error: Bolt extensions', $html);
                $html = str_replace('%info%', '<p>You will only be able to continue by disabling the extension by adding the following to app/config/extensions.yml:</p>' . '<p><code style="display:block;">' . $parts[1] . ':<br>' . '&nbsp;&nbsp;&nbsp;&nbsp;' . $parts[2] . ': false</code></p>', $html);
                $message = '<h4>There is a fatal error in the \'' . $package . '\' extension ' . 'loaded on your Bolt Installation.<h4>';
                $message .= $errorblock;
            } else {
                // Unknown
                $html = str_replace('%error_title%', 'PHP Fatal error: Bolt generic', $html);
                $html = str_replace('%info%', '', $html);
                $message = $errorblock;
            }
            $message = nl2br($message);
            $html = str_replace('%error%', $message, $html);
            // Determine if we're on the command line. If so, don't output HTML.
            if (php_sapi_name() == 'cli') {
                $html = self::cleanHTML($html);
            }
            echo str_replace($app['resources']->getPath('rootpath'), '', $html);
        }
        echo self::$screen;
    }

Usage Example

Exemplo n.º 1
0
 public function testGeneralFatalErrorCatch()
 {
     $app = ['resources' => new Standard(TEST_ROOT)];
     ResourceManager::$theApp = $app;
     $this->php2->expects($this->once())->method('error_get_last')->will($this->returnValue($this->errorResponses['unknown']));
     $this->expectOutputRegex('/PHP Fatal error: Bolt generic/');
     LowlevelException::catchFatalErrors($this->getApp(), false);
 }