Last active
October 19, 2021 18:04
-
-
Save kmuenkel/eb5443192627c5dd35950eabba4a8a59 to your computer and use it in GitHub Desktop.
Include source for Laravel dd() calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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