master

laravel/framework

Last updated at: 29/12/2023 09:23

WhoopsHandler.php

TLDR

This file contains the WhoopsHandler class which is responsible for creating a Whoops handler for debug mode. It includes methods to register application paths, register a blacklist, and register an editor with the handler.

Methods

forDebug

This method creates a new instance of the PrettyPageHandler and configures it for debug mode. It registers the application paths, blacklist, and editor with the handler.

registerApplicationPaths

This method registers the application paths with the handler by setting the application paths on the PrettyPageHandler instance.

directoriesExceptVendor

This method returns an array of application paths excluding the "vendor" directory.

registerBlacklist

This method registers a blacklist with the handler. It iterates over the debug blacklist and debug hide configuration options from the app config and adds them to the handler's blacklist.

registerEditor

This method registers an editor with the handler by setting the editor configuration option on the PrettyPageHandler instance.

<?php

namespace Illuminate\Foundation\Exceptions\Whoops;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Whoops\Handler\PrettyPageHandler;

class WhoopsHandler
{
    /**
     * Create a new Whoops handler for debug mode.
     *
     * @return \Whoops\Handler\PrettyPageHandler
     */
    public function forDebug()
    {
        return tap(new PrettyPageHandler, function ($handler) {
            $handler->handleUnconditionally(true);

            $this->registerApplicationPaths($handler)
                ->registerBlacklist($handler)
                ->registerEditor($handler);
        });
    }

    /**
     * Register the application paths with the handler.
     *
     * @param  \Whoops\Handler\PrettyPageHandler  $handler
     * @return $this
     */
    protected function registerApplicationPaths($handler)
    {
        $handler->setApplicationPaths(
            array_flip($this->directoriesExceptVendor())
        );

        return $this;
    }

    /**
     * Get the application paths except for the "vendor" directory.
     *
     * @return array
     */
    protected function directoriesExceptVendor()
    {
        return Arr::except(
            array_flip((new Filesystem)->directories(base_path())),
            [base_path('vendor')]
        );
    }

    /**
     * Register the blacklist with the handler.
     *
     * @param  \Whoops\Handler\PrettyPageHandler  $handler
     * @return $this
     */
    protected function registerBlacklist($handler)
    {
        foreach (config('app.debug_blacklist', config('app.debug_hide', [])) as $key => $secrets) {
            foreach ($secrets as $secret) {
                $handler->blacklist($key, $secret);
            }
        }

        return $this;
    }

    /**
     * Register the editor with the handler.
     *
     * @param  \Whoops\Handler\PrettyPageHandler  $handler
     * @return $this
     */
    protected function registerEditor($handler)
    {
        if (config('app.editor', false)) {
            $handler->setEditor(config('app.editor'));
        }

        return $this;
    }
}