DebugCommand.php
170 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\AutoMappingStrategy; |
| 16 | use MailPoetVendor\Symfony\Component\Validator\Mapping\CascadingStrategy; |
| 17 | use MailPoetVendor\Symfony\Component\Validator\Mapping\ClassMetadataInterface; |
| 18 | use MailPoetVendor\Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
| 19 | use MailPoetVendor\Symfony\Component\Validator\Mapping\GenericMetadata; |
| 20 | use MailPoetVendor\Symfony\Component\Validator\Mapping\TraversalStrategy; |
| 21 | class DebugCommand extends Command |
| 22 | { |
| 23 | protected static $defaultName = 'debug:validator'; |
| 24 | protected static $defaultDescription = 'Display validation constraints for classes'; |
| 25 | private $validator; |
| 26 | public function __construct(MetadataFactoryInterface $validator) |
| 27 | { |
| 28 | parent::__construct(); |
| 29 | $this->validator = $validator; |
| 30 | } |
| 31 | protected function configure() |
| 32 | { |
| 33 | $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' |
| 34 | The <info>%command.name% 'App\Entity\Dummy'</info> command dumps the validators for the dummy class. |
| 35 | The <info>%command.name% src/</info> command dumps the validators for the `src` directory. |
| 36 | EOF |
| 37 | ); |
| 38 | } |
| 39 | protected function execute(InputInterface $input, OutputInterface $output) : int |
| 40 | { |
| 41 | $class = $input->getArgument('class'); |
| 42 | if (\class_exists($class)) { |
| 43 | $this->dumpValidatorsForClass($input, $output, $class); |
| 44 | return 0; |
| 45 | } |
| 46 | try { |
| 47 | foreach ($this->getResourcesByPath($class) as $class) { |
| 48 | $this->dumpValidatorsForClass($input, $output, $class); |
| 49 | } |
| 50 | } catch (DirectoryNotFoundException $exception) { |
| 51 | $io = new SymfonyStyle($input, $output); |
| 52 | $io->error(\sprintf('Neither class nor path were found with "%s" argument.', $input->getArgument('class'))); |
| 53 | return 1; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | private function dumpValidatorsForClass(InputInterface $input, OutputInterface $output, string $class) : void |
| 58 | { |
| 59 | $io = new SymfonyStyle($input, $output); |
| 60 | $title = \sprintf('<info>%s</info>', $class); |
| 61 | $rows = []; |
| 62 | $dump = new Dumper($output); |
| 63 | $classMetadata = $this->validator->getMetadataFor($class); |
| 64 | foreach ($this->getClassConstraintsData($classMetadata) as $data) { |
| 65 | $rows[] = ['-', $data['class'], \implode(', ', $data['groups']), $dump($data['options'])]; |
| 66 | } |
| 67 | foreach ($this->getConstrainedPropertiesData($classMetadata) as $propertyName => $constraintsData) { |
| 68 | foreach ($constraintsData as $data) { |
| 69 | $rows[] = [$propertyName, $data['class'], \implode(', ', $data['groups']), $dump($data['options'])]; |
| 70 | } |
| 71 | } |
| 72 | if (!$rows) { |
| 73 | if (\false === $input->getOption('show-all')) { |
| 74 | return; |
| 75 | } |
| 76 | $io->section($title); |
| 77 | $io->text('No validators were found for this class.'); |
| 78 | return; |
| 79 | } |
| 80 | $io->section($title); |
| 81 | $table = new Table($output); |
| 82 | $table->setHeaders(['Property', 'Name', 'Groups', 'Options']); |
| 83 | $table->setRows($rows); |
| 84 | $table->setColumnMaxWidth(3, 80); |
| 85 | $table->render(); |
| 86 | } |
| 87 | private function getClassConstraintsData(ClassMetadataInterface $classMetadata) : iterable |
| 88 | { |
| 89 | foreach ($classMetadata->getConstraints() as $constraint) { |
| 90 | (yield ['class' => \get_class($constraint), 'groups' => $constraint->groups, 'options' => $this->getConstraintOptions($constraint)]); |
| 91 | } |
| 92 | } |
| 93 | private function getConstrainedPropertiesData(ClassMetadataInterface $classMetadata) : array |
| 94 | { |
| 95 | $data = []; |
| 96 | foreach ($classMetadata->getConstrainedProperties() as $constrainedProperty) { |
| 97 | $data[$constrainedProperty] = $this->getPropertyData($classMetadata, $constrainedProperty); |
| 98 | } |
| 99 | return $data; |
| 100 | } |
| 101 | private function getPropertyData(ClassMetadataInterface $classMetadata, string $constrainedProperty) : array |
| 102 | { |
| 103 | $data = []; |
| 104 | $propertyMetadata = $classMetadata->getPropertyMetadata($constrainedProperty); |
| 105 | foreach ($propertyMetadata as $metadata) { |
| 106 | $autoMapingStrategy = 'Not supported'; |
| 107 | if ($metadata instanceof GenericMetadata) { |
| 108 | switch ($metadata->getAutoMappingStrategy()) { |
| 109 | case AutoMappingStrategy::ENABLED: |
| 110 | $autoMapingStrategy = 'Enabled'; |
| 111 | break; |
| 112 | case AutoMappingStrategy::DISABLED: |
| 113 | $autoMapingStrategy = 'Disabled'; |
| 114 | break; |
| 115 | case AutoMappingStrategy::NONE: |
| 116 | $autoMapingStrategy = 'None'; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | $traversalStrategy = 'None'; |
| 121 | if (TraversalStrategy::TRAVERSE === $metadata->getTraversalStrategy()) { |
| 122 | $traversalStrategy = 'Traverse'; |
| 123 | } |
| 124 | if (TraversalStrategy::IMPLICIT === $metadata->getTraversalStrategy()) { |
| 125 | $traversalStrategy = 'Implicit'; |
| 126 | } |
| 127 | $data[] = ['class' => 'property options', 'groups' => [], 'options' => ['cascadeStrategy' => CascadingStrategy::CASCADE === $metadata->getCascadingStrategy() ? 'Cascade' : 'None', 'autoMappingStrategy' => $autoMapingStrategy, 'traversalStrategy' => $traversalStrategy]]; |
| 128 | foreach ($metadata->getConstraints() as $constraint) { |
| 129 | $data[] = ['class' => \get_class($constraint), 'groups' => $constraint->groups, 'options' => $this->getConstraintOptions($constraint)]; |
| 130 | } |
| 131 | } |
| 132 | return $data; |
| 133 | } |
| 134 | private function getConstraintOptions(Constraint $constraint) : array |
| 135 | { |
| 136 | $options = []; |
| 137 | foreach (\array_keys(\get_object_vars($constraint)) as $propertyName) { |
| 138 | // Groups are dumped on a specific column. |
| 139 | if ('groups' === $propertyName) { |
| 140 | continue; |
| 141 | } |
| 142 | $options[$propertyName] = $constraint->{$propertyName}; |
| 143 | } |
| 144 | \ksort($options); |
| 145 | return $options; |
| 146 | } |
| 147 | private function getResourcesByPath(string $path) : array |
| 148 | { |
| 149 | $finder = new Finder(); |
| 150 | $finder->files()->in($path)->name('*.php')->sortByName(\true); |
| 151 | $classes = []; |
| 152 | foreach ($finder as $file) { |
| 153 | $fileContent = \file_get_contents($file->getRealPath()); |
| 154 | \preg_match('/namespace (.+);/', $fileContent, $matches); |
| 155 | $namespace = $matches[1] ?? null; |
| 156 | if (!\preg_match('/class +([^{ ]+)/', $fileContent, $matches)) { |
| 157 | // no class found |
| 158 | continue; |
| 159 | } |
| 160 | $className = \trim($matches[1]); |
| 161 | if (null !== $namespace) { |
| 162 | $classes[] = $namespace . '\\' . $className; |
| 163 | } else { |
| 164 | $classes[] = $className; |
| 165 | } |
| 166 | } |
| 167 | return $classes; |
| 168 | } |
| 169 | } |
| 170 |