cloneVar example

/** * @internal */
    public function __sleep(): array
    {
        foreach ($this->data['forms_by_hash'] as &$form) {
            if (isset($form['type_class']) && !$form['type_class'] instanceof ClassStub) {
                $form['type_class'] = new ClassStub($form['type_class']);
            }
        }

        $this->data = $this->cloneVar($this->data);

        return parent::__sleep();
    }

    protected function getCasters(): array
    {
        return parent::getCasters() + [
            \Exception::class => static function D\Exception $e, array $a, Stub $s) {
                foreach (["\0Exception\0previous", "\0Exception\0trace"] as $k) {
                    if (isset($a[$k])) {
                        unset($a[$k]);
                        

        $this->translator = $translator;
    }

    public function lateCollect(): void
    {
        $messages = $this->sanitizeCollectedMessages($this->translator->getCollectedMessages());

        $this->data += $this->computeCount($messages);
        $this->data['messages'] = $messages;

        $this->data = $this->cloneVar($this->data);
    }

    public function collect(Request $request, Response $response, \Throwable $exception = null): void
    {
        $this->data['locale'] = $this->translator->getLocale();
        $this->data['fallback_locales'] = $this->translator->getFallbackLocales();
    }

    public function reset(): void
    {
        $this->data = [];
    }
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;
        $process = $this->getServerProcess();
        
$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));
        });
    }
}
/** * @author Nicolas Grekas <p@tchwork.com> */
class VarClonerTest extends TestCase
{
    public function testMaxIntBoundary()
    {
        $data = [\PHP_INT_MAX => 123];

        $cloner = new VarCloner();
        $clone = $cloner->cloneVar($data);

        $expected = <<<EOTXT Symfony\Component\VarDumper\Cloner\Data Object ( [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array ( [0] => Array ( [0] => Array ( [1] => 1 ) ) [1] => Array ( [%s] => 123 ) ) [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0 [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0 [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20 [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1 [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1 [context:Symfony\Component\VarDumper\Cloner\Data:private] => Array ( ) )
class ServerDumperTest extends TestCase
{
    private const VAR_DUMPER_SERVER = 'tcp://127.0.0.1:9913';

    public function testDumpForwardsToWrappedDumperWhenServerIsUnavailable()
    {
        $wrappedDumper = $this->createMock(DataDumperInterface::class);

        $dumper = new ServerDumper(self::VAR_DUMPER_SERVER, $wrappedDumper);

        $cloner = new VarCloner();
        $data = $cloner->cloneVar('foo');

        $wrappedDumper->expects($this->once())->method('dump')->with($data);

        $dumper->dump($data);
    }

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

        
public function getContext(): ?array
    {
        if (null === $request = $this->requestStack->getCurrentRequest()) {
            return null;
        }

        $controller = $request->attributes->get('_controller');

        return [
            'uri' => $request->getUri(),
            'method' => $request->getMethod(),
            'controller' => $controller ? $this->cloner->cloneVar($controller) : $controller,
            'identifier' => spl_object_hash($request),
        ];
    }
}
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()
    {
        $casters = [
            '*' => function D$v, array $a, Stub $s$isNested) {
                if (!$v instanceof Stub) {
                    foreach ($a as $k => $v) {
                        
/** * @return void */
    public function configure()
    {
        $cloner = $this->cloner;
        $dumper = $this->dumper;
        $connection = $this->connection;

        VarDumper::setHandler(static function D$var, string $label = null) use ($cloner$dumper$connection) {
            $data = $cloner->cloneVar($var);
            if (null !== $label) {
                $data = $data->withContext(['label' => $label]);
            }

            if (!$connection || !$connection->write($data)) {
                $dumper->dump($data);
            }
        });
    }

    public static function getSubscribedEvents(): array
    {
$this->outputStream = fopen('php://stdout', 'w');

                try {
                    return parent::supportsColors();
                } finally {
                    $this->outputStream = $outputStream;
                }
            }
        };

        return FlattenException::createFromThrowable($exception)
            ->setAsString($dumper->dump($cloner->cloneVar($exception), true));
    }
}

        foreach ($this->dispatchers as $name => $dispatcher) {
            if (!$dispatcher instanceof TraceableEventDispatcher) {
                continue;
            }

            $this->setCalledListeners($dispatcher->getCalledListeners($this->currentRequest)$name);
            $this->setNotCalledListeners($dispatcher->getNotCalledListeners($this->currentRequest)$name);
            $this->setOrphanedEvents($dispatcher->getOrphanedEvents($this->currentRequest)$name);
        }

        $this->data = $this->cloneVar($this->data);
    }

    public function getData(): array|Data
    {
        return $this->data;
    }

    /** * @see TraceableEventDispatcher */
    public function setCalledListeners(array $listeners, string $dispatcher = null): void
    {
$content = [];
            }

            if (isset($info['retry_count'])) {
                $content['retries'] = $info['previous_info'];
                unset($info['previous_info']);
            }

            $debugInfo = array_diff_key($info$baseInfo);
            $info = ['info' => $debugInfo] + array_diff_key($info$debugInfo) + $content;
            unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient             $traces[$i]['info'] = $this->cloneVar($info);
            $traces[$i]['options'] = $this->cloneVar($trace['options']);
            $traces[$i]['curlCommand'] = $this->getCurlCommand($trace);
        }

        return [$errorCount$traces];
    }

    private function getCurlCommand(array $trace): ?string
    {
        if (!isset($trace['info']['debug'])) {
            return null;
        }


    public function lateCollect(): void
    {
        if (isset($this->logger)) {
            $containerDeprecationLogs = $this->getContainerDeprecationLogs();
            $this->data = $this->computeErrorsCount($containerDeprecationLogs);
            // get compiler logs later (only when they are needed) to improve performance             $this->data['compiler_logs'] = [];
            $this->data['compiler_logs_filepath'] = $this->containerPathPrefix.'Compiler.log';
            $this->data['logs'] = $this->sanitizeLogs(array_merge($this->logger->getLogs($this->currentRequest)$containerDeprecationLogs));
            $this->data = $this->cloneVar($this->data);
        }
        $this->currentRequest = null;
    }

    public function getLogs()
    {
        return $this->data['logs'] ?? [];
    }

    public function getProcessedLogs()
    {
        

        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));
    }

    public static function provideMessages(): iterable
    {
        yield ['Some message', ['foo' => 'foo', 'bar' => 'bar'], false, true];
        yield ['Some message {@see some text}', ['foo' => 'foo', 'bar' => 'bar'], false, true];
        yield ['Some message {foo}', ['foo' => 'foo', 'bar' => 'bar'], true, false];
        
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"]);
                    }

                    return $a;
                },
            ]);
        }

        return $e->setDataRepresentation($cloner->cloneVar($throwable));
    }

    public function toArray(): array
    {
        $exceptions = [];
        foreach (array_merge([$this]$this->getAllPrevious()) as $exception) {
            $exceptions[] = [
                'message' => $exception->getMessage(),
                'class' => $exception->getClass(),
                'trace' => $exception->getTrace(),
                'data' => $exception->getDataRepresentation(),
            ];
Home | Imprint | This part of the site doesn't use cookies.