| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 | <?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: yunwuxin <448901948@qq.com>// +----------------------------------------------------------------------namespace think\console\output;use think\console\Input;use think\console\Output;use think\console\output\question\Choice;use think\console\output\question\Confirmation;class Ask{    private static $stty;    private static $shell;    /** @var  Input */    protected $input;    /** @var  Output */    protected $output;    /** @var  Question */    protected $question;    public function __construct(Input $input, Output $output, Question $question)    {        $this->input    = $input;        $this->output   = $output;        $this->question = $question;    }    public function run()    {        if (!$this->input->isInteractive()) {            return $this->question->getDefault();        }        if (!$this->question->getValidator()) {            return $this->doAsk();        }        $that = $this;        $interviewer = function () use ($that) {            return $that->doAsk();        };        return $this->validateAttempts($interviewer);    }    protected function doAsk()    {        $this->writePrompt();        $inputStream  = STDIN;        $autocomplete = $this->question->getAutocompleterValues();        if (null === $autocomplete || !$this->hasSttyAvailable()) {            $ret = false;            if ($this->question->isHidden()) {                try {                    $ret = trim($this->getHiddenResponse($inputStream));                } catch (\RuntimeException $e) {                    if (!$this->question->isHiddenFallback()) {                        throw $e;                    }                }            }            if (false === $ret) {                $ret = fgets($inputStream, 4096);                if (false === $ret) {                    throw new \RuntimeException('Aborted');                }                $ret = trim($ret);            }        } else {            $ret = trim($this->autocomplete($inputStream));        }        $ret = strlen($ret) > 0 ? $ret : $this->question->getDefault();        if ($normalizer = $this->question->getNormalizer()) {            return $normalizer($ret);        }        return $ret;    }    private function autocomplete($inputStream)    {        $autocomplete = $this->question->getAutocompleterValues();        $ret          = '';        $i          = 0;        $ofs        = -1;        $matches    = $autocomplete;        $numMatches = count($matches);        $sttyMode = shell_exec('stty -g');        shell_exec('stty -icanon -echo');        while (!feof($inputStream)) {            $c = fread($inputStream, 1);            if ("\177" === $c) {                if (0 === $numMatches && 0 !== $i) {                    --$i;                    $this->output->write("\033[1D");                }                if ($i === 0) {                    $ofs        = -1;                    $matches    = $autocomplete;                    $numMatches = count($matches);                } else {                    $numMatches = 0;                }                $ret = substr($ret, 0, $i);            } elseif ("\033" === $c) {                $c .= fread($inputStream, 2);                if (isset($c[2]) && ('A' === $c[2] || 'B' === $c[2])) {                    if ('A' === $c[2] && -1 === $ofs) {                        $ofs = 0;                    }                    if (0 === $numMatches) {                        continue;                    }                    $ofs += ('A' === $c[2]) ? -1 : 1;                    $ofs = ($numMatches + $ofs) % $numMatches;                }            } elseif (ord($c) < 32) {                if ("\t" === $c || "\n" === $c) {                    if ($numMatches > 0 && -1 !== $ofs) {                        $ret = $matches[$ofs];                        $this->output->write(substr($ret, $i));                        $i = strlen($ret);                    }                    if ("\n" === $c) {                        $this->output->write($c);                        break;                    }                    $numMatches = 0;                }                continue;            } else {                $this->output->write($c);                $ret .= $c;                ++$i;                $numMatches = 0;                $ofs        = 0;                foreach ($autocomplete as $value) {                    if (0 === strpos($value, $ret) && $i !== strlen($value)) {                        $matches[$numMatches++] = $value;                    }                }            }            $this->output->write("\033[K");            if ($numMatches > 0 && -1 !== $ofs) {                $this->output->write("\0337");                $this->output->highlight(substr($matches[$ofs], $i));                $this->output->write("\0338");            }        }        shell_exec(sprintf('stty %s', $sttyMode));        return $ret;    }    protected function getHiddenResponse($inputStream)    {        if ('\\' === DIRECTORY_SEPARATOR) {            $exe = __DIR__ . '/../bin/hiddeninput.exe';            $value = rtrim(shell_exec($exe));            $this->output->writeln('');            if (isset($tmpExe)) {                unlink($tmpExe);            }            return $value;        }        if ($this->hasSttyAvailable()) {            $sttyMode = shell_exec('stty -g');            shell_exec('stty -echo');            $value = fgets($inputStream, 4096);            shell_exec(sprintf('stty %s', $sttyMode));            if (false === $value) {                throw new \RuntimeException('Aborted');            }            $value = trim($value);            $this->output->writeln('');            return $value;        }        if (false !== $shell = $this->getShell()) {            $readCmd = $shell === 'csh' ? 'set mypassword = $<' : 'read -r mypassword';            $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);            $value   = rtrim(shell_exec($command));            $this->output->writeln('');            return $value;        }        throw new \RuntimeException('Unable to hide the response.');    }    protected function validateAttempts($interviewer)    {        /** @var \Exception $error */        $error    = null;        $attempts = $this->question->getMaxAttempts();        while (null === $attempts || $attempts--) {            if (null !== $error) {                $this->output->error($error->getMessage());            }            try {                return call_user_func($this->question->getValidator(), $interviewer());            } catch (\Exception $error) {            }        }        throw $error;    }    /**     * 显示问题的提示信息     */    protected function writePrompt()    {        $text    = $this->question->getQuestion();        $default = $this->question->getDefault();        switch (true) {            case null === $default:                $text = sprintf(' <info>%s</info>:', $text);                break;            case $this->question instanceof Confirmation:                $text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');                break;            case $this->question instanceof Choice && $this->question->isMultiselect():                $choices = $this->question->getChoices();                $default = explode(',', $default);                foreach ($default as $key => $value) {                    $default[$key] = $choices[trim($value)];                }                $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default));                break;            case $this->question instanceof Choice:                $choices = $this->question->getChoices();                $text    = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);                break;            default:                $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);        }        $this->output->writeln($text);        if ($this->question instanceof Choice) {            $width = max(array_map('strlen', array_keys($this->question->getChoices())));            foreach ($this->question->getChoices() as $key => $value) {                $this->output->writeln(sprintf("  [<comment>%-${width}s</comment>] %s", $key, $value));            }        }        $this->output->write(' > ');    }    private function getShell()    {        if (null !== self::$shell) {            return self::$shell;        }        self::$shell = false;        if (file_exists('/usr/bin/env')) {            $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";            foreach (['bash', 'zsh', 'ksh', 'csh'] as $sh) {                if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {                    self::$shell = $sh;                    break;                }            }        }        return self::$shell;    }    private function hasSttyAvailable()    {        if (null !== self::$stty) {            return self::$stty;        }        exec('stty 2>&1', $output, $exitcode);        return self::$stty = $exitcode === 0;    }}
 |