_showFiles = $showFiles; } /** * Set if only the file fragment related to the exception must be shown instead of the complete file * * @param boolean $showFileFragment */ public function showFileFragment($showFileFragment) { $this->_showFileFragment = $showFileFragment; } /** * Change the base uri for css/javascript sources * * @param string $uri */ public function setBaseUri($uri) { $this->_uri = $uri; } /** * Change the CSS theme * * @param string $theme */ public function setTheme($theme) { $this->_theme = $theme; } /** * Set if the exception/error backtrace must be shown * * @param boolean $showBackTrace */ public function showBackTrace($showBackTrace) { $this->_showBackTrace = $showBackTrace; } /** * Returns the css sources * * @return string */ public function getCssSources() { return ''; } /** * Returns the javascript sources * * @return string */ public function getJsSources() { return ''; } /** * Returns the current framework version */ public function getVersion() { $version = \Phalcon\Version::get(); $parts = explode(' ', $version); return '
Phalcon Framework ' . $version . '
'; } protected function _escapeString($value) { $value = str_replace("\n", "\\n", $value); $value = htmlentities($value, ENT_COMPAT, 'utf-8'); return $value; } protected function _getArrayDump($argument, $n = 0) { if ($n < 3 && count($argument) > 0 && count($argument) < 8) { $dump = []; foreach ($argument as $k => $v) { if (is_scalar($v)) { if ($v === '') { $dump[] = $k . ' => (empty string)'; } else { $dump[] = $k . ' => ' . $this->_escapeString($v); } } else { if (is_array($v)) { $dump[] = $k . ' => Array(' . $this->_getArrayDump($v, $n + 1) . ')'; continue; } if (is_object($v)) { $dump[] = $k . ' => Object(' . get_class($v) . ')'; continue; } if (is_null($v)) { $dump[] = $k . ' => null'; continue; } $dump[] = $k . ' => '.$v; } } return join(', ', $dump); } return count($argument); } /** * Shows a backtrace item * * @param int $n * @param array $trace */ protected function _showTraceItem($n, $trace) { echo '#', $n, ''; if (isset($trace['class'])) { if (preg_match('/^Phalcon/', $trace['class'])) { echo '', $trace['class'], ''; } else { $classReflection = new \ReflectionClass($trace['class']); if ($classReflection->isInternal()) { echo '', $trace['class'], ''; } else { echo '', $trace['class'], ''; } } echo $trace['type']; } if (isset($trace['class'])) { echo '', $trace['function'], ''; } else { if (function_exists($trace['function'])) { $functionReflection = new \ReflectionFunction($trace['function']); if ($functionReflection->isInternal()) { echo '', $trace['function'], ''; } else { echo '', $trace['function'], ''; } } else { echo '', $trace['function'], ''; } } if (isset($trace['args'])) { $arguments = []; foreach ($trace['args'] as $argument) { if (is_scalar($argument)) { if (is_bool($argument)) { if ($argument) { $arguments[] = 'true'; } else { $arguments[] = 'null'; } continue; } if (is_string($argument)) { $argument = $this->_escapeString($argument); } $arguments[] = '' . $argument . ''; } else { if (is_object($argument)) { if (method_exists($argument, 'dump')) { $arguments[] = 'Object(' . get_class($argument) . ': ' . $this->_getArrayDump($argument->dump()) . ')'; } else { $arguments[] = 'Object(' . get_class($argument) . ')'; } } else { if (is_array($argument)) { $arguments[] = 'Array(' . $this->_getArrayDump($argument) . ')'; } else { if (is_null($argument)) { $arguments[] = 'null'; continue; } } } } } echo '(' . join(', ', $arguments) . ')'; } if (isset($trace['file'])) { echo '
', $trace['file'], ' (', $trace['line'], ')'; } echo ''; if ($this->_showFiles) { if (isset($trace['file'])) { echo ''; $line = $trace['line']; $lines = file($trace['file']); if ($this->_showFileFragment) { $numberLines = count($lines); $firstLine = ($line - 7) < 1 ? 1 : $line - 7; $lastLine = ($line + 5 > $numberLines ? $numberLines : $line + 5); echo "
";
				    } else {
					    $firstLine = 1;
					    $lastLine = count($lines) - 1;
					    echo "
";
				    }

				    for ($i = $firstLine; $i <= $lastLine; ++$i) {

					    if ($this->_showFileFragment) {
						    if ($i == $firstLine) {
							    if (preg_match('#\*\/$#', rtrim($lines[$i - 1]))) {
								    $lines[$i-1] = str_replace("* /", "  ", $lines[$i - 1]);
							    }
						    }
					    }

					    if ($lines[$i - 1] != PHP_EOL) {
						    $lines[$i - 1] = str_replace("\t", "  ", $lines[$i - 1]);
						    echo htmlentities($lines[$i - 1], ENT_COMPAT, 'UTF-8');
					    } else {
						    echo ' ' . "\n";
					    }
				    }
				    echo '
'; echo ''; } } } /** * Handles exceptions * * @param Exception $e * @return boolean */ public function handle($e) { if (ob_get_level() > 0) { ob_end_clean(); } if (self::$_showActive) { echo $e->getMessage(); return; } self::$_showActive = true; echo 'Exception - ', get_class($e), ': ', $e->getMessage(), '', $this->getCssSources(), ''; echo '
', get_class($e), ': ', $e->getMessage(), '
', $e->getFile(), ' (', $e->getLine(), ')
'; if ($this->_showBackTrace) { echo '
'; foreach ($e->getTrace() as $n => $trace) { $this->_showTraceItem($n, $trace); } echo '
'; } echo $this->getVersion(); echo $this->getJsSources() . ''; // STOP die(); self::$_showActive = false; #return true; } /** * Handles errors/warnings/notices * * @param int $errorCode * @param string $errorMessage * @param string $errorFile * @param int $errorLine */ public function handleError($errorCode, $errorMessage, $errorFile, $errorLine) { if (ob_get_level() > 0) { ob_end_clean(); } if (self::$_showActive) { echo $errorMessage; return false; } if (!(error_reporting() & $errorCode)) { return false; } self::$_showActive = true; echo 'Exception - ', $errorMessage, '', $this->getCssSources(), ''; echo '
', $errorMessage, '
', $errorFile, ' (', $errorLine, ')
'; if ($this->_showBackTrace) { echo '
'; foreach (debug_backtrace() as $n => $trace) { if ($n == 0) { continue; } $this->_showTraceItem($n, $trace); } echo '
'; } echo $this->getVersion(); echo $this->getJsSources() . ''; self::$_showActive = false; // STOP die(); #return true; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////