2
2
3
3
namespace Acquia \Drupal \RecommendedSettings \Tests \Traits ;
4
4
5
- use Acquia \Drupal \RecommendedSettings \Common \ArrayManipulator ;
6
- use Symfony \Component \Console \Helper \Table ;
7
5
use Symfony \Component \Console \Output \NullOutput ;
8
6
use Symfony \Component \Console \Output \OutputInterface ;
9
- use Symfony \Component \Console \Question \ChoiceQuestion ;
10
- use Symfony \Component \Console \Question \Question ;
11
7
12
8
/**
13
9
* An extension of \Robo\Common\IO.
16
12
*/
17
13
trait DrsIO {
18
14
15
+ /**
16
+ * Stores the output.
17
+ */
18
+ protected OutputInterface $ output ;
19
+
19
20
/**
20
21
* Writes text to screen, without decoration.
21
22
*
@@ -41,29 +42,6 @@ protected function yell($text, $length = 40, $color = 'green'): void {
41
42
$ this ->formattedOutput ($ text , $ length , $ format );
42
43
}
43
44
44
- /**
45
- * Prints the message to terminal.
46
- *
47
- * @param string $message
48
- * Given message to print.
49
- * @param string $type
50
- * Type of message. Ex: error, success, info etc.
51
- * @param string $color
52
- * Given background color.
53
- */
54
- protected function print (string $ message , string $ type = "success " , string $ color = "" ): void {
55
- if (!$ color ) {
56
- $ color = match ($ type ) {
57
- "warning " => "yellow " ,
58
- "notice " => "cyan " ,
59
- "error " => "red " ,
60
- default => "green " ,
61
- };
62
- }
63
- $ message = " <fg=white;bg= $ color;options=bold>[ $ type]</fg=white;bg= $ color;options=bold> " . $ message ;
64
- $ this ->say ($ message );
65
- }
66
-
67
45
/**
68
46
* Format text as a question.
69
47
*
@@ -77,88 +55,6 @@ protected function formatQuestion($message): string {
77
55
return "<question> $ message</question> " ;
78
56
}
79
57
80
- /**
81
- * Asks the user a multiple-choice question.
82
- *
83
- * @param string $question
84
- * The question text.
85
- * @param string[] $options
86
- * An array of available options.
87
- * @param mixed $default
88
- * Default.
89
- *
90
- * @return string
91
- * The chosen option.
92
- */
93
- protected function askChoice (string $ question , array $ options , mixed $ default = NULL ): string {
94
- return $ this ->doAsk (new ChoiceQuestion ($ this ->formatQuestion ($ question ),
95
- $ options , $ default ));
96
- }
97
-
98
- /**
99
- * Asks a required question.
100
- *
101
- * @param string $message
102
- * The question text.
103
- *
104
- * @return string
105
- * The response.
106
- */
107
- protected function askRequired (string $ message ): string {
108
- $ question = new Question ($ this ->formatQuestion ($ message ));
109
- $ question ->setValidator (function ($ answer ) {
110
- if (empty ($ answer )) {
111
- throw new \RuntimeException (
112
- 'You must enter a value! '
113
- );
114
- }
115
-
116
- return $ answer ;
117
- });
118
- return $ this ->doAsk ($ question );
119
- }
120
-
121
- /**
122
- * Writes an array to the screen as a formatted table.
123
- *
124
- * @param string[] $array
125
- * The unformatted array.
126
- * @param string[] $headers
127
- * The headers for the array. Defaults to ['Property','Value'].
128
- */
129
- protected function printArrayAsTable (
130
- array $ array ,
131
- array $ headers = ['Property ' , 'Value ' ]
132
- ): void {
133
- $ table = new Table ($ this ->output );
134
- $ table ->setHeaders ($ headers )
135
- ->setRows (ArrayManipulator::convertArrayToFlatTextArray ($ array ))
136
- ->render ();
137
- }
138
-
139
- /**
140
- * Writes a particular configuration key's value to the log.
141
- *
142
- * @param string[] $array
143
- * The configuration.
144
- * @param string $prefix
145
- * A prefix to add to each row in the configuration.
146
- * @param int $verbosity
147
- * The verbosity level at which to display the logged message.
148
- */
149
- protected function logConfig (array $ array , string $ prefix = '' , int $ verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE ): void {
150
- if ($ this ->getOutput ()->getVerbosity () >= $ verbosity ) {
151
- if ($ prefix ) {
152
- $ this ->getOutput ()->writeln ("<comment>Configuration for $ prefix:</comment> " );
153
- foreach ($ array as $ key => $ value ) {
154
- $ array ["$ prefix. $ key " ] = $ value ;
155
- unset($ array [$ key ]);
156
- }
157
- }
158
- $ this ->printArrayAsTable ($ array );
159
- }
160
- }
161
-
162
58
/**
163
59
* Asks a required question.
164
60
*
0 commit comments