basePath = $basePath; $this->output = $output; $this->compiledViewPath = $compiledViewPath; } /** * Create a new CLI dumper instance and register it as the default dumper. * * @param string $basePath * @param string $compiledViewPath * @return void */ public static function register($basePath, $compiledViewPath) { $cloner = tap(new VarCloner())->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO); $dumper = new static(new ConsoleOutput(), $basePath, $compiledViewPath); VarDumper::setHandler(fn ($value) => $dumper->dumpWithSource($cloner->cloneVar($value))); } /** * Dump a variable with its source file / line. * * @param \Symfony\Component\VarDumper\Cloner\Data $data * @return void */ public function dumpWithSource(Data $data) { if ($this->dumping) { $this->dump($data); return; } $this->dumping = true; $output = (string) $this->dump($data, true); $lines = explode("\n", $output); $lines[0] .= $this->getDumpSourceContent(); $this->output->write(implode("\n", $lines)); $this->dumping = false; } /** * Get the dump's source console content. * * @return string */ protected function getDumpSourceContent() { if (is_null($dumpSource = $this->resolveDumpSource())) { return ''; } [$file, $relativeFile, $line] = $dumpSource; $href = $this->resolveSourceHref($file, $line); return sprintf( ' // %s%s', is_null($href) ? '' : ";href=$href", $relativeFile, is_null($line) ? '' : ":$line" ); } /** * {@inheritDoc} */ protected function supportsColors(): bool { return $this->output->isDecorated(); } }