Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add description of autocompleter callback #11349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2019
Merged

Add description of autocompleter callback #11349

merged 1 commit into from
Apr 9, 2019

Conversation

MikkelPaulson
Copy link
Contributor

Documented feature added by symfony/symfony#30997.

@@ -179,6 +179,46 @@ will be autocompleted as the user types::
$bundleName = $helper->ask($input, $output, $question);
}

In more complex use cases, it may be necessary to generate suggestions on the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I‘m not sure this is the best example. What you‘re doing is passing on user input to scandir() which opens the door for a dot dot slash attack. Sure, it‘s your own system but who knows who‘s going to read the docs and it what context the reader is using the example :) wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I added the feature because I wanted to autocomplete subsequent words in a command chain, but the most obvious use case seems to be path autocompletion (particularly since that's behaviour we're accustomed to in bash as well).

I'm open to alternatives, but that's the best I could come up with in terms of something that was a) useful, and b) straightforward enough to be readable as an example.

Maybe a disclaimer would be an appropriate compromise?

symfony-splitter pushed a commit to symfony/console that referenced this pull request Apr 8, 2019
…ocompleter (Mikkel Paulson)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Console] Add callback support to Console\Question autocompleter

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | minor edge case, see below
| Deprecations? | no
| Tests pass?   | yes (with expanded coverage)
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | symfony/symfony-docs#11349

Autocompletion is a useful feature, but it's not always possible to anticipate every input the user could provide in advance. For instance, if we're allowing the user to input a path to a file, it's not practical to populate an array with every file and directory in the filesystem, but we can easily build a callback function that populates its suggestions based on the path already inputted.

This change replaces the autocomplete logic that accepts an array of suggestions with an architecture that uses a callback function to populate suggestions in real time as the user provides input.

The first commit adds a test class covering all methods of the `Question` object, while the second commit modifies the `Question` object to accept and store a callback function. The existing `[gs]etAutocompleterValues()` methods are preserved, but instead of being referenced directly from the `QuestionHelper`, they create and call their own callbacks to emulate the current behaviour.

There is one edge case that is changed, as documented in the test: when a `Traversable` object is passed to `setAutocompleterValues()`, the return value of `getAutocompleterValues()` will be the unpacked (array) form of that object rather than the object itself. The unpacking is done lazily and cached on the callback function.

Commits
-------

caad562c11 [Console] Add callback support to Console\Question autocompleter
fabpot added a commit to symfony/symfony that referenced this pull request Apr 8, 2019
…ocompleter (Mikkel Paulson)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Console] Add callback support to Console\Question autocompleter

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | minor edge case, see below
| Deprecations? | no
| Tests pass?   | yes (with expanded coverage)
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | symfony/symfony-docs#11349

Autocompletion is a useful feature, but it's not always possible to anticipate every input the user could provide in advance. For instance, if we're allowing the user to input a path to a file, it's not practical to populate an array with every file and directory in the filesystem, but we can easily build a callback function that populates its suggestions based on the path already inputted.

This change replaces the autocomplete logic that accepts an array of suggestions with an architecture that uses a callback function to populate suggestions in real time as the user provides input.

The first commit adds a test class covering all methods of the `Question` object, while the second commit modifies the `Question` object to accept and store a callback function. The existing `[gs]etAutocompleterValues()` methods are preserved, but instead of being referenced directly from the `QuestionHelper`, they create and call their own callbacks to emulate the current behaviour.

There is one edge case that is changed, as documented in the test: when a `Traversable` object is passed to `setAutocompleterValues()`, the return value of `getAutocompleterValues()` will be the unpacked (array) form of that object rather than the object itself. The unpacking is done lazily and cached on the callback function.

Commits
-------

caad562 [Console] Add callback support to Console\Question autocompleter
@MikkelPaulson
Copy link
Contributor Author

@Toflar @nicolas-grekas I've pushed a commit that addresses your feedback. Please re-review.

$filePath = $this->output->askQuestion($question);
}

Note that this example code allows unrestricted access to the host filesystem,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a .. note:: directive instead

Copy link
Contributor Author

@MikkelPaulson MikkelPaulson Apr 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use .. caution::, which seemed more appropriate.

@javiereguiluz javiereguiluz added Console and removed Waiting Code Merge Docs for features pending to be merged labels Apr 9, 2019
@javiereguiluz javiereguiluz added this to the 4.3 milestone Apr 9, 2019
@javiereguiluz javiereguiluz merged commit 5cfc67e into symfony:master Apr 9, 2019
javiereguiluz added a commit that referenced this pull request Apr 9, 2019
This PR was merged into the master branch.

Discussion
----------

Add description of autocompleter callback

Documented feature added by symfony/symfony#30997.

Commits
-------

5cfc67e Add description of autocompleter callback
@javiereguiluz
Copy link
Member

@MikkelPaulson thanks a lot for providing this fantastic new feature and for documenting it too. What a great first contribution to Symfony Docs!

While merging we made some minor tweaks to your code example to make some comments more concise. Also, we moved the caution note to inside the code example, so people can't miss it. Finally, we added the versionadded directive to warn users that this is only available in Symfony 4.3 and up. Thanks!

@MikkelPaulson
Copy link
Contributor Author

Thanks everyone for the super fast feedback and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants