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

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