Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
Last active October 19, 2021 18:04
Show Gist options
  • Save kmuenkel/eb5443192627c5dd35950eabba4a8a59 to your computer and use it in GitHub Desktop.
Save kmuenkel/eb5443192627c5dd35950eabba4a8a59 to your computer and use it in GitHub Desktop.
Include source for Laravel dd() calls
<?php
namespace App\Providers;
use stdClass;
use ReflectionObject;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\VarDumper\VarDumper;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->helpers();
}
/**
* Thwart losing track of your dd() calls
* @throws ReflectionException
*/
protected function helpers()
{
//Private properties are irritating
$varDumper = app(VarDumper::class);
$varDumperReflection = app(ReflectionObject::class, ['argument' => $varDumper]);
$handlerReflection = $varDumperReflection->getProperty('handler');
$handlerReflection->setAccessible(true);
$handler = $handlerReflection->getValue($varDumper);
//Wrap the handler with one that finds the call location
$handler = function ($var) use ($handler) {
static $sourced = false;
if (!$sourced) {
$sourced = true;
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$source = $trace[2]['file'].':'.$trace[2]['line'];
$handler($source);
}
return $handler($var);
};
VarDumper::setHandler($handler);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment