blob: e3d5f1d113c58689e90da80a7b92af819ea54bb1 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Command\Descriptor;
13
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020014use Symfony\Component\Console\Input\ArrayInput;
15use Symfony\Component\Console\Output\OutputInterface;
16use Symfony\Component\Console\Style\SymfonyStyle;
17use Symfony\Component\VarDumper\Cloner\Data;
18use Symfony\Component\VarDumper\Dumper\CliDumper;
19
20/**
21 * Describe collected data clones for cli output.
22 *
23 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
24 *
25 * @final
26 */
27class CliDescriptor implements DumpDescriptorInterface
28{
29 private $dumper;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010030 private mixed $lastIdentifier = null;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020031
32 public function __construct(CliDumper $dumper)
33 {
34 $this->dumper = $dumper;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020035 }
36
37 public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
38 {
39 $io = $output instanceof SymfonyStyle ? $output : new SymfonyStyle(new ArrayInput([]), $output);
40 $this->dumper->setColors($output->isDecorated());
41
42 $rows = [['date', date('r', (int) $context['timestamp'])]];
43 $lastIdentifier = $this->lastIdentifier;
44 $this->lastIdentifier = $clientId;
45
46 $section = "Received from client #$clientId";
47 if (isset($context['request'])) {
48 $request = $context['request'];
49 $this->lastIdentifier = $request['identifier'];
50 $section = sprintf('%s %s', $request['method'], $request['uri']);
51 if ($controller = $request['controller']) {
52 $rows[] = ['controller', rtrim($this->dumper->dump($controller, true), "\n")];
53 }
54 } elseif (isset($context['cli'])) {
55 $this->lastIdentifier = $context['cli']['identifier'];
56 $section = '$ '.$context['cli']['command_line'];
57 }
58
59 if ($this->lastIdentifier !== $lastIdentifier) {
60 $io->section($section);
61 }
62
63 if (isset($context['source'])) {
64 $source = $context['source'];
65 $sourceInfo = sprintf('%s on line %d', $source['name'], $source['line']);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010066 if ($fileLink = $source['file_link'] ?? null) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020067 $sourceInfo = sprintf('<href=%s>%s</>', $fileLink, $sourceInfo);
68 }
69 $rows[] = ['source', $sourceInfo];
70 $file = $source['file_relative'] ?? $source['file'];
71 $rows[] = ['file', $file];
72 }
73
74 $io->table([], $rows);
75
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020076 $this->dumper->dump($data);
77 $io->newLine();
78 }
79}