vendor/symfony/form/Command/DebugCommand.php line 45

Open in your IDE?
  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\Form\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Completion\CompletionInput;
  14. use Symfony\Component\Console\Completion\CompletionSuggestions;
  15. use Symfony\Component\Console\Exception\InvalidArgumentException;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Symfony\Component\Console\Style\SymfonyStyle;
  21. use Symfony\Component\Form\Console\Helper\DescriptorHelper;
  22. use Symfony\Component\Form\Extension\Core\CoreExtension;
  23. use Symfony\Component\Form\FormRegistryInterface;
  24. use Symfony\Component\Form\FormTypeInterface;
  25. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  26. /**
  27.  * A console command for retrieving information about form types.
  28.  *
  29.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  30.  */
  31. #[AsCommand(name'debug:form'description'Display form type information')]
  32. class DebugCommand extends Command
  33. {
  34.     private FormRegistryInterface $formRegistry;
  35.     private array $namespaces;
  36.     private array $types;
  37.     private array $extensions;
  38.     private array $guessers;
  39.     private ?FileLinkFormatter $fileLinkFormatter;
  40.     public function __construct(FormRegistryInterface $formRegistry, array $namespaces = ['Symfony\Component\Form\Extension\Core\Type'], array $types = [], array $extensions = [], array $guessers = [], FileLinkFormatter $fileLinkFormatter null)
  41.     {
  42.         parent::__construct();
  43.         $this->formRegistry $formRegistry;
  44.         $this->namespaces $namespaces;
  45.         $this->types $types;
  46.         $this->extensions $extensions;
  47.         $this->guessers $guessers;
  48.         $this->fileLinkFormatter $fileLinkFormatter;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     protected function configure()
  54.     {
  55.         $this
  56.             ->setDefinition([
  57.                 new InputArgument('class'InputArgument::OPTIONAL'The form type class'),
  58.                 new InputArgument('option'InputArgument::OPTIONAL'The form type option'),
  59.                 new InputOption('show-deprecated'nullInputOption::VALUE_NONE'Display deprecated options in form types'),
  60.                 new InputOption('format'nullInputOption::VALUE_REQUIRED'The output format (txt or json)''txt'),
  61.             ])
  62.             ->setHelp(<<<'EOF'
  63. The <info>%command.name%</info> command displays information about form types.
  64.   <info>php %command.full_name%</info>
  65. The command lists all built-in types, services types, type extensions and
  66. guessers currently available.
  67.   <info>php %command.full_name% Symfony\Component\Form\Extension\Core\Type\ChoiceType</info>
  68.   <info>php %command.full_name% ChoiceType</info>
  69. The command lists all defined options that contains the given form type,
  70. as well as their parents and type extensions.
  71.   <info>php %command.full_name% ChoiceType choice_value</info>
  72. Use the <info>--show-deprecated</info> option to display form types with
  73. deprecated options or the deprecated options of the given form type:
  74.   <info>php %command.full_name% --show-deprecated</info>
  75.   <info>php %command.full_name% ChoiceType --show-deprecated</info>
  76. The command displays the definition of the given option name.
  77.   <info>php %command.full_name% --format=json</info>
  78. The command lists everything in a machine readable json format.
  79. EOF
  80.             )
  81.         ;
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      */
  86.     protected function execute(InputInterface $inputOutputInterface $output): int
  87.     {
  88.         $io = new SymfonyStyle($input$output);
  89.         if (null === $class $input->getArgument('class')) {
  90.             $object null;
  91.             $options['core_types'] = $this->getCoreTypes();
  92.             $options['service_types'] = array_values(array_diff($this->types$options['core_types']));
  93.             if ($input->getOption('show-deprecated')) {
  94.                 $options['core_types'] = $this->filterTypesByDeprecated($options['core_types']);
  95.                 $options['service_types'] = $this->filterTypesByDeprecated($options['service_types']);
  96.             }
  97.             $options['extensions'] = $this->extensions;
  98.             $options['guessers'] = $this->guessers;
  99.             foreach ($options as $k => $list) {
  100.                 sort($options[$k]);
  101.             }
  102.         } else {
  103.             if (!class_exists($class) || !is_subclass_of($classFormTypeInterface::class)) {
  104.                 $class $this->getFqcnTypeClass($input$io$class);
  105.             }
  106.             $resolvedType $this->formRegistry->getType($class);
  107.             if ($option $input->getArgument('option')) {
  108.                 $object $resolvedType->getOptionsResolver();
  109.                 if (!$object->isDefined($option)) {
  110.                     $message sprintf('Option "%s" is not defined in "%s".'$option\get_class($resolvedType->getInnerType()));
  111.                     if ($alternatives $this->findAlternatives($option$object->getDefinedOptions())) {
  112.                         if (=== \count($alternatives)) {
  113.                             $message .= "\n\nDid you mean this?\n    ";
  114.                         } else {
  115.                             $message .= "\n\nDid you mean one of these?\n    ";
  116.                         }
  117.                         $message .= implode("\n    "$alternatives);
  118.                     }
  119.                     throw new InvalidArgumentException($message);
  120.                 }
  121.                 $options['type'] = $resolvedType->getInnerType();
  122.                 $options['option'] = $option;
  123.             } else {
  124.                 $object $resolvedType;
  125.             }
  126.         }
  127.         $helper = new DescriptorHelper($this->fileLinkFormatter);
  128.         $options['format'] = $input->getOption('format');
  129.         $options['show_deprecated'] = $input->getOption('show-deprecated');
  130.         $helper->describe($io$object$options);
  131.         return 0;
  132.     }
  133.     private function getFqcnTypeClass(InputInterface $inputSymfonyStyle $iostring $shortClassName): string
  134.     {
  135.         $classes $this->getFqcnTypeClasses($shortClassName);
  136.         if (=== $count \count($classes)) {
  137.             $message sprintf("Could not find type \"%s\" into the following namespaces:\n    %s"$shortClassNameimplode("\n    "$this->namespaces));
  138.             $allTypes array_merge($this->getCoreTypes(), $this->types);
  139.             if ($alternatives $this->findAlternatives($shortClassName$allTypes)) {
  140.                 if (=== \count($alternatives)) {
  141.                     $message .= "\n\nDid you mean this?\n    ";
  142.                 } else {
  143.                     $message .= "\n\nDid you mean one of these?\n    ";
  144.                 }
  145.                 $message .= implode("\n    "$alternatives);
  146.             }
  147.             throw new InvalidArgumentException($message);
  148.         }
  149.         if (=== $count) {
  150.             return $classes[0];
  151.         }
  152.         if (!$input->isInteractive()) {
  153.             throw new InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\n\nDid you mean one of these?\n    %s."$shortClassNameimplode("\n    "$classes)));
  154.         }
  155.         return $io->choice(sprintf("The type \"%s\" is ambiguous.\n\nSelect one of the following form types to display its information:"$shortClassName), $classes$classes[0]);
  156.     }
  157.     private function getFqcnTypeClasses(string $shortClassName): array
  158.     {
  159.         $classes = [];
  160.         sort($this->namespaces);
  161.         foreach ($this->namespaces as $namespace) {
  162.             if (class_exists($fqcn $namespace.'\\'.$shortClassName)) {
  163.                 $classes[] = $fqcn;
  164.             } elseif (class_exists($fqcn $namespace.'\\'.ucfirst($shortClassName))) {
  165.                 $classes[] = $fqcn;
  166.             } elseif (class_exists($fqcn $namespace.'\\'.ucfirst($shortClassName).'Type')) {
  167.                 $classes[] = $fqcn;
  168.             } elseif (str_ends_with($shortClassName'type') && class_exists($fqcn $namespace.'\\'.ucfirst(substr($shortClassName0, -4).'Type'))) {
  169.                 $classes[] = $fqcn;
  170.             }
  171.         }
  172.         return $classes;
  173.     }
  174.     private function getCoreTypes(): array
  175.     {
  176.         $coreExtension = new CoreExtension();
  177.         $loadTypesRefMethod = (new \ReflectionObject($coreExtension))->getMethod('loadTypes');
  178.         $coreTypes $loadTypesRefMethod->invoke($coreExtension);
  179.         $coreTypes array_map(function (FormTypeInterface $type) { return \get_class($type); }, $coreTypes);
  180.         sort($coreTypes);
  181.         return $coreTypes;
  182.     }
  183.     private function filterTypesByDeprecated(array $types): array
  184.     {
  185.         $typesWithDeprecatedOptions = [];
  186.         foreach ($types as $class) {
  187.             $optionsResolver $this->formRegistry->getType($class)->getOptionsResolver();
  188.             foreach ($optionsResolver->getDefinedOptions() as $option) {
  189.                 if ($optionsResolver->isDeprecated($option)) {
  190.                     $typesWithDeprecatedOptions[] = $class;
  191.                     break;
  192.                 }
  193.             }
  194.         }
  195.         return $typesWithDeprecatedOptions;
  196.     }
  197.     private function findAlternatives(string $name, array $collection): array
  198.     {
  199.         $alternatives = [];
  200.         foreach ($collection as $item) {
  201.             $lev levenshtein($name$item);
  202.             if ($lev <= \strlen($name) / || str_contains($item$name)) {
  203.                 $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev $lev;
  204.             }
  205.         }
  206.         $threshold 1e3;
  207.         $alternatives array_filter($alternatives, function ($lev) use ($threshold) { return $lev $threshold; });
  208.         ksort($alternatives\SORT_NATURAL \SORT_FLAG_CASE);
  209.         return array_keys($alternatives);
  210.     }
  211.     public function complete(CompletionInput $inputCompletionSuggestions $suggestions): void
  212.     {
  213.         if ($input->mustSuggestArgumentValuesFor('class')) {
  214.             $suggestions->suggestValues(array_merge($this->getCoreTypes(), $this->types));
  215.             return;
  216.         }
  217.         if ($input->mustSuggestArgumentValuesFor('option') && null !== $class $input->getArgument('class')) {
  218.             $this->completeOptions($class$suggestions);
  219.             return;
  220.         }
  221.         if ($input->mustSuggestOptionValuesFor('format')) {
  222.             $helper = new DescriptorHelper();
  223.             $suggestions->suggestValues($helper->getFormats());
  224.         }
  225.     }
  226.     private function completeOptions(string $classCompletionSuggestions $suggestions): void
  227.     {
  228.         if (!class_exists($class) || !is_subclass_of($classFormTypeInterface::class)) {
  229.             $classes $this->getFqcnTypeClasses($class);
  230.             if (=== \count($classes)) {
  231.                 $class $classes[0];
  232.             }
  233.         }
  234.         if (!$this->formRegistry->hasType($class)) {
  235.             return;
  236.         }
  237.         $resolvedType $this->formRegistry->getType($class);
  238.         $suggestions->suggestValues($resolvedType->getOptionsResolver()->getDefinedOptions());
  239.     }
  240. }