@@ -34,6 +34,7 @@ class QuestionHelper extends Helper
3434 private $ inputStream ;
3535 private static $ shell ;
3636 private static $ stty = true ;
37+ private static $ stdinIsInteractive ;
3738
3839 /**
3940 * Asks a question to the user.
@@ -417,33 +418,26 @@ private function getHiddenResponse(OutputInterface $output, $inputStream, bool $
417418
418419 if (self ::$ stty && Terminal::hasSttyAvailable ()) {
419420 $ sttyMode = shell_exec ('stty -g ' );
420-
421421 shell_exec ('stty -echo ' );
422- $ value = fgets ($ inputStream , 4096 );
423- shell_exec (sprintf ('stty %s ' , $ sttyMode ));
422+ } elseif ($ this ->isInteractiveInput ($ inputStream )) {
423+ throw new RuntimeException ('Unable to hide the response. ' );
424+ }
424425
425- if (false === $ value ) {
426- throw new MissingInputException ('Aborted. ' );
427- }
428- if ($ trimmable ) {
429- $ value = trim ($ value );
430- }
431- $ output ->writeln ('' );
426+ $ value = fgets ($ inputStream , 4096 );
432427
433- return $ value ;
428+ if (self ::$ stty && Terminal::hasSttyAvailable ()) {
429+ shell_exec (sprintf ('stty %s ' , $ sttyMode ));
434430 }
435431
436- if (false !== $ shell = $ this ->getShell ()) {
437- $ readCmd = 'csh ' === $ shell ? 'set mypassword = $< ' : 'read -r mypassword ' ;
438- $ command = sprintf ("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword' 2> /dev/null " , $ shell , $ readCmd );
439- $ sCommand = shell_exec ($ command );
440- $ value = $ trimmable ? rtrim ($ sCommand ) : $ sCommand ;
441- $ output ->writeln ('' );
442-
443- return $ value ;
432+ if (false === $ value ) {
433+ throw new MissingInputException ('Aborted. ' );
434+ }
435+ if ($ trimmable ) {
436+ $ value = trim ($ value );
444437 }
438+ $ output ->writeln ('' );
445439
446- throw new RuntimeException ( ' Unable to hide the response. ' ) ;
440+ return $ value ;
447441 }
448442
449443 /**
@@ -471,56 +465,35 @@ private function validateAttempts(callable $interviewer, OutputInterface $output
471465 throw $ e ;
472466 } catch (\Exception $ error ) {
473467 }
474-
475- $ attempts = $ attempts ?? -(int ) $ this ->askForever ();
476468 }
477469
478470 throw $ error ;
479471 }
480472
481- /**
482- * Returns a valid unix shell.
483- *
484- * @return string|bool The valid shell name, false in case no valid shell is found
485- */
486- private function getShell ()
473+ private function isInteractiveInput ($ inputStream ): bool
487474 {
488- if (null !== self :: $ shell ) {
489- return self :: $ shell ;
475+ if (' php://stdin ' !== ( stream_get_meta_data ( $ inputStream )[ ' uri ' ] ?? null ) ) {
476+ return false ;
490477 }
491478
492- self ::$ shell = false ;
493-
494- if (file_exists ('/usr/bin/env ' )) {
495- // handle other OSs with bash/zsh/ksh/csh if available to hide the answer
496- $ test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null " ;
497- foreach (['bash ' , 'zsh ' , 'ksh ' , 'csh ' ] as $ sh ) {
498- if ('OK ' === rtrim (shell_exec (sprintf ($ test , $ sh )))) {
499- self ::$ shell = $ sh ;
500- break ;
501- }
502- }
503- }
504-
505- return self ::$ shell ;
506- }
507-
508- private function askForever (): bool
509- {
510- $ inputStream = $ this ->inputStream ?: fopen ('php://stdin ' , 'r ' );
511-
512- if ('php://stdin ' !== (stream_get_meta_data ($ inputStream )['url ' ] ?? null )) {
513- return true ;
479+ if (null !== self ::$ stdinIsInteractive ) {
480+ return self ::$ stdinIsInteractive ;
514481 }
515482
516483 if (\function_exists ('stream_isatty ' )) {
517- return stream_isatty ($ inputStream );
484+ return self :: $ stdinIsInteractive = stream_isatty (fopen ( ' php://stdin ' , ' r ' ) );
518485 }
519486
520487 if (\function_exists ('posix_isatty ' )) {
521- return posix_isatty ($ inputStream );
488+ return self :: $ stdinIsInteractive = posix_isatty (fopen ( ' php://stdin ' , ' r ' ) );
522489 }
523490
524- return true ;
491+ if (!\function_exists ('exec ' )) {
492+ return self ::$ stdinIsInteractive = true ;
493+ }
494+
495+ exec ('stty 2> /dev/null ' , $ output , $ status );
496+
497+ return self ::$ stdinIsInteractive = 1 !== $ status ;
525498 }
526499}
0 commit comments