DebugCommand.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\Dumper;
  13. use Symfony\Component\Console\Helper\Table;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
  20. use Symfony\Component\Finder\Finder;
  21. use Symfony\Component\Validator\Constraint;
  22. use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
  23. use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
  24. /**
  25. * A console command to debug Validators information.
  26. *
  27. * @author Loïc Frémont <lc.fremont@gmail.com>
  28. */
  29. class DebugCommand extends Command
  30. {
  31. protected static $defaultName = 'debug:validator';
  32. private $validator;
  33. public function __construct(MetadataFactoryInterface $validator)
  34. {
  35. parent::__construct();
  36. $this->validator = $validator;
  37. }
  38. protected function configure()
  39. {
  40. $this
  41. ->addArgument('class', InputArgument::REQUIRED, 'A fully qualified class name or a path')
  42. ->addOption('show-all', null, InputOption::VALUE_NONE, 'Show all classes even if they have no validation constraints')
  43. ->setDescription('Display validation constraints for classes')
  44. ->setHelp(<<<'EOF'
  45. The <info>%command.name% 'App\Entity\Dummy'</info> command dumps the validators for the dummy class.
  46. The <info>%command.name% src/</info> command dumps the validators for the `src` directory.
  47. EOF
  48. )
  49. ;
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output): int
  52. {
  53. $class = $input->getArgument('class');
  54. if (class_exists($class)) {
  55. $this->dumpValidatorsForClass($input, $output, $class);
  56. return 0;
  57. }
  58. try {
  59. foreach ($this->getResourcesByPath($class) as $class) {
  60. $this->dumpValidatorsForClass($input, $output, $class);
  61. }
  62. } catch (DirectoryNotFoundException $exception) {
  63. $io = new SymfonyStyle($input, $output);
  64. $io->error(sprintf('Neither class nor path were found with "%s" argument.', $input->getArgument('class')));
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. private function dumpValidatorsForClass(InputInterface $input, OutputInterface $output, string $class): void
  70. {
  71. $io = new SymfonyStyle($input, $output);
  72. $title = sprintf('<info>%s</info>', $class);
  73. $rows = [];
  74. $dump = new Dumper($output);
  75. foreach ($this->getConstrainedPropertiesData($class) as $propertyName => $constraintsData) {
  76. foreach ($constraintsData as $data) {
  77. $rows[] = [
  78. $propertyName,
  79. $data['class'],
  80. implode(', ', $data['groups']),
  81. $dump($data['options']),
  82. ];
  83. }
  84. }
  85. if (!$rows) {
  86. if (false === $input->getOption('show-all')) {
  87. return;
  88. }
  89. $io->section($title);
  90. $io->text('No validators were found for this class.');
  91. return;
  92. }
  93. $io->section($title);
  94. $table = new Table($output);
  95. $table->setHeaders(['Property', 'Name', 'Groups', 'Options']);
  96. $table->setRows($rows);
  97. $table->setColumnMaxWidth(3, 80);
  98. $table->render();
  99. }
  100. private function getConstrainedPropertiesData(string $class): array
  101. {
  102. $data = [];
  103. /** @var ClassMetadataInterface $classMetadata */
  104. $classMetadata = $this->validator->getMetadataFor($class);
  105. foreach ($classMetadata->getConstrainedProperties() as $constrainedProperty) {
  106. $data[$constrainedProperty] = $this->getPropertyData($classMetadata, $constrainedProperty);
  107. }
  108. return $data;
  109. }
  110. private function getPropertyData(ClassMetadataInterface $classMetadata, string $constrainedProperty): array
  111. {
  112. $data = [];
  113. $propertyMetadata = $classMetadata->getPropertyMetadata($constrainedProperty);
  114. foreach ($propertyMetadata as $metadata) {
  115. foreach ($metadata->getConstraints() as $constraint) {
  116. $data[] = [
  117. 'class' => \get_class($constraint),
  118. 'groups' => $constraint->groups,
  119. 'options' => $this->getConstraintOptions($constraint),
  120. ];
  121. }
  122. }
  123. return $data;
  124. }
  125. private function getConstraintOptions(Constraint $constraint): array
  126. {
  127. $options = [];
  128. foreach (array_keys(get_object_vars($constraint)) as $propertyName) {
  129. // Groups are dumped on a specific column.
  130. if ('groups' === $propertyName) {
  131. continue;
  132. }
  133. $options[$propertyName] = $constraint->$propertyName;
  134. }
  135. return $options;
  136. }
  137. private function getResourcesByPath(string $path): array
  138. {
  139. $finder = new Finder();
  140. $finder->files()->in($path)->name('*.php')->sortByName(true);
  141. $classes = [];
  142. foreach ($finder as $file) {
  143. $fileContent = file_get_contents($file->getRealPath());
  144. preg_match('/namespace (.+);/', $fileContent, $matches);
  145. $namespace = $matches[1] ?? null;
  146. if (!preg_match('/class +([^{ ]+)/', $fileContent, $matches)) {
  147. // no class found
  148. continue;
  149. }
  150. $className = trim($matches[1]);
  151. if (null !== $namespace) {
  152. $classes[] = $namespace.'\\'.$className;
  153. } else {
  154. $classes[] = $className;
  155. }
  156. }
  157. return $classes;
  158. }
  159. }