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