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

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