22
33namespace Moox \Prompts \Filament \Components ;
44
5- use Filament \Forms \Components \TextInput ;
6- use Filament \Forms \Components \Textarea ;
7- use Filament \Forms \Components \Select ;
85use Filament \Forms \Components \Checkbox ;
96use Filament \Forms \Components \Placeholder ;
107use Filament \Forms \Components \Radio ;
8+ use Filament \Forms \Components \Select ;
9+ use Filament \Forms \Components \Textarea ;
10+ use Filament \Forms \Components \TextInput ;
1111use Filament \Forms \Concerns \InteractsWithForms ;
1212use Filament \Forms \Contracts \HasForms ;
13+ use Illuminate \Console \OutputStyle ;
1314use Livewire \Component ;
1415use Moox \Prompts \Support \PendingPromptsException ;
1516use Moox \Prompts \Support \PromptResponseStore ;
1617use Symfony \Component \Console \Input \ArrayInput ;
1718use Symfony \Component \Console \Output \BufferedOutput ;
18- use Illuminate \Console \OutputStyle ;
1919
2020class RunCommandComponent extends Component implements HasForms
2121{
2222 use InteractsWithForms;
2323
2424 public string $ command = '' ;
25+
2526 public array $ commandInput = [];
27+
2628 public ?array $ currentPrompt = null ;
29+
2730 public string $ output = '' ;
31+
2832 public bool $ isComplete = false ;
33+
2934 public ?string $ error = null ;
35+
3036 public array $ answers = [];
37+
3138 public array $ data = [];
3239
3340 protected PromptResponseStore $ responseStore ;
@@ -47,9 +54,9 @@ public function mount(string $command = '', array $commandInput = []): void
4754 $ this ->currentPrompt = null ;
4855 $ this ->output = '' ;
4956 $ this ->error = null ;
50-
57+
5158 $ this ->responseStore ->resetCounter ();
52-
59+
5360 if ($ command ) {
5461 $ this ->runCommand ();
5562 }
@@ -62,23 +69,24 @@ protected function runCommand(): void
6269
6370 try {
6471 $ this ->responseStore ->resetCounter ();
65-
72+
6673 foreach ($ this ->answers as $ promptId => $ answer ) {
6774 $ this ->responseStore ->set ($ promptId , $ answer );
6875 }
69-
76+
7077 app ()->instance ('moox.prompts.response_store ' , $ this ->responseStore );
7178
7279 $ commandInstance = app (\Illuminate \Contracts \Console \Kernel::class)
7380 ->all ()[$ this ->command ] ?? null ;
7481
75- if (!$ commandInstance ) {
82+ if (! $ commandInstance ) {
7683 $ this ->error = "Command nicht gefunden: {$ this ->command }" ;
84+
7785 return ;
7886 }
7987
8088 $ commandInstance ->setLaravel (app ());
81- $ output = new BufferedOutput () ;
89+ $ output = new BufferedOutput ;
8290
8391 $ outputStyle = new OutputStyle (
8492 new ArrayInput ($ this ->commandInput ),
@@ -103,7 +111,7 @@ protected function runCommand(): void
103111 if ($ promptId && isset ($ this ->answers [$ promptId ])) {
104112 $ value = $ this ->answers [$ promptId ];
105113 if ($ prompt ['method ' ] === 'multiselect ' ) {
106- if (!is_array ($ value )) {
114+ if (! is_array ($ value )) {
107115 if ($ value === true ) {
108116 $ params = $ prompt ['params ' ] ?? [];
109117 $ options = $ params [1 ] ?? [];
@@ -124,12 +132,12 @@ protected function runCommand(): void
124132
125133 public function submitPrompt (): void
126134 {
127- if (!$ this ->currentPrompt ) {
135+ if (! $ this ->currentPrompt ) {
128136 return ;
129137 }
130138
131139 $ promptId = $ this ->currentPrompt ['id ' ] ?? null ;
132- if (!$ promptId ) {
140+ if (! $ promptId ) {
133141 return ;
134142 }
135143
@@ -138,9 +146,9 @@ public function submitPrompt(): void
138146 $ params = $ this ->currentPrompt ['params ' ] ?? [];
139147 $ options = $ params [1 ] ?? [];
140148 $ answer = [];
141-
149+
142150 foreach (array_keys ($ options ) as $ key ) {
143- $ checkboxId = $ promptId . '_ ' . $ key ;
151+ $ checkboxId = $ promptId. '_ ' . $ key ;
144152 if (isset ($ this ->data [$ checkboxId ]) && $ this ->data [$ checkboxId ] === true ) {
145153 $ answer [] = $ key ;
146154 }
@@ -152,9 +160,9 @@ public function submitPrompt(): void
152160 if ($ this ->currentPrompt ['method ' ] !== 'multiselect ' && ($ answer === null || ($ answer === '' && $ this ->currentPrompt ['method ' ] !== 'confirm ' ))) {
153161 $ allRequestData = request ()->all ();
154162 $ updates = data_get ($ allRequestData , 'components.0.updates ' , []);
155-
163+
156164 if (is_array ($ updates ) || is_object ($ updates )) {
157- $ updateKey = 'data. ' . $ promptId ;
165+ $ updateKey = 'data. ' . $ promptId ;
158166 if (isset ($ updates [$ updateKey ])) {
159167 $ answer = $ updates [$ updateKey ];
160168 }
@@ -163,7 +171,7 @@ public function submitPrompt(): void
163171 }
164172 if ($ answer === null ) {
165173 foreach ($ updates as $ key => $ value ) {
166- if (str_ends_with ($ key , '. ' . $ promptId ) || $ key === $ promptId ) {
174+ if (str_ends_with ($ key , '. ' . $ promptId ) || $ key === $ promptId ) {
167175 $ answer = $ value ;
168176 break ;
169177 }
@@ -181,13 +189,13 @@ public function submitPrompt(): void
181189 $ answer = false ;
182190 }
183191
184- if (($ answer === null || $ answer === '' || ($ this ->currentPrompt ['method ' ] === 'multiselect ' && !is_array ($ answer ))) && $ this ->currentPrompt ['method ' ] !== 'confirm ' ) {
192+ if (($ answer === null || $ answer === '' || ($ this ->currentPrompt ['method ' ] === 'multiselect ' && ! is_array ($ answer ))) && $ this ->currentPrompt ['method ' ] !== 'confirm ' ) {
185193 try {
186194 $ data = $ this ->form ->getState ();
187195 $ answer = $ data [$ promptId ] ?? null ;
188-
196+
189197 if ($ this ->currentPrompt ['method ' ] === 'multiselect ' ) {
190- if (!is_array ($ answer )) {
198+ if (! is_array ($ answer )) {
191199 if ($ answer === true ) {
192200 $ params = $ this ->currentPrompt ['params ' ] ?? [];
193201 $ options = $ params [1 ] ?? [];
@@ -201,25 +209,25 @@ public function submitPrompt(): void
201209 return ;
202210 }
203211 }
204-
212+
205213 if (($ answer === null || $ answer === '' ) && $ this ->currentPrompt ['method ' ] === 'select ' ) {
206214 $ params = $ this ->currentPrompt ['params ' ] ?? [];
207215 $ options = $ params [1 ] ?? [];
208- if (!empty ($ options )) {
216+ if (! empty ($ options )) {
209217 $ answer = array_key_first ($ options );
210218 }
211219 }
212-
220+
213221 if ($ this ->currentPrompt ['method ' ] === 'confirm ' ) {
214222 if ($ answer === null ) {
215223 return ;
216224 }
217225 } elseif ($ answer === null || $ answer === '' ) {
218226 return ;
219227 }
220-
228+
221229 if ($ this ->currentPrompt ['method ' ] === 'multiselect ' ) {
222- if (!is_array ($ answer )) {
230+ if (! is_array ($ answer )) {
223231 if ($ answer === true ) {
224232 $ params = $ this ->currentPrompt ['params ' ] ?? [];
225233 $ options = $ params [1 ] ?? [];
@@ -230,18 +238,18 @@ public function submitPrompt(): void
230238 $ answer = [];
231239 }
232240 }
233-
234- if (!is_array ($ answer )) {
241+
242+ if (! is_array ($ answer )) {
235243 $ answer = [];
236244 }
237245 }
238-
246+
239247 if ($ this ->currentPrompt ['method ' ] === 'confirm ' ) {
240- if (!is_bool ($ answer )) {
248+ if (! is_bool ($ answer )) {
241249 $ answer = (bool ) $ answer ;
242250 }
243251 }
244-
252+
245253 $ this ->answers [$ promptId ] = $ answer ;
246254 $ this ->currentPrompt = null ;
247255 $ this ->runCommand ();
@@ -264,7 +272,7 @@ public function form(\Filament\Schemas\Schema $schema): \Filament\Schemas\Schema
264272
265273 protected function getFormSchema (): array
266274 {
267- if ($ this ->isComplete || !$ this ->currentPrompt ) {
275+ if ($ this ->isComplete || ! $ this ->currentPrompt ) {
268276 return [];
269277 }
270278
@@ -278,7 +286,7 @@ protected function getFormSchema(): array
278286
279287 $ field = $ this ->createFieldFromPrompt ($ promptId , $ method , $ params );
280288
281- if (!$ field ) {
289+ if (! $ field ) {
282290 return [];
283291 }
284292
@@ -291,23 +299,23 @@ protected function createMultiselectFields(string $promptId, array $params): arr
291299 $ required = ($ params [3 ] ?? false ) !== false ;
292300 $ defaultValue = $ this ->answers [$ promptId ] ?? null ;
293301 $ options = $ params [1 ] ?? [];
294-
302+
295303 $ fields = [];
296-
297- $ fields [] = Placeholder::make ($ promptId . '_label ' )
304+
305+ $ fields [] = Placeholder::make ($ promptId. '_label ' )
298306 ->label ($ label )
299307 ->content ('' );
300-
308+
301309 foreach ($ options as $ key => $ optionLabel ) {
302- $ checkboxId = $ promptId . '_ ' . $ key ;
310+ $ checkboxId = $ promptId. '_ ' . $ key ;
303311 $ isChecked = is_array ($ defaultValue ) && in_array ($ key , $ defaultValue );
304-
312+
305313 $ fields [] = Checkbox::make ($ checkboxId )
306314 ->label ($ optionLabel )
307315 ->default ($ isChecked )
308316 ->live (onBlur: false );
309317 }
310-
318+
311319 return $ fields ;
312320 }
313321
@@ -317,7 +325,7 @@ protected function createFieldFromPrompt(string $promptId, string $method, array
317325 $ required = ($ params [3 ] ?? false ) !== false ;
318326 $ defaultValue = $ this ->answers [$ promptId ] ?? null ;
319327
320- return match ($ method ) {
328+ return match ($ method ) {
321329 'text ' => TextInput::make ($ promptId )
322330 ->label ($ label )
323331 ->placeholder ($ params [1 ] ?? '' )
0 commit comments