<commit_before><?php declare(strict_types=1);

namespace Gmo\Web\Provider;

use Bolt\Common\Ini;
use Psr\Log\LoggerInterface;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Debug\BufferingLogger;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\EventListener\DebugHandlersListener;

/**
 * Error/Exception handler configuration. This should be registered as early as possible.
 */
class ErrorHandlerServiceProvider implements ServiceProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function register(Application $app)
    {
        // Thrown errors in an integer bit field of E_* constants
        $app['error_handler.throw_at'] = function ($app) {
            return $app['debug'] ? E_ALL : 0;
        };

        // Logged errors in an integer bit field of E_* constants.
        $app['error_handler.log_at'] = E_ALL;

        $app['error_handler.logger'] = function ($app) {
            return $app['logger'];
        };

        // Enable handlers for web and cli, but not test runners since they have their own.
        $app['error_handler.enabled'] =
        $app['exception_handler.enabled'] =
            PHP_SAPI !== 'cli' || !defined('PHPUNIT_COMPOSER_INSTALL');

        $app['error_handler'] = $app->share(function () {
            return new ErrorHandler(new BufferingLogger());
        });

        // Exception Handler is registered when this service is invoked if enabled.
        // This is only for bootstrapping. The real one gets set on kernel request / console command event.
        $app['exception_handler.early'] = function ($app) {
            static $handler;

            if ($handler !== null) {
                return $handler;
            }

            if ($app['error_handler.enabled']) {
                // Register the ExceptionHandler on the ErrorHandler as well.
                $handler = ExceptionHandler::register($app['debug'], $app['charset'], $app['code.file_link_format']);
            } else {
                $handler = new ExceptionHandler($app['debug'], $app['charset'], $app['code.file_link_format']);
            }

            // The ExceptionHandler by default renders HTML.
            // If we are on CLI, change it to render for CLI.
            if (PHP_SAPI === 'cli' && class_exists(ConsoleApplication::class)) {
                $handler->setHandler(function ($e) {
                    $app = new ConsoleApplication();
                    $output = new ConsoleOutput(OutputInterface::VERBOSITY_DEBUG);
                    $app->renderException($e, $output);
                    ob_clean();
                });
            }

            return $handler;
        };

        // Listener to set the exception handler from HttpKernel or Console App.
        $app['debug.handlers_listener'] = $app->share(function () {
            return new DebugHandlersListener();
        });

        // Added by WebProviderServiceProvider
        if (!isset($app['code.file_link_format'])) {
            $app['code.file_link_format'] = null;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function boot(Application $app)
    {
        if ($app['debug']) {
            DebugClassLoader::enable();
        }

        if ($app['error_handler.enabled']) {
            // Report all errors since the error handler has
            // its own logging / throwing errors logic.
            error_reporting(E_ALL);
            // Disable built-in PHP error displaying logic. Errors are:
            // 1. Logged to the logger.
            // 2. Converted to an exception and thrown to the user that way.
            // Both of these are regardless of HTML or CLI output.
            // This makes the built-in display_errors redundant.
            Ini::set('display_errors', false);

            $handler = $app['error_handler'];
            ErrorHandler::register($handler);
            // Set throw at value based on config value during 2nd phase.
            // (Has to be after register() as that resets the level.)
            $handler->throwAt($app['error_handler.throw_at'], true);

            $this->configureLogger($handler, $app['error_handler.logger'], $app['error_handler.log_at']);<commit_msg>Move ErrorHandlerSP first in the Application provider list when it is registered. This makes the actual order it is registered in less important, and works with Silex v2 where there are built-in SPs.<commit_after><?php declare(strict_types=1);

namespace Gmo\Web\Provider;

use Bolt\Common\Ini;
use Psr\Log\LoggerInterface;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Debug\BufferingLogger;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\EventListener\DebugHandlersListener;

/**
 * Error/Exception handler configuration.
 *
 * @author <NAME> <<EMAIL>>
 */
class ErrorHandlerServiceProvider implements ServiceProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function register(Application $app)
    {
        // Thrown errors in an integer bit field of E_* constants
        $app['error_handler.throw_at'] = function ($app) {
            return $app['debug'] ? E_ALL : 0;
        };

        // Logged errors in an integer bit field of E_* constants.
        $app['error_handler.log_at'] = E_ALL;

        $app['error_handler.logger'] = function ($app) {
            return $app['logger'];
        };

        // Enable handlers for web and cli, but not test runners since they have their own.
        $app['error_handler.enabled'] =
        $app['exception_handler.enabled'] =
            PHP_SAPI !== 'cli' || !defined('PHPUNIT_COMPOSER_INSTALL');

        $app['error_handler'] = $app->share(function () {
            return new ErrorHandler(new BufferingLogger());
        });

        // Exception Handler is registered when this service is invoked if enabled.
        // This is only for bootstrapping. The real one gets set on kernel request / console command event.
        $app['exception_handler.early'] = function ($app) {
            static $handler;

            if ($handler !== null) {
                return $handler;
            }

            if ($app['error_handler.enabled']) {
                // Register the ExceptionHandler on the ErrorHandler as well.
                $handler = ExceptionHandler::register($app['debug'], $app['charset'], $app['code.file_link_format']);
            } else {
                $handler = new ExceptionHandler($app['debug'], $app['charset'], $app['code.file_link_format']);
            }

            // The ExceptionHandler by default renders HTML.
            // If we are on CLI, change it to render for CLI.
            if (PHP_SAPI === 'cli' && class_exists(ConsoleApplication::class)) {
                $handler->setHandler(function ($e) {
                    $app = new ConsoleApplication();
                    $output = new ConsoleOutput(OutputInterface::VERBOSITY_DEBUG);
                    $app->renderException($e, $output);
                    ob_clean();
                });
            }

            return $handler;
        };

        // Listener to set the exception handler from HttpKernel or Console App.
        $app['debug.handlers_listener'] = $app->share(function () {
            return new DebugHandlersListener();
        });

        // Added by WebProviderServiceProvider
        if (!isset($app['code.file_link_format'])) {
            $app['code.file_link_format'] = null;
        }

        // Move this SP to be first in the provider list, so
        // error handlers are configured before anything else.
        (function () {
            $sp = array_pop($this->providers);
            array_unshift($this->providers, $sp);
        })->call($app);
    }

    /**
     * {@inheritdoc}
     */
    public function boot(Application $app)
    {
        if ($app['debug']) {
            DebugClassLoader::enable();
        }

        if ($app['error_handler.enabled']) {
            // Report all errors since the error handler has
            // its own logging / throwing errors logic.
            error_reporting(E_ALL);
            // Disable built-in PHP error displaying logic. Errors are:
            // 1. Logged to the logger.
            // 2. Converted to an exception and thrown to the user that way.
            // Both of these are regardless of HTML or CLI output.
            // This makes the built-in display_errors redundant.
            Ini::set('display_errors', false);

            $handler = $app['error_handler'];
            ErrorHandler::register($handler);
            // Set throw at value based on config value during 2nd phase.
            // (Has to be after register() as that resets the level.)
            $handler->throwAt($app['error_handler.throw_at'], true);

            $this->configureLogger($handler, $app['error_handler.logger'], $app['error_handler.log_at']);
{'fraction_non_alphanumeric': 0.11513994910941476, 'fraction_numerical': 0.001166242578456319, 'mean_word_length': 2.2913468248429867, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 21, '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': '5619167', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2375, 'n_words': 692}