<commit_before><?php

namespace Difra;

use Difra\Envi\Request;

/**
 * Class Debugger
 * @package Difra
 */
class Debugger
{
    /** Debugging is disabled */
    const DEBUG_DISABLED = 0;
    /** Debugging is enabled */
    const DEBUG_ENABLED = 1;
    /** Console is disabled */
    const CONSOLE_DISABLED = 0;
    /** Console is enabled, but switched off by developer */
    const CONSOLE_OFF = 1;
    /** Console is enabled */
    const CONSOLE_ON = 2; // console disabled
    /** Caches are disabled */
    const CACHES_DISABLED = 0; // console enabled, but not active
    /** Caches are enabled */
    const CACHES_ENABLED = 1; // console enabled and active
    /** Display errors */
    const ERRORS_SHOW = 1;
    /** Don't display errors */
    const ERRORS_HIDE = 0;
    /** @var int Debugger state */
    private static $enabled = self::DEBUG_DISABLED;
    /** @var int Console state */
    private static $console = self::CONSOLE_DISABLED;
    /** @var int Caches state */
    private static $caches = self::CACHES_ENABLED;
    /** @var int Display errors state */
    private static $errors = self::ERRORS_HIDE;
    /** @var array Console data */
    private static $output = [];
    /** @var bool Error flag */
    private static $hadError = false;
    /** @var string If there was a handled exception, don't capture it on shutdown */
    private static $handledByException = null;
    /** @var array Last error message captured by captureNormal (let captureShutdown skip it) */
    private static $handledByNormal = null;
    /** @var bool Shut down flag (to prevent undesired output) */
    static public $shutdown = false;

    /**
     * Is debugging enabled?
     * @return bool
     */
    public static function isEnabled()
    {
        self::init();
        return (bool)self::$enabled;
    }

    public static function init()
    {
        // run once
        static $initDone = false;
        if ($initDone) {
            return;
        }
        $initDone = true;

        self::configure();
        self::apply();
    }

    private static function configure()
    {
        // cli mode
        if (Envi::getMode() == 'cli') {
            self::$enabled = self::DEBUG_ENABLED;
            self::$console = self::CONSOLE_DISABLED;
            self::$caches = self::CACHES_DISABLED;
            self::$errors = self::ERRORS_SHOW;
            return;
        }

        // production environment
        if (Envi::isProduction()) {
            self::$enabled = self::DEBUG_DISABLED;
            self::$console = self::CONSOLE_DISABLED;
            self::$caches = self::CACHES_ENABLED;
            self::$errors = self::ERRORS_HIDE;
            set_exception_handler(['\Difra\Debugger', 'productionException']);
            return;
        }

        // got GET parameter debug=-1, emulate production but show errors
        if (isset($_GET['debug']) and $_GET['debug'] == -1) {
            self::$enabled = self::DEBUG_DISABLED;
            self::$console = self::CONSOLE_DISABLED;
            self::$errors = self::ERRORS_SHOW;
            self::$caches = self::CACHES_ENABLED;
            return;
        }

        // configure default development mode
        self::$enabled = self::DEBUG_ENABLED;
        self::$console = self::CONSOLE_ON;
        self::$caches = self::CACHES_DISABLED;
        self::$errors = self::ERRORS_SHOW;

        // console is disabled by configuration or no XSL extension is available
        if ((!is_null($confConsole = Config::getInstance()->getValue('debug', 'console')) and !$confConsole) or
            !extension_loaded('xsl')
        ) {
            self::$console = self::CONSOLE_DISABLED;
            return;
        }

        // debug is disabled by a cookie or GET parameter
        if ((isset($_GET['debug']) and !$_GET['debug']) or (isset($_COOKIE['debug']) and !$_COOKIE['debug'])) {
            self::$enabled = self::DEBUG_DISABLED;
            self::$console = self::CONSOLE_OFF;
            self::$errors = self::ERRORS_HIDE;
            return;
        }

        // console is disabled by a cookie
        if (isset($_COOKIE['debugConsole']) and !$_COOKIE['debugConsole']) {
            self::$console = self::CONSOLE_OFF;
        }

        // caches enabled by a cookie
        if (isset($_COOKIE['cachesEnabled']) and $_COOKIE['cachesEnabled']) {
            self::$caches = self::CACHES_ENABLED;
        }
    }

    private static function apply()
    {
        if (self::$errors == self::ERRORS_HIDE) {
            ini_set('display_errors', 'Off');
        } else {
            ini_set('display_errors', 'On');
            ini_set('error_reporting', E_ALL);
            ini_set('html_errors', (Envi::getMode() != 'web' or Request::isAjax()) ? 'Off' : 'On');
        }
        if (self::$console == self::CONSOLE_ON) {
            ini_set('display_errors', 'Off');
            ini_set('error_reporting', E_ALL);
            set_error_handler(['\Difra\Debugger', 'captureNormal']);
            set_exception_handler(['\Difra\Debugger', 'captureException']);
            register_shutdown_function(['\Difra\Debugger', 'captureShutdown']);
        }<commit_msg>Log errors when errors display is enabled<commit_after><?php

namespace Difra;

use Difra\Envi\Request;

/**
 * Class Debugger
 * @package Difra
 */
class Debugger
{
    /** Debugging is disabled */
    const DEBUG_DISABLED = 0;
    /** Debugging is enabled */
    const DEBUG_ENABLED = 1;
    /** Console is disabled */
    const CONSOLE_DISABLED = 0;
    /** Console is enabled, but switched off by developer */
    const CONSOLE_OFF = 1;
    /** Console is enabled */
    const CONSOLE_ON = 2; // console disabled
    /** Caches are disabled */
    const CACHES_DISABLED = 0; // console enabled, but not active
    /** Caches are enabled */
    const CACHES_ENABLED = 1; // console enabled and active
    /** Display errors */
    const ERRORS_SHOW = 1;
    /** Don't display errors */
    const ERRORS_HIDE = 0;
    /** @var int Debugger state */
    private static $enabled = self::DEBUG_DISABLED;
    /** @var int Console state */
    private static $console = self::CONSOLE_DISABLED;
    /** @var int Caches state */
    private static $caches = self::CACHES_ENABLED;
    /** @var int Display errors state */
    private static $errors = self::ERRORS_HIDE;
    /** @var array Console data */
    private static $output = [];
    /** @var bool Error flag */
    private static $hadError = false;
    /** @var string If there was a handled exception, don't capture it on shutdown */
    private static $handledByException = null;
    /** @var array Last error message captured by captureNormal (let captureShutdown skip it) */
    private static $handledByNormal = null;
    /** @var bool Shut down flag (to prevent undesired output) */
    static public $shutdown = false;

    /**
     * Is debugging enabled?
     * @return bool
     */
    public static function isEnabled()
    {
        self::init();
        return (bool)self::$enabled;
    }

    public static function init()
    {
        // run once
        static $initDone = false;
        if ($initDone) {
            return;
        }
        $initDone = true;

        self::configure();
        self::apply();
    }

    private static function configure()
    {
        // cli mode
        if (Envi::getMode() == 'cli') {
            self::$enabled = self::DEBUG_ENABLED;
            self::$console = self::CONSOLE_DISABLED;
            self::$caches = self::CACHES_DISABLED;
            self::$errors = self::ERRORS_SHOW;
            return;
        }

        // production environment
        if (Envi::isProduction()) {
            self::$enabled = self::DEBUG_DISABLED;
            self::$console = self::CONSOLE_DISABLED;
            self::$caches = self::CACHES_ENABLED;
            self::$errors = self::ERRORS_HIDE;
            set_exception_handler(['\Difra\Debugger', 'productionException']);
            return;
        }

        // got GET parameter debug=-1, emulate production but show errors
        if (isset($_GET['debug']) and $_GET['debug'] == -1) {
            self::$enabled = self::DEBUG_DISABLED;
            self::$console = self::CONSOLE_DISABLED;
            self::$errors = self::ERRORS_SHOW;
            self::$caches = self::CACHES_ENABLED;
            return;
        }

        // configure default development mode
        self::$enabled = self::DEBUG_ENABLED;
        self::$console = self::CONSOLE_ON;
        self::$caches = self::CACHES_DISABLED;
        self::$errors = self::ERRORS_SHOW;

        // console is disabled by configuration or no XSL extension is available
        if ((!is_null($confConsole = Config::getInstance()->getValue('debug', 'console')) and !$confConsole) or
            !extension_loaded('xsl')
        ) {
            self::$console = self::CONSOLE_DISABLED;
            return;
        }

        // debug is disabled by a cookie or GET parameter
        if ((isset($_GET['debug']) and !$_GET['debug']) or (isset($_COOKIE['debug']) and !$_COOKIE['debug'])) {
            self::$enabled = self::DEBUG_DISABLED;
            self::$console = self::CONSOLE_OFF;
            self::$errors = self::ERRORS_HIDE;
            return;
        }

        // console is disabled by a cookie
        if (isset($_COOKIE['debugConsole']) and !$_COOKIE['debugConsole']) {
            self::$console = self::CONSOLE_OFF;
        }

        // caches enabled by a cookie
        if (isset($_COOKIE['cachesEnabled']) and $_COOKIE['cachesEnabled']) {
            self::$caches = self::CACHES_ENABLED;
        }
    }

    private static function apply()
    {
        if (self::$errors == self::ERRORS_HIDE) {
            ini_set('display_errors', 'Off');
        } else {
            ini_set('display_errors', 'On');
            ini_set('error_reporting', E_ALL);
            ini_set('html_errors', (Envi::getMode() != 'web' or Request::isAjax()) ? 'Off' : 'On');
            ini_set('log_errors', 'On');
        }
        if (self::$console == self::CONSOLE_ON) {
            ini_set('display_errors', 'Off');
            ini_set('error_reporting', E_ALL);
            set_error_handler(['\Difra\Debugger', 'captureNormal']);
            set_exception_handler(['\Difra\Debugger', 'captureException']);
            register_shutdown_function(['\Difra\Debugger', 'captureShutdown']);
        }
{'fraction_non_alphanumeric': 0.1350072428778368, 'fraction_numerical': 0.0021245774987928537, 'mean_word_length': 2.297039159503343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3989033', 'n_tokens_mistral': 2951, 'n_tokens_neox': 3006, 'n_words': 754}