CommandCompletionTester.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Console\Tester;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Completion\CompletionInput;
  13. use Symfony\Component\Console\Completion\CompletionSuggestions;
  14. /**
  15. * Eases the testing of command completion.
  16. *
  17. * @author Jérôme Tamarelle <jerome@tamarelle.net>
  18. */
  19. class CommandCompletionTester
  20. {
  21. private $command;
  22. /**
  23. * @param \Symfony\Component\Console\Command\Command $command
  24. */
  25. public function __construct($command)
  26. {
  27. $this->command = $command;
  28. }
  29. /**
  30. * Create completion suggestions from input tokens.
  31. * @param mixed[] $input
  32. */
  33. public function complete($input)
  34. {
  35. $currentIndex = \count($input);
  36. if ('' === end($input)) {
  37. array_pop($input);
  38. }
  39. array_unshift($input, $this->command->getName());
  40. $completionInput = CompletionInput::fromTokens($input, $currentIndex);
  41. $completionInput->bind($this->command->getDefinition());
  42. $suggestions = new CompletionSuggestions();
  43. $this->command->complete($completionInput, $suggestions);
  44. $options = [];
  45. foreach ($suggestions->getOptionSuggestions() as $option) {
  46. $options[] = '--'.$option->getName();
  47. }
  48. return array_map('strval', array_merge($options, $suggestions->getValueSuggestions()));
  49. }
  50. }