Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Console/Concerns/PromptsForMissingInput.php
Строк: 200
<?php

namespace IlluminateConsoleConcerns;

use 
Closure;
use 
IlluminateContractsConsolePromptsForMissingInput as PromptsForMissingInputContract;
use 
SymfonyComponentConsoleInputInputInterface;
use 
SymfonyComponentConsoleOutputOutputInterface;

use function 
LaravelPromptstext;

trait 
PromptsForMissingInput
{
    
/**
     * Interact with the user before validating the input.
     *
     * @param  SymfonyComponentConsoleInputInputInterface  $input
     * @param  SymfonyComponentConsoleOutputOutputInterface  $output
     * @return void
     */
    
protected function interact(InputInterface $inputOutputInterface $output)
    {
        
parent::interact($input$output);

        if (
$this instanceof PromptsForMissingInputContract) {
            
$this->promptForMissingArguments($input$output);
        }
    }

    
/**
     * Prompt the user for any missing arguments.
     *
     * @param  SymfonyComponentConsoleInputInputInterface  $input
     * @param  SymfonyComponentConsoleOutputOutputInterface  $output
     * @return void
     */
    
protected function promptForMissingArguments(InputInterface $inputOutputInterface $output)
    {
        
$prompted collect($this->getDefinition()->getArguments())
            ->
filter(fn ($argument) => $argument->isRequired() && is_null($input->getArgument($argument->getName())))
            ->
filter(fn ($argument) => $argument->getName() !== 'command')
            ->
each(function ($argument) use ($input) {
                
$label $this->promptForMissingArgumentsUsing()[$argument->getName()] ??
                    
'What is '.lcfirst($argument->getDescription() ?: ('the '.$argument->getName())).'?';

                if (
$label instanceof Closure) {
                    return 
$input->setArgument($argument->getName(), $label());
                }

                if (
is_array($label)) {
                    [
$label$placeholder] = $label;
                }

                
$input->setArgument($argument->getName(), text(
                    
label$label,
                    
placeholder$placeholder ?? '',
                    
validatefn ($value) => empty($value) ? "The {$argument->getName()} is required." null,
                ));
            })
            ->
isNotEmpty();

        if (
$prompted) {
            
$this->afterPromptingForMissingArguments($input$output);
        }
    }

    
/**
     * Prompt for missing input arguments using the returned questions.
     *
     * @return array
     */
    
protected function promptForMissingArgumentsUsing()
    {
        return [];
    }

    
/**
     * Perform actions after the user was prompted for missing arguments.
     *
     * @param  SymfonyComponentConsoleInputInputInterface  $input
     * @param  SymfonyComponentConsoleOutputOutputInterface  $output
     * @return void
     */
    
protected function afterPromptingForMissingArguments(InputInterface $inputOutputInterface $output)
    {
        
//
    
}

    
/**
     * Whether the input contains any options that differ from the default values.
     *
     * @param  SymfonyComponentConsoleInputInputInterface  $input
     * @return bool
     */
    
protected function didReceiveOptions(InputInterface $input)
    {
        return 
collect($this->getDefinition()->getOptions())
            ->
reject(fn ($option) => $input->getOption($option->getName()) === $option->getDefault())
            ->
isNotEmpty();
    }
}
Онлайн: 2
Реклама