<commit_before><?php
/**
 * UserFrosting (http://www.userfrosting.com)
 *
 * @link      https://github.com/userfrosting/UserFrosting
 * @license   https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
 */
namespace UserFrosting\Sprinkle\Core\Util;

use Interop\Container\ContainerInterface;
use UserFrosting\Sprinkle\Core\Http\Concerns\DeterminesContentType;

/**
 * Registers a handler to be invoked whenever the application shuts down.
 * If it shut down due to a fatal error, will generate a clean error message.
 *
 * @author <NAME> (https://alexanderweissman.com)
 */
class ShutdownHandler
{
    use DeterminesContentType;

    /**
     * @var ContainerInterface The global container object, which holds all your services.
     */
    protected $ci;

    /**
     * @var bool
     */
    protected $displayErrorInfo;

    /**
     * Constructor.
     *
     * @param ContainerInterface $ci The global container object, which holds all your services.
     * @param bool $displayErrorInfo
     */
    public function __construct(ContainerInterface $ci, $displayErrorInfo)
    {
        $this->ci = $ci;
        $this->displayErrorInfo = $displayErrorInfo;
    }

    /**
     * Register this class with the shutdown handler.
     *
     * @return void
     */
    public function register()
    {
        register_shutdown_function([$this, 'fatalHandler']);
    }

    /**
     * Set up the fatal error handler, so that we get a clean error message and alert instead of a WSOD.
     */
    public function fatalHandler()
    {
        $error = error_get_last();

        // Handle fatal errors and parse errors
        if ($error !== NULL && $error['type'] == E_ERROR || $error['type'] == E_PARSE) {

            // Build the appropriate error message (debug or client)
            if ($this->displayErrorInfo) {
                $errorMessage = $this->buildErrorInfoMessage($error);
            } else {
                $errorMessage = "Oops, looks like our server might have goofed.  If you're an admin, please ensure that <code>php.log_errors</code> is enabled, and then check the <strong>PHP</strong> error log.";
            }

            // For CLI, just print the message and exit.
            if (php_sapi_name() === 'cli') {
				exit($errorMessage . PHP_EOL);
            }

            // For all other environments, print a debug response for the requested data type
            echo $this->buildErrorPage($errorMessage);

            // If this is an AJAX request and AJAX debugging is turned off, write message to the alert stream<commit_msg>catch more types of fatal errors<commit_after><?php
/**
 * UserFrosting (http://www.userfrosting.com)
 *
 * @link      https://github.com/userfrosting/UserFrosting
 * @license   https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
 */
namespace UserFrosting\Sprinkle\Core\Util;

use Interop\Container\ContainerInterface;
use UserFrosting\Sprinkle\Core\Http\Concerns\DeterminesContentType;

/**
 * Registers a handler to be invoked whenever the application shuts down.
 * If it shut down due to a fatal error, will generate a clean error message.
 *
 * @author <NAME> (https://alexanderweissman.com)
 */
class ShutdownHandler
{
    use DeterminesContentType;

    /**
     * @var ContainerInterface The global container object, which holds all your services.
     */
    protected $ci;

    /**
     * @var bool
     */
    protected $displayErrorInfo;

    /**
     * Constructor.
     *
     * @param ContainerInterface $ci The global container object, which holds all your services.
     * @param bool $displayErrorInfo
     */
    public function __construct(ContainerInterface $ci, $displayErrorInfo)
    {
        $this->ci = $ci;
        $this->displayErrorInfo = $displayErrorInfo;
    }

    /**
     * Register this class with the shutdown handler.
     *
     * @return void
     */
    public function register()
    {
        register_shutdown_function([$this, 'fatalHandler']);
    }

    /**
     * Set up the fatal error handler, so that we get a clean error message and alert instead of a WSOD.
     */
    public function fatalHandler()
    {
        $error = error_get_last();
        $fatalErrorTypes = [
            E_ERROR,
            E_PARSE,
            E_CORE_ERROR,
            E_COMPILE_ERROR
        ];

        // Handle fatal errors and parse errors
        if ($error !== NULL && in_array($error['type'], $fatalErrorTypes)) {

            // Build the appropriate error message (debug or client)
            if ($this->displayErrorInfo) {
                $errorMessage = $this->buildErrorInfoMessage($error);
            } else {
                $errorMessage = "Oops, looks like our server might have goofed.  If you're an admin, please ensure that <code>php.log_errors</code> is enabled, and then check the <strong>PHP</strong> error log.";
            }

            // For CLI, just print the message and exit.
            if (php_sapi_name() === 'cli') {
				exit($errorMessage . PHP_EOL);
            }

            // For all other environments, print a debug response for the requested data type
            echo $this->buildErrorPage($errorMessage);

            // If this is an AJAX request and AJAX debugging is turned off, write message to the alert stream
{'fraction_non_alphanumeric': 0.10349990641961444, 'fraction_numerical': 0.0, 'mean_word_length': 2.4588996763754047, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '1207871', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1450, 'n_words': 461}