VarCloner example

return $e;
    }

    public static function createWithDataRepresentation(\Throwable $throwable, int $statusCode = null, array $headers = [], VarCloner $cloner = null)static
    {
        $e = static::createFromThrowable($throwable$statusCode$headers);

        static $defaultCloner;

        if (!$cloner ??= $defaultCloner) {
            $cloner = $defaultCloner = new VarCloner();
            $cloner->addCasters([
                \Throwable::class => function D\Throwable $e, array $a, Stub $s, bool $isNested): array {
                    if (!$isNested) {
                        unset($a[Caster::PREFIX_PROTECTED.'message']);
                        unset($a[Caster::PREFIX_PROTECTED.'code']);
                        unset($a[Caster::PREFIX_PROTECTED.'file']);
                        unset($a[Caster::PREFIX_PROTECTED.'line']);
                        unset($a["\0Error\0trace"]$a["\0Exception\0trace"]);
                        unset($a["\0Error\0previous"]$a["\0Exception\0previous"]);
                    }

                    
public function testGet()
    {
        if (\ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
            $this->markTestSkipped('A custom file_link_format is defined.');
        }

        require __DIR__.'/../Fixtures/dumb-var.php';

        $dumper = new HtmlDumper('php://output');
        $dumper->setDumpHeader('<foo></foo>');
        $dumper->setDumpBoundaries('<bar>', '</bar>');
        $cloner = new VarCloner();
        $cloner->addCasters([
            ':stream' => function D$res$a) {
                unset($a['uri']$a['wrapper_data']);

                return $a;
            },
        ]);
        $data = $cloner->cloneVar($var);

        ob_start();
        $dumper->dump($data);
        

class ContextualizedDumperTest extends TestCase
{
    public function testContextualizedCliDumper()
    {
        $wrappedDumper = new CliDumper('php://output');
        $wrappedDumper->setColors(true);

        $var = 'example';
        $href = sprintf('file://%s#L%s', __FILE__, 37);
        $dumper = new ContextualizedDumper($wrappedDumper[new SourceContextProvider()]);
        $cloner = new VarCloner();
        $data = $cloner->cloneVar($var);

        ob_start();
        $dumper->dump($data);
        $out = ob_get_clean();

        $this->assertStringContainsString("\e]8;;{$href}\e\\^\e]", $out);
        $this->assertStringContainsString("m{$var}\e[", $out);
    }
}

class CliDumperTest extends TestCase
{
    use VarDumperTestTrait;

    public function testGet()
    {
        require __DIR__.'/../Fixtures/dumb-var.php';

        $dumper = new CliDumper('php://output');
        $dumper->setColors(false);
        $cloner = new VarCloner();
        $cloner->addCasters([
            ':stream' => function D$res$a) {
                unset($a['uri']$a['wrapper_data']);

                return $a;
            },
            'Symfony\Component\VarDumper\Tests\Fixture\DumbFoo' => function D$foo$a) {
                $a['foo'] = new CutStub($a['foo']);

                return $a;
            },
        ]);
public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null)
    {
        $this->output = $output;
        $this->dumper = $dumper;
        $this->cloner = $cloner;

        if (class_exists(CliDumper::class)) {
            $this->handler = function D$var): string {
                $dumper = $this->dumper ??= new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
                $dumper->setColors($this->output->isDecorated());

                return rtrim($dumper->dump(($this->cloner ??= new VarCloner())->cloneVar($var)->withRefHandles(false), true));
            };
        } else {
            $this->handler = fn ($var): string => match (true) {
                null === $var => 'null',
                true === $var => 'true',
                false === $var => 'false',
                \is_string($var) => '"'.$var.'"',
                default => rtrim(print_r($var, true)),
            };
        }
    }

    
$var3 = 'c';

        ob_start();
        $return = dump($var1, second: $var2, third: $var3);
        ob_end_clean();

        $this->assertSame([$var1, 'second' => $var2, 'third' => $var3]$return);
    }

    protected function setupVarDumper()
    {
        $cloner = new VarCloner();
        $dumper = new CliDumper('php://output');
        VarDumper::setHandler(function D$var) use ($cloner$dumper) {
            $dumper->dump($cloner->cloneVar($var));
        });
    }
}

class VarDumperFormatter implements FormatterInterface
{
    use CompatibilityFormatter;

    private VarCloner $cloner;

    public function __construct(VarCloner $cloner = null)
    {
        $this->cloner = $cloner ?? new VarCloner();
    }

    private function doFormat(array|LogRecord $record): mixed
    {
        if ($record instanceof LogRecord) {
            $record = $record->toArray();
        }

        $record['context'] = $this->cloner->cloneVar($record['context']);
        $record['extra'] = $this->cloner->cloneVar($record['extra']);

        

    protected function cloneVar(mixed $var): Data
    {
        if ($var instanceof Data) {
            return $var;
        }
        if (!isset($this->cloner)) {
            $this->cloner = new VarCloner();
            $this->cloner->setMaxItems(-1);
            $this->cloner->addCasters($this->getCasters());
        }

        return $this->cloner->cloneVar($var);
    }

    /** * @return callable[] The casters to add to the cloner */
    protected function getCasters()
    {
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\LoaderInterface;

class DumpExtensionTest extends TestCase
{
    /** * @dataProvider getDumpTags */
    public function testDumpTag($template$debug$expectedOutput$expectedDumped)
    {
        $extension = new DumpExtension(new VarCloner());
        $twig = new Environment(new ArrayLoader(['template' => $template])[
            'debug' => $debug,
            'cache' => false,
            'optimizations' => 0,
        ]);
        $twig->addExtension($extension);

        $dumped = null;
        $exception = null;
        $prevDumper = VarDumper::setHandler(function D$var) use (&$dumped) { $dumped = $var});

        
class ConnectionTest extends TestCase
{
    private const VAR_DUMPER_SERVER = 'tcp://127.0.0.1:9913';

    public function testDump()
    {
        if ('True' === getenv('APPVEYOR')) {
            $this->markTestSkipped('Skip transient test on AppVeyor');
        }

        $cloner = new VarCloner();
        $data = $cloner->cloneVar('foo');
        $connection = new Connection(self::VAR_DUMPER_SERVER, [
            'foo_provider' => new class() implements ContextProviderInterface {
                public function getContext(): ?array
                {
                    return ['foo'];
                }
            },
        ]);

        $dumped = null;
        
public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null)
    {
        $this->output = $output;
        $this->dumper = $dumper;
        $this->cloner = $cloner;

        if (class_exists(CliDumper::class)) {
            $this->handler = function D$var): string {
                $dumper = $this->dumper ??= new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
                $dumper->setColors($this->output->isDecorated());

                return rtrim($dumper->dump(($this->cloner ??= new VarCloner())->cloneVar($var)->withRefHandles(false), true));
            };
        } else {
            $this->handler = fn ($var): string => match (true) {
                null === $var => 'null',
                true === $var => 'true',
                false === $var => 'false',
                \is_string($var) => '"'.$var.'"',
                default => rtrim(print_r($var, true)),
            };
        }
    }

    
$name = $this->style('meta', $name);
                    }
                    $this->line .= $name.' on line '.$this->style('meta', $line).':';
                } else {
                    $this->line .= $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
                }
                $this->dumpLine(0);
            };
            $contextDumper = $contextDumper->bindTo($dumper$dumper);
            $contextDumper($name$file$line$this->fileLinkFormat, $label);
        } else {
            $cloner = new VarCloner();
            $dumper->dump($cloner->cloneVar(('' !== $label ? $label.' in ' : '').$name.' on line '.$line.':'));
        }
        $dumper->dump($data);
    }
}
$name = $this->style('meta', $name);
                    }
                    $this->line .= $name.' on line '.$this->style('meta', $line).':';
                } else {
                    $this->line .= $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
                }
                $this->dumpLine(0);
            };
            $contextDumper = $contextDumper->bindTo($dumper$dumper);
            $contextDumper($name$file$line$this->fileLinkFormat, $label);
        } else {
            $cloner = new VarCloner();
            $dumper->dump($cloner->cloneVar(('' !== $label ? $label.' in ' : '').$name.' on line '.$line.':'));
        }
        $dumper->dump($data);
    }
}

        $this->options = array_replace([
            'format' => self::SIMPLE_FORMAT,
            'date_format' => self::SIMPLE_DATE,
            'colors' => true,
            'multiline' => false,
            'level_name_format' => '%-9s',
            'ignore_empty_context_and_extra' => true,
        ]$options);

        if (class_exists(VarCloner::class)) {
            $this->cloner = new VarCloner();
            $this->cloner->addCasters([
                '*' => $this->castObject(...),
            ]);

            $this->outputBuffer = fopen('php://memory', 'r+');
            if ($this->options['multiline']) {
                $output = $this->outputBuffer;
            } else {
                $output = $this->echoLine(...);
            }

            
class WebProfilerExtensionTest extends TestCase
{
    /** * @dataProvider provideMessages */
    public function testDumpHeaderIsDisplayed(string $message, array $context, bool $dump1HasHeader, bool $dump2HasHeader)
    {
        class_exists(CoreExtension::class); // Load twig_convert_encoding()         class_exists(EscaperExtension::class); // Load twig_escape_filter()
        $twigEnvironment = $this->mockTwigEnvironment();
        $varCloner = new VarCloner();

        $webProfilerExtension = new WebProfilerExtension();

        $needle = 'window.Sfdump';

        $dump1 = $webProfilerExtension->dumpLog($twigEnvironment$message$varCloner->cloneVar($context));
        self::assertSame($dump1HasHeaderstr_contains($dump1$needle));

        $dump2 = $webProfilerExtension->dumpData($twigEnvironment$varCloner->cloneVar([]));
        self::assertSame($dump2HasHeaderstr_contains($dump2$needle));
    }

    
Home | Imprint | This part of the site doesn't use cookies.