@@ -191,39 +191,30 @@ provide a callback function to dynamically generate suggestions::
191
191
// This function is called whenever the input changes and new
192
192
// suggestions are needed.
193
193
$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);
214
207
};
215
208
216
209
$question = new Question('Please provide the full path of a file to parse');
217
210
$question->setAutocompleterCallback($callback);
218
211
219
- $filePath = $this->output->askQuestion( $question);
212
+ $filePath = $helper->ask($input, $output, $question);
220
213
}
221
214
222
- .. caution ::
215
+ .. versionadded :: 4.3
223
216
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.
227
218
228
219
Hiding the User's Response
229
220
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments