Skip to content

IDEA: Fruitcake debugbar helper for production environments #1987

Description

@ssw1cblarrion

I opened it as an issue because there's no discussions tab

fruitcake/laravel-debugbar is for debug environments, but there should be an option to leave code as measurements or informational messages.
I'm not sure if I'm misunderstanding the usage. If I accidentally leave debugbar()->startMeasure or any other method and send it to production where vendor laravel-debugbar doesn't exist, it will give me an error.

maybe there should be a package used as a fallback in production(fruitcake/laravel-debugbar-helper, fruitcake/laravel-debugbar-fallback, other name).
PoC example:

// src\LaravelDebugbarHelper.php
namespace Fruitcake\LaravelDebugbarHelper {
    if( !class_exists(\Fruitcake\LaravelDebugbar\LaravelDebugbar::class) ) {
        // dump helper class
        class LaravelDebugbarHelper{
            private static $_INSTANCE=null;

            public static function instance(){
                if(!self::$_INSTANCE)
                    self::$_INSTANCE=new static();

                return self::$_INSTANCE;
            }
            public function __call($name,$args) {
                return static::__callStatic($name,$args);
            }
            public static function __callStatic($name,$args) {
                if($name=='measure' && is_callable($args[1]??null))
                    return $args[1](); // run the closure on measure

                return null;
            }
        }
        // alias dump helper class as the real class name
        class_alias(\Fruitcake\LaravelDebugbarHelper\LaravelDebugbarHelper::class,\Fruitcake\LaravelDebugbar\LaravelDebugbar::class);
        class_alias(\Fruitcake\LaravelDebugbarHelper\LaravelDebugbarHelper::class,\Fruitcake\LaravelDebugbar\Facades\Debugbar::class);
    }
}
// src\helpers.php
namespace  {
    if( class_exists(\Fruitcake\LaravelDebugbarHelper\LaravelDebugbarHelper::class) ) {
        if(!function_exists('debugbar')){
            function debugbar(?string $collector = null) : \Fruitcake\LaravelDebugbar\LaravelDebugbar{
                return $collector ? null : \Fruitcake\LaravelDebugbarHelper\LaravelDebugbarHelper::instance();
            }
        }
        if(!function_exists('debug')){
            function debug(): void {}
        }
    }
}

The same could also exist for php-debugbar/php-debugbar

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions