From 869412c3a367c59f3d4c18882713b7e6f0e398ca Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Tue, 15 Nov 2011 18:10:46 +0100 Subject: [PATCH] added exception handler example --- VimExceptionHandler.php | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 VimExceptionHandler.php diff --git a/VimExceptionHandler.php b/VimExceptionHandler.php new file mode 100644 index 0000000..f46ef26 --- /dev/null +++ b/VimExceptionHandler.php @@ -0,0 +1,77 @@ + + */ +class VimExceptionHandler +{ + private $levels = array( + E_WARNING => 'Warning', + E_NOTICE => 'Notice', + E_USER_ERROR => 'User Error', + E_USER_WARNING => 'User Warning', + E_USER_NOTICE => 'User Notice', + E_STRICT => 'Runtime Notice', + E_RECOVERABLE_ERROR => 'Catchable Fatal Error', + ); + + private $level; + + static public function register($level = null) + { + $handler = new static(); + $handler->setLevel($level); + set_error_handler(array($handler, 'handleError')); + set_exception_handler(array($handler, 'handleException')); + + return $handler; + } + + public function onKernelException(GetResponseForExceptionEvent $event) + { + $exception = $event->getException(); + $this->handleException($exception); + } + + public function setLevel($level) + { + $this->level = null === $level ? error_reporting() : $level; + } + + /** + * @throws \ErrorException When error_reporting returns error + */ + public function handleError($level, $message, $file, $line, $context) + { + if (0 === $this->level) { + return false; + } + + if (error_reporting() & $level && $this->level & $level) { + throw new \ErrorException(sprintf('%s: %s in %s line %d', isset($this->levels[$level]) ? $this->levels[$level] : $level, $message, $file, $line)); + } + + return false; + } + + private function handleException(Exception $exception) + { + $stack = ''; + foreach ($exception->getTrace() as $row) { + if (isset($row['file'])) { + $stack .= sprintf('%s | %s | %s', $row['file'], $row['line'], $row['function'])."\n"; + } + } + file_put_contents('/tmp/stack.php', $stack); + + $cmd = sprintf('vim --servername %s --remote-send ":call PhpStackTrace()"', 'florian'); + shell_exec($cmd); + } +} +