master

laravel/framework

Last updated at: 29/12/2023 09:21

AskWithCompletion.php

TLDR

This file defines a class called AskWithCompletion in the Illuminate\Console\View\Components namespace. The class has a single method called render() which is used to render a component by asking a question with autocompletion.

Classes

AskWithCompletion

This class extends the Component class and is responsible for rendering a component by asking a question with autocompletion. It has the following method:

render($question, $choices, $default = null)

This method renders the component using the given arguments. It takes three parameters:

  • $question (string): The question to ask the user.
  • $choices (array|callable): The choices for autocompletion. This can be either an array of values or a callable function that returns an array of values.
  • $default (string|null): The default value for the question. This parameter is optional.

The method creates a new Question object with the given question and default value. If the choices parameter is a callable, it sets the autocompleter callback for the question using the callable. Otherwise, it sets the autocompleter values for the question using the choices parameter. Finally, it asks the question using the askQuestion() method of the output object and returns the result.

<?php

namespace Illuminate\Console\View\Components;

use Symfony\Component\Console\Question\Question;

class AskWithCompletion extends Component
{
    /**
     * Renders the component using the given arguments.
     *
     * @param  string  $question
     * @param  array|callable  $choices
     * @param  string  $default
     * @return mixed
     */
    public function render($question, $choices, $default = null)
    {
        $question = new Question($question, $default);

        is_callable($choices)
            ? $question->setAutocompleterCallback($choices)
            : $question->setAutocompleterValues($choices);

        return $this->usingQuestionHelper(
            fn () => $this->output->askQuestion($question)
        );
    }
}