Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Command/DebugCommand.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Serializer\Command;
13
14 use Symfony\Component\Console\Attribute\AsCommand;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Helper\Dumper;
17 use Symfony\Component\Console\Helper\Table;
18 use Symfony\Component\Console\Input\InputArgument;
19 use Symfony\Component\Console\Input\InputInterface;
20 use Symfony\Component\Console\Output\OutputInterface;
21 use Symfony\Component\Console\Style\SymfonyStyle;
22 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
23 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
24
25 /**
26 * A console command to debug Serializer information.
27 *
28 * @author Loïc Frémont <[email protected]m>
29 */
30 #[AsCommand(name: 'debug:serializer', description: 'Display serialization information for classes')]
31 class DebugCommand extends Command
32 {
33 public function __construct(private readonly ClassMetadataFactoryInterface $serializer)
34 {
35 parent::__construct();
36 }
37
38 protected function configure(): void
39 {
40 $this
41 ->addArgument('class', InputArgument::REQUIRED, 'A fully qualified class name')
42 ->setHelp("The <info>%command.name% 'App\Entity\Dummy'</info> command dumps the serializer groups for the dummy class.")
43 ;
44 }
45
46 protected function execute(InputInterface $input, OutputInterface $output): int
47 {
48 $class = $input->getArgument('class');
49
50 if (!class_exists($class)) {
51 $io = new SymfonyStyle($input, $output);
52 $io->error(\sprintf('Class "%s" was not found.', $class));
53
54 return Command::FAILURE;
55 }
56
57 $this->dumpSerializerDataForClass($input, $output, $class);
58
59 return Command::SUCCESS;
60 }
61
62 private function dumpSerializerDataForClass(InputInterface $input, OutputInterface $output, string $class): void
63 {
64 $io = new SymfonyStyle($input, $output);
65 $title = \sprintf('<info>%s</info>', $class);
66 $rows = [];
67 $dump = new Dumper($output);
68
69 $classMetadata = $this->serializer->getMetadataFor($class);
70
71 foreach ($this->getAttributesData($classMetadata) as $propertyName => $data) {
72 $rows[] = [
73 $propertyName,
74 $dump($data),
75 ];
76 }
77
78 $io->section($title);
79
80 if (!$rows) {
81 $io->text('No Serializer data were found for this class.');
82
83 return;
84 }
85
86 $table = new Table($output);
87 $table->setHeaders(['Property', 'Options']);
88 $table->setRows($rows);
89 $table->render();
90 }
91
92 /**
93 * @return array<string, array<string, mixed>>
94 */
95 private function getAttributesData(ClassMetadataInterface $classMetadata): array
96 {
97 $data = [];
98
99 $mapping = $classMetadata->getClassDiscriminatorMapping();
100 $typeProperty = $mapping?->getTypeProperty();
101
102 foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
103 $data[$attributeMetadata->getName()] = [
104 'groups' => $attributeMetadata->getGroups(),
105 'maxDepth' => $attributeMetadata->getMaxDepth(),
106 'serializedName' => $attributeMetadata->getSerializedName(),
107 'serializedPath' => $attributeMetadata->getSerializedPath() ? (string) $attributeMetadata->getSerializedPath() : null,
108 'ignore' => $attributeMetadata->isIgnored(),
109 'normalizationContexts' => $attributeMetadata->getNormalizationContexts(),
110 'denormalizationContexts' => $attributeMetadata->getDenormalizationContexts(),
111 ];
112
113 if ($mapping && $typeProperty === $attributeMetadata->getName()) {
114 $data[$attributeMetadata->getName()]['discriminatorMap'] = $mapping->getTypesMapping();
115 }
116 }
117
118 return $data;
119 }
120 }