-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Debug validator command #37706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabpot
merged 2 commits into
symfony:master
from
loic425:feature/debug-validator-command
Sep 5, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
src/Symfony/Component/Validator/Command/DebugCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Dumper; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Finder\Exception\DirectoryNotFoundException; | ||
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Mapping\ClassMetadataInterface; | ||
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; | ||
|
||
/** | ||
* A console command to debug Validators information. | ||
* | ||
* @author Loïc Frémont <lc.fremont@gmail.com> | ||
*/ | ||
class DebugCommand extends Command | ||
{ | ||
protected static $defaultName = 'debug:validator'; | ||
|
||
private $validator; | ||
|
||
public function __construct(MetadataFactoryInterface $validator) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->validator = $validator; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$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('Displays validation constraints for classes') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name% 'App\Entity\Dummy'</info> command dumps the validators for the dummy class. | ||
|
||
The <info>%command.name% src/</info> command dumps the validators for the `src` directory. | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$class = $input->getArgument('class'); | ||
|
||
if (class_exists($class)) { | ||
$this->dumpValidatorsForClass($input, $output, $class); | ||
|
||
return 0; | ||
loic425 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
try { | ||
foreach ($this->getResourcesByPath($class) as $class) { | ||
$this->dumpValidatorsForClass($input, $output, $class); | ||
} | ||
} catch (DirectoryNotFoundException $exception) { | ||
$io = new SymfonyStyle($input, $output); | ||
$io->error(sprintf('Neither class nor path were found with "%s" argument.', $input->getArgument('class'))); | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
ogizanagi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private function dumpValidatorsForClass(InputInterface $input, OutputInterface $output, string $class): void | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$title = sprintf('<info>%s</info>', $class); | ||
$rows = []; | ||
$dump = new Dumper($output); | ||
|
||
foreach ($this->getConstrainedPropertiesData($class) as $propertyName => $constraintsData) { | ||
foreach ($constraintsData as $data) { | ||
$rows[] = [ | ||
$propertyName, | ||
$data['class'], | ||
implode(', ', $data['groups']), | ||
$dump($data['options']), | ||
]; | ||
} | ||
} | ||
|
||
if (!$rows) { | ||
if (false === $input->getOption('show-all')) { | ||
return; | ||
} | ||
|
||
$io->section($title); | ||
$io->text('No validators were found for this class.'); | ||
|
||
return; | ||
} | ||
|
||
$io->section($title); | ||
|
||
$table = new Table($output); | ||
$table->setHeaders(['Property', 'Name', 'Groups', 'Options']); | ||
$table->setRows($rows); | ||
$table->setColumnMaxWidth(3, 80); | ||
$table->render(); | ||
} | ||
|
||
private function getConstrainedPropertiesData(string $class): array | ||
{ | ||
$data = []; | ||
|
||
/** @var ClassMetadataInterface $classMetadata */ | ||
$classMetadata = $this->validator->getMetadataFor($class); | ||
|
||
foreach ($classMetadata->getConstrainedProperties() as $constrainedProperty) { | ||
$data[$constrainedProperty] = $this->getPropertyData($classMetadata, $constrainedProperty); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
private function getPropertyData(ClassMetadataInterface $classMetadata, string $constrainedProperty): array | ||
{ | ||
$data = []; | ||
|
||
$propertyMetadata = $classMetadata->getPropertyMetadata($constrainedProperty); | ||
foreach ($propertyMetadata as $metadata) { | ||
foreach ($metadata->getConstraints() as $constraint) { | ||
$data[] = [ | ||
'class' => \get_class($constraint), | ||
'groups' => $constraint->groups, | ||
'options' => $this->getConstraintOptions($constraint), | ||
]; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
private function getConstraintOptions(Constraint $constraint): array | ||
{ | ||
$options = []; | ||
|
||
foreach (array_keys(get_object_vars($constraint)) as $propertyName) { | ||
// Groups are dumped on a specific column. | ||
if ('groups' === $propertyName) { | ||
continue; | ||
} | ||
|
||
$options[$propertyName] = $constraint->$propertyName; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
private function getResourcesByPath(string $path): array | ||
{ | ||
$finder = new Finder(); | ||
$finder->files()->in($path)->name('*.php')->sortByName(true); | ||
$classes = []; | ||
|
||
foreach ($finder as $file) { | ||
$fileContent = file_get_contents($file->getRealPath()); | ||
|
||
preg_match('/namespace (.+);/', $fileContent, $matches); | ||
|
||
$namespace = $matches[1] ?? null; | ||
|
||
if (false === preg_match('/class +([^{ ]+)/', $fileContent, $matches)) { | ||
// no class found | ||
continue; | ||
} | ||
|
||
$className = trim($matches[1]); | ||
|
||
if (null !== $namespace) { | ||
$classes[] = $namespace.'\\'.$className; | ||
} else { | ||
$classes[] = $className; | ||
} | ||
} | ||
|
||
return $classes; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.