1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Cli\Interaction\Message; |
15
|
|
|
|
16
|
|
|
use Override; |
|
|
|
|
17
|
|
|
use Valkyrja\Cli\Interaction\Exception\InvalidArgumentException; |
18
|
|
|
use Valkyrja\Cli\Interaction\Formatter\Contract\Formatter; |
19
|
|
|
use Valkyrja\Cli\Interaction\Formatter\QuestionFormatter; |
20
|
|
|
use Valkyrja\Cli\Interaction\Message\Contract\Answer; |
|
|
|
|
21
|
|
|
use Valkyrja\Cli\Interaction\Message\Contract\Question as Contract; |
22
|
|
|
use Valkyrja\Cli\Interaction\Output\Contract\Output; |
23
|
|
|
|
24
|
|
|
use function fgets; |
25
|
|
|
use function fopen; |
26
|
|
|
use function is_callable; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Question. |
30
|
|
|
* |
31
|
|
|
* @author Melech Mizrachi |
32
|
|
|
*/ |
33
|
|
|
class Question extends Message implements Contract |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @param non-empty-string $text The text |
|
|
|
|
37
|
|
|
* @param callable(Output, Answer):Output $callable The callable |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
string $text, |
41
|
|
|
protected $callable, |
42
|
|
|
protected Answer $answer, |
43
|
|
|
Formatter|null $formatter = new QuestionFormatter() |
44
|
|
|
) { |
45
|
|
|
if (! is_callable($this->callable)) { |
46
|
|
|
throw new InvalidArgumentException('$callable must be a valid callable'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
parent::__construct($text, $formatter); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
55
|
|
|
#[Override] |
56
|
|
|
public function getCallable(): callable |
57
|
|
|
{ |
58
|
|
|
return $this->callable; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
#[Override] |
65
|
|
|
public function withCallable(callable $callable): static |
66
|
|
|
{ |
67
|
|
|
$new = clone $this; |
68
|
|
|
|
69
|
|
|
$new->callable = $callable; |
70
|
|
|
|
71
|
|
|
return $new; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
#[Override] |
78
|
|
|
public function getAnswer(): Answer |
79
|
|
|
{ |
80
|
|
|
return $this->answer; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
*/ |
86
|
|
|
#[Override] |
87
|
|
|
public function withAnswer(Answer $answer): static |
88
|
|
|
{ |
89
|
|
|
$new = clone $this; |
90
|
|
|
|
91
|
|
|
$new->answer = $answer; |
92
|
|
|
|
93
|
|
|
return $new; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
#[Override] |
100
|
|
|
public function ask(): Answer |
101
|
|
|
{ |
102
|
|
|
$answer = $this->answer; |
103
|
|
|
$handle = $this->fopen(filename: 'php://stdin', mode: 'rb'); |
104
|
|
|
|
105
|
|
|
if ($handle === false) { |
106
|
|
|
// TODO: Determine if we want to throw RuntimeException (UnhandledStreamQuestionException) here |
107
|
|
|
return $answer; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$line = $this->fgets($handle); |
111
|
|
|
|
112
|
|
|
if ($line === false) { |
113
|
|
|
// TODO: Determine if we want to throw RuntimeException (UnhandledLineQuestionException) here |
114
|
|
|
return $answer; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$response = trim($line); |
118
|
|
|
|
119
|
|
|
if ($response === '') { |
120
|
|
|
return $answer; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $answer->withUserResponse($response); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param non-empty-string $filename The filename to open |
|
|
|
|
128
|
|
|
* @param non-empty-string $mode The mode |
129
|
|
|
* |
130
|
|
|
* @return resource|false |
131
|
|
|
*/ |
132
|
|
|
protected function fopen(string $filename, string $mode) |
133
|
|
|
{ |
134
|
|
|
return fopen(filename: $filename, mode: $mode); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param resource $stream The stream |
139
|
|
|
*/ |
140
|
|
|
protected function fgets($stream): string|false |
141
|
|
|
{ |
142
|
|
|
return fgets($stream); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths