DebugBar/Core/DebugBarErrorHandler.php

105 lignes
3.2 KiB
PHP
Brut Vue normale Historique

<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DebugBar\Core;
use D3\DebugBar\Application\Models\AvailabilityCheck;
use D3\DebugBar\Application\Models\Exceptions\CompileErrorException;
use D3\DebugBar\Application\Models\Exceptions\CoreErrorException;
use D3\DebugBar\Application\Models\Exceptions\ParseException;
use D3\DebugBar\Application\Models\Exceptions\UserErrorException;
use ErrorException;
use OxidEsales\Eshop\Core\Registry;
class DebugBarErrorHandler
{
/**
2022-08-16 11:08:53 +02:00
* @param int $severity
* @param string $message
* @param string $file
* @param int $line
*
* @return void|false
* @throws CompileErrorException
* @throws CoreErrorException
* @throws ErrorException
* @throws ParseException
* @throws UserErrorException
*/
2022-08-16 11:08:53 +02:00
public function callback(int $severity, string $message, string $file, int $line)
{
AvailabilityCheck::markErrorOccured();
2022-08-16 11:08:53 +02:00
if (0 === error_reporting() || !(error_reporting() & $severity)) {
// This error code is not included in error_reporting.
return false;
}
2022-08-16 11:08:53 +02:00
$smartyTemplate = $this->getSmartyTemplateLocationFromError($message);
if (is_array($smartyTemplate)) {
[ $file, $line ] = $smartyTemplate;
}
2022-08-16 11:08:53 +02:00
switch ($severity) {
case E_CORE_ERROR:
throw new CoreErrorException($message, 0, $severity, $file, $line);
case E_COMPILE_ERROR:
throw new CompileErrorException($message, 0, $severity, $file, $line);
case E_USER_ERROR:
throw new UserErrorException($message, 0, $severity, $file, $line);
case E_PARSE:
throw new ParseException($message, 0, $severity, $file, $line);
case E_ERROR:
throw new ErrorException($message, 0, $severity, $file, $line);
default:
$this->handleUnregisteredErrorTypes($message, $severity, $file, $line);
}
}
/**
2022-08-16 11:08:53 +02:00
* @param string $messsage
* @return array|null
*/
2022-08-16 11:08:53 +02:00
protected function getSmartyTemplateLocationFromError(string $messsage): ?array
{
if (stristr($messsage, 'Smarty error: [in ')) {
$start = strpos($messsage, '[')+1;
$end = strpos($messsage, ']');
$parts = explode(' ', substr($messsage, $start, $end - $start));
return [Registry::getConfig()->getTemplateDir(isAdmin()).$parts[1], (int) $parts[3]];
}
return null;
}
/**
2022-08-16 11:08:53 +02:00
* @param string $message
* @param int $severity
* @param string $file
* @param int $line
* @return void
*
* @throws ErrorException
*/
2022-08-16 11:08:53 +02:00
protected function handleUnregisteredErrorTypes(
string $message = '',
int $severity = 1,
string $file = __FILE__,
int $line = __LINE__
): void {
throw new ErrorException($message, 0, $severity, $file, $line);
}
2022-08-16 11:08:53 +02:00
}