Skip to content

Include Syntax Highlighting Language Option in Var-Dumper Dumps #164

@butschster

Description

@butschster

We aim to enhance Var-Dumper by allowing developers to specify the language for syntax highlighting within their dumps. This enhancement will streamline the debugging process and improve the readability of dumped content, particularly in cases involving multiple programming languages.

Introduce a new parameter or method within Var-Dumper that enables developers to specify the language for syntax highlighting. This parameter could accept standard language identifiers or aliases, such as those commonly used in code editors or highlighting libraries (e.g., "php", "javascript", "python", etc.).

image

The current method for adding additional context to a VarDumper involves complex and verbose code, which can be cumbersome and error-prone. This proposal aims to enhance the Symfony VarDumper API to provide a more intuitive and concise way of adding context information.

<?php

use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Dumper\ServerDumper;

\Symfony\Component\VarDumper\VarDumper::setHandler(function ($var, ?string $label = null) {
    $dumper = new ServerDumper('127.0.0.1', new HtmlDumper(), [
        'language' => 'php', // <===== this is the line that you need to add to your code
        'cli' => new CliContextProvider(),
        'source' => new SourceContextProvider(),
    ]);
    $dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);


    $cloner = new VarCloner();
    $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
    $var = $cloner->cloneVar($var);

    if (null !== $label) {
        $var = $var->withContext(['label' => $label]);
    }

    $dumper->dump($var);
});

Proposed Solutions:

  1. Static Method in VarDumper Class: Introduce a static method in the VarDumper class, such as addContext(), which accepts an array of default context settings. Developers can then call this method to set default context options, including the language for syntax highlighting. See https://github.com/symfony/var-dumper/blob/7.0/VarDumper.php#L101
\Symfony\Component\VarDumper\VarDumper::addContext(['language' => 'php']);
  1. Named Argument in dump() function: Extend the dump() function to accept a named argument context, allowing developers to specify default context settings directly when calling dump().
dump($foo, $bar, ..., context: ['language' => 'php']);

UPD: as I can see there is a PR in symfony repository symfony/symfony#48667 that can solve our problem.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy