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