| 402 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Console\Concerns;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Console\PromptsForMissingInput as PromptsForMissingInputContract;
|
|
|
6 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
7 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
8 |
|
|
|
9 |
trait PromptsForMissingInput
|
|
|
10 |
{
|
|
|
11 |
/**
|
|
|
12 |
* Interact with the user before validating the input.
|
|
|
13 |
*
|
|
|
14 |
* @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
15 |
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
16 |
* @return void
|
|
|
17 |
*/
|
|
|
18 |
protected function interact(InputInterface $input, OutputInterface $output)
|
|
|
19 |
{
|
|
|
20 |
parent::interact($input, $output);
|
|
|
21 |
|
|
|
22 |
if ($this instanceof PromptsForMissingInputContract) {
|
|
|
23 |
$this->promptForMissingArguments($input, $output);
|
|
|
24 |
}
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Prompt the user for any missing arguments.
|
|
|
29 |
*
|
|
|
30 |
* @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
31 |
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
32 |
* @return void
|
|
|
33 |
*/
|
|
|
34 |
protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
|
|
|
35 |
{
|
|
|
36 |
$prompted = collect($this->getDefinition()->getArguments())
|
|
|
37 |
->filter(fn ($argument) => $argument->isRequired() && is_null($input->getArgument($argument->getName())))
|
|
|
38 |
->filter(fn ($argument) => $argument->getName() !== 'command')
|
|
|
39 |
->each(fn ($argument) => $input->setArgument(
|
|
|
40 |
$argument->getName(),
|
|
|
41 |
$this->askPersistently(
|
|
|
42 |
$this->promptForMissingArgumentsUsing()[$argument->getName()] ??
|
|
|
43 |
'What is '.lcfirst($argument->getDescription()).'?'
|
|
|
44 |
)
|
|
|
45 |
))
|
|
|
46 |
->isNotEmpty();
|
|
|
47 |
|
|
|
48 |
if ($prompted) {
|
|
|
49 |
$this->afterPromptingForMissingArguments($input, $output);
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Prompt for missing input arguments using the returned questions.
|
|
|
55 |
*
|
|
|
56 |
* @return array
|
|
|
57 |
*/
|
|
|
58 |
protected function promptForMissingArgumentsUsing()
|
|
|
59 |
{
|
|
|
60 |
return [];
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Perform actions after the user was prompted for missing arguments.
|
|
|
65 |
*
|
|
|
66 |
* @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
67 |
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
68 |
* @return void
|
|
|
69 |
*/
|
|
|
70 |
protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
|
|
|
71 |
{
|
|
|
72 |
//
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Whether the input contains any options that differ from the default values.
|
|
|
77 |
*
|
|
|
78 |
* @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
79 |
* @return bool
|
|
|
80 |
*/
|
|
|
81 |
protected function didReceiveOptions(InputInterface $input)
|
|
|
82 |
{
|
|
|
83 |
return collect($this->getDefinition()->getOptions())
|
|
|
84 |
->reject(fn ($option) => $input->getOption($option->getName()) === $option->getDefault())
|
|
|
85 |
->isNotEmpty();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Continue asking a question until an answer is provided.
|
|
|
90 |
*
|
|
|
91 |
* @param string $question
|
|
|
92 |
* @return string
|
|
|
93 |
*/
|
|
|
94 |
private function askPersistently($question)
|
|
|
95 |
{
|
|
|
96 |
$answer = null;
|
|
|
97 |
|
|
|
98 |
while ($answer === null) {
|
|
|
99 |
$answer = $this->components->ask($question);
|
|
|
100 |
|
|
|
101 |
if ($answer === null) {
|
|
|
102 |
$this->components->error('The answer is required.');
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return $answer;
|
|
|
107 |
}
|
|
|
108 |
}
|