New in Symfony 4.3: Better console autocomplete
April 10, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Mikkel Paulson
in #30997.
The Console component provides an utility to autocomplete the answers to questions created with the Question Helper. This is used for example to autocomplete classes and entities in the MakerBundle to boost your productivity.
However, autocomplete requires to know all the possible suggestions beforehand. This is impossible if the suggestions depend on the user input and not practical if the completion is highly dynamic (e.g. when autocompleting the contents of directories).
In Symfony 4.3 we improved this feature with a new method called
setAutocompleterCallback()
that allows to use a callback function to
generate the autocomplete suggestions dynamically. For example, this example
shows how to autocomplete the user input with valid directory contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
use Symfony\Component\Console\Question\Question;
// ...
public function execute(InputInterface $input, OutputInterface $output)
{
// This function is called whenever the input changes and new
// suggestions are needed.
$callback = function (string $userInput): array {
// Strip any characters from the last slash to the end of the string
// to keep only the last directory and generate suggestions for it
$inputPath = preg_replace('%(/|^)[^/]*$%', '$1', $userInput);
$inputPath = '' === $inputPath ? '.' : $inputPath;
// CAUTION - this example code allows unrestricted access to the
// entire filesystem. In real applications, restrict the directories
// where files and dirs can be found
$foundFilesAndDirs = @scandir($inputPath) ?: [];
return array_map(function ($dirOrFile) use ($inputPath) {
return $inputPath.$dirOrFile;
}, $foundFilesAndDirs);
};
$question = new Question('Please provide the full path of a file to parse');
$question->setAutocompleterCallback($callback);
$filePath = $helper->ask($input, $output, $question);
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Maybe it could be a good idea to make it available for configuration generation (when building an external bundle or dedicated configuration) by providing an autocomplete on the available keys?
I could have read it wrong, but it would be awesome to support generators for such a callable IMO.
Otherwise, it's great. :}