Skip to content

Commit 9af9d57

Browse files
committed
Tweaks
1 parent 3def7a7 commit 9af9d57

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,39 +191,30 @@ provide a callback function to dynamically generate suggestions::
191191
// This function is called whenever the input changes and new
192192
// suggestions are needed.
193193
$callback = function (string $input): array {
194-
// Strip any characters from the last slash to the end of the
195-
// string - this is considered a complete path and should populate
196-
// our autocomplete suggestions.
197-
$inputPath = preg_replace(
198-
'%(/|^)[^/]*$%',
199-
'$1',
200-
$input
201-
);
202-
203-
// All suggestions should start with the input the user has already
204-
// provided (existing path), followed in this case by the contents
205-
// of the referenced directory. Some suggestions may be ignored
206-
// because they don't match the full string provided by the user,
207-
// but the autocomplete helper will take care of that for us.
208-
return array_map(
209-
function ($suggestion) use ($inputPath) {
210-
return $inputPath . $suggestion;
211-
},
212-
@scandir($inputPath === '' ? '.' : $inputPath) ?: []
213-
);
194+
// Strip any characters from the last slash to the end of the string
195+
// to keep only the last directory and generate suggestions for it
196+
$inputPath = preg_replace('%(/|^)[^/]*$%', '$1', $userInput);
197+
$inputPath = '' === $inputPath ? '.' : $inputPath;
198+
199+
// CAUTION - this example code allows unrestricted access to the
200+
// entire filesystem. In real applications, restrict the directories
201+
// where files and dirs can be found
202+
$foundFilesAndDirs = @scandir($inputPath) ?: [];
203+
204+
return array_map(function ($dirOrFile) use ($inputPath) {
205+
return $inputPath.$dirOrFile;
206+
}, $foundFilesAndDirs);
214207
};
215208

216209
$question = new Question('Please provide the full path of a file to parse');
217210
$question->setAutocompleterCallback($callback);
218211

219-
$filePath = $this->output->askQuestion($question);
212+
$filePath = $helper->ask($input, $output, $question);
220213
}
221214

222-
.. caution::
215+
.. versionadded:: 4.3
223216

224-
This example code allows unrestricted access to the host filesystem, and
225-
should only ever be used in a local context, such as in a script being
226-
manually invoked from the command line on a server you control.
217+
The ``setAutocompleterCallback()`` method was introduced in Symfony 4.3.
227218

228219
Hiding the User's Response
229220
~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)