Home > Software design >  How can I use the "dump()" twig function in a template that is going to be rendered on pro
How can I use the "dump()" twig function in a template that is going to be rendered on pro

Time:10-15

I would like to implement a debug output in our test environments in which I would like to output service requests that the application sends.

For this I wanted to use the symfony/twig function dump(), because here the output is wonderfully formatted for all types of variables and also offers the option of opening and closing the structure.

Pseudo-code would be something like this

{% if debugEnabled %}
     {{dump (debugInfos)}}
{% endif %}

Unfortunately, "dump" is part of the Symfony DebugBundle, which for good reasons is not loaded in Prod environments and which should stay that way:

Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],

In the Symfony documentation says:

By design, the dump() function is only available in the dev and test environments, to avoid leaking sensitive information in production. In fact, trying to use the dump() function in the prod environment will result in a PHP error.

I don't want to use dump() in production environments at all, but only locally to output our service requests.

However, I cannot implement a code like above because an error always occurs in production (undefined function dump()) of course, since dump() is not loaded at all.

CodePudding user response:

You can create your own function which tests if the extension is loaded. As you can see in the source the function twig_var_dump is registered, so you can test if the function exists or not to determine if the debug extension was loaded or not.

Should be fine to register your own function as dump as long as your extension gets loaded after the debug extension

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Twig\Environment;

class AppExtension extends AbstractExtension
{
    public function getFunctions()
    {
        return [
            new TwigFunction('dump', [$this, 'dump'] , ['needs_context' => true, 'needs_environment' => true, ]),
        ];
    }

    public function dump(Environment $env, $context, ...$vars)
    {
        if (!function_exists('\twig_var_dump')) return;
        return \twig_var_dump($env, $context, ...$vars);
    }
}

sidenote: Not tested this but should work

CodePudding user response:

Create your own extension that's only loaded in production.

Create a folder (for example Twig) in your src directory. Make that folder excluded from autowiring if you use the default:

    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'
            - '../src/Twig/' # This is the directory excluded from autowiring

Then create a config/services_prod.yaml file with this content:

services:
    App\Twig\FakeDebugExtension: # replace with your class name
        tags:
            - twig.extension # not needed if you use autoconfiguration

Then create a class with this content:

<?php

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class FakeDebugExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('dump', [$this, 'fakeDump']),
        ];
    }

    public function fakeDump(...$arguments): void
    {
    }
}
  • Related