2012-01-16 13:14:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\IO;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2015-02-06 12:52:44 +00:00
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
2012-01-16 13:14:15 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Helper\HelperSet;
|
2015-04-30 10:09:56 +00:00
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
|
|
|
use Symfony\Component\Console\Question\Question;
|
2014-03-31 13:14:47 +00:00
|
|
|
use Symfony\Component\Process\ExecutableFinder;
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-01-17 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* The Input/Output helper.
|
|
|
|
*
|
|
|
|
* @author François Pluchino <francois.pluchino@opendisplay.com>
|
2012-05-06 15:17:36 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2012-01-16 13:14:15 +00:00
|
|
|
*/
|
2013-08-18 13:32:34 +00:00
|
|
|
class ConsoleIO extends BaseIO
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
|
|
|
protected $input;
|
|
|
|
protected $output;
|
|
|
|
protected $helperSet;
|
2012-03-06 18:08:15 +00:00
|
|
|
protected $lastMessage;
|
2015-02-06 12:52:44 +00:00
|
|
|
protected $lastMessageErr;
|
2013-03-05 14:21:54 +00:00
|
|
|
private $startTime;
|
2012-01-16 13:14:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2012-01-17 09:29:54 +00:00
|
|
|
* @param InputInterface $input The input instance
|
|
|
|
* @param OutputInterface $output The output instance
|
|
|
|
* @param HelperSet $helperSet The helperSet instance
|
2012-01-16 13:14:15 +00:00
|
|
|
*/
|
2012-01-17 09:29:54 +00:00
|
|
|
public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
|
|
|
$this->input = $input;
|
|
|
|
$this->output = $output;
|
|
|
|
$this->helperSet = $helperSet;
|
|
|
|
}
|
|
|
|
|
2013-03-05 14:21:54 +00:00
|
|
|
public function enableDebugging($startTime)
|
|
|
|
{
|
|
|
|
$this->startTime = $startTime;
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:14:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isInteractive()
|
|
|
|
{
|
|
|
|
return $this->input->isInteractive();
|
|
|
|
}
|
|
|
|
|
2012-04-26 12:54:34 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isDecorated()
|
|
|
|
{
|
|
|
|
return $this->output->isDecorated();
|
|
|
|
}
|
|
|
|
|
2012-04-01 02:32:00 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isVerbose()
|
|
|
|
{
|
2013-04-26 21:23:35 +00:00
|
|
|
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isVeryVerbose()
|
|
|
|
{
|
2013-04-27 10:30:58 +00:00
|
|
|
return $this->output->getVerbosity() >= 3; // OutputInterface::VERSOBITY_VERY_VERBOSE
|
2013-04-26 21:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isDebug()
|
|
|
|
{
|
2013-04-27 10:30:58 +00:00
|
|
|
return $this->output->getVerbosity() >= 4; // OutputInterface::VERBOSITY_DEBUG
|
2012-04-01 02:32:00 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 13:14:15 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-17 22:07:33 +00:00
|
|
|
public function write($messages, $newline = true)
|
2015-02-06 12:52:44 +00:00
|
|
|
{
|
|
|
|
$this->doWrite($messages, $newline, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function writeError($messages, $newline = true)
|
|
|
|
{
|
|
|
|
$this->doWrite($messages, $newline, true);
|
|
|
|
}
|
|
|
|
|
2015-02-18 09:03:45 +00:00
|
|
|
/**
|
|
|
|
* @param array $messages
|
|
|
|
* @param boolean $newline
|
|
|
|
* @param boolean $stderr
|
|
|
|
*/
|
2015-02-06 12:52:44 +00:00
|
|
|
private function doWrite($messages, $newline, $stderr)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2013-03-05 14:21:54 +00:00
|
|
|
if (null !== $this->startTime) {
|
2015-01-28 13:43:58 +00:00
|
|
|
$memoryUsage = memory_get_usage() / 1024 / 1024;
|
|
|
|
$timeSpent = microtime(true) - $this->startTime;
|
|
|
|
$messages = array_map(function ($message) use ($memoryUsage, $timeSpent) {
|
|
|
|
return sprintf('[%.1fMB/%.2fs] %s', $memoryUsage, $timeSpent, $message);
|
|
|
|
}, (array) $messages);
|
2013-03-05 14:21:54 +00:00
|
|
|
}
|
2015-02-06 12:52:44 +00:00
|
|
|
|
|
|
|
if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
|
|
|
|
$this->output->getErrorOutput()->write($messages, $newline);
|
|
|
|
$this->lastMessageErr = join($newline ? "\n" : '', (array) $messages);
|
2015-04-15 00:21:03 +00:00
|
|
|
|
2015-02-06 12:52:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-17 22:07:33 +00:00
|
|
|
$this->output->write($messages, $newline);
|
2012-03-06 18:08:15 +00:00
|
|
|
$this->lastMessage = join($newline ? "\n" : '', (array) $messages);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-03-06 18:08:15 +00:00
|
|
|
public function overwrite($messages, $newline = true, $size = null)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->doOverwrite($messages, $newline, $size, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function overwriteError($messages, $newline = true, $size = null)
|
|
|
|
{
|
|
|
|
$this->doOverwrite($messages, $newline, $size, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $messages
|
|
|
|
* @param boolean $newline
|
|
|
|
* @param integer $size
|
|
|
|
* @param boolean $stderr
|
|
|
|
*/
|
|
|
|
private function doOverwrite($messages, $newline, $size, $stderr)
|
|
|
|
{
|
|
|
|
if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
|
|
|
|
$output = $this->output->getErrorOutput();
|
|
|
|
} else {
|
|
|
|
$output = $this->output;
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:08:15 +00:00
|
|
|
// messages can be an array, let's convert it to string anyway
|
|
|
|
$messages = join($newline ? "\n" : '', (array) $messages);
|
|
|
|
|
|
|
|
// since overwrite is supposed to overwrite last message...
|
|
|
|
if (!isset($size)) {
|
|
|
|
// removing possible formatting of lastMessage with strip_tags
|
2015-02-18 09:03:45 +00:00
|
|
|
$size = strlen(strip_tags($stderr ? $this->lastMessageErr : $this->lastMessage));
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
2012-03-06 18:08:15 +00:00
|
|
|
// ...let's fill its length with backspaces
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->doWrite(str_repeat("\x08", $size), false, $stderr);
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-03-06 18:08:15 +00:00
|
|
|
// write the new message
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->doWrite($messages, false, $stderr);
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-03-06 18:08:15 +00:00
|
|
|
$fill = $size - strlen(strip_tags($messages));
|
|
|
|
if ($fill > 0) {
|
|
|
|
// whitespace whatever has left
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->doWrite(str_repeat(' ', $fill), false, $stderr);
|
2012-03-06 18:08:15 +00:00
|
|
|
// move the cursor back
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->doWrite(str_repeat("\x08", $fill), false, $stderr);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($newline) {
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->doWrite('', true, $stderr);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
2015-03-05 14:40:29 +00:00
|
|
|
|
|
|
|
if ($stderr) {
|
|
|
|
$this->lastMessageErr = $messages;
|
|
|
|
} else {
|
|
|
|
$this->lastMessage = $messages;
|
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function ask($question, $default = null)
|
|
|
|
{
|
2015-02-18 09:03:45 +00:00
|
|
|
$output = $this->output;
|
|
|
|
|
|
|
|
if ($output instanceof ConsoleOutputInterface) {
|
|
|
|
$output = $output->getErrorOutput();
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:09:56 +00:00
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
|
|
|
$helper = $this->helperSet->get('question');
|
|
|
|
$question = new Question($question, $default);
|
2015-02-18 09:03:45 +00:00
|
|
|
|
2015-04-30 10:09:56 +00:00
|
|
|
return $helper->ask($this->input, $output, $question);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function askConfirmation($question, $default = true)
|
|
|
|
{
|
2015-02-18 09:03:45 +00:00
|
|
|
$output = $this->output;
|
|
|
|
|
|
|
|
if ($output instanceof ConsoleOutputInterface) {
|
|
|
|
$output = $output->getErrorOutput();
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:09:56 +00:00
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
|
|
|
$helper = $this->helperSet->get('question');
|
|
|
|
$question = new ConfirmationQuestion($question, $default);
|
2015-02-18 09:03:45 +00:00
|
|
|
|
2015-04-30 10:09:56 +00:00
|
|
|
return $helper->ask($this->input, $output, $question);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2015-04-30 10:09:56 +00:00
|
|
|
public function askAndValidate($question, $validator, $attempts = null, $default = null)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2015-02-18 09:03:45 +00:00
|
|
|
$output = $this->output;
|
|
|
|
|
|
|
|
if ($output instanceof ConsoleOutputInterface) {
|
|
|
|
$output = $output->getErrorOutput();
|
|
|
|
}
|
|
|
|
|
2015-04-30 10:09:56 +00:00
|
|
|
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
|
|
|
|
$helper = $this->helperSet->get('question');
|
|
|
|
$question = new Question($question, $default);
|
|
|
|
$question->setValidator($validator);
|
|
|
|
$question->setMaxAttempts($attempts);
|
2015-02-18 09:03:45 +00:00
|
|
|
|
2015-04-30 10:09:56 +00:00
|
|
|
return $helper->ask($this->input, $output, $question);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function askAndHideAnswer($question)
|
|
|
|
{
|
2012-03-25 15:35:47 +00:00
|
|
|
// handle windows
|
2012-01-22 19:21:14 +00:00
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
|
2014-03-31 13:14:47 +00:00
|
|
|
$finder = new ExecutableFinder();
|
|
|
|
|
2014-03-29 16:26:43 +00:00
|
|
|
// use bash if it's present
|
2014-03-31 13:14:47 +00:00
|
|
|
if ($finder->find('bash') && $finder->find('stty')) {
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->writeError($question, false);
|
2014-07-05 12:49:45 +00:00
|
|
|
$value = rtrim(shell_exec('bash -c "stty -echo; read -n0 discard; read -r mypassword; stty echo; echo $mypassword"'));
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->writeError('');
|
2014-03-29 16:26:43 +00:00
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to hiddeninput executable
|
2012-04-01 18:59:50 +00:00
|
|
|
$exe = __DIR__.'\\hiddeninput.exe';
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-04-01 18:59:50 +00:00
|
|
|
// handle code running from a phar
|
|
|
|
if ('phar:' === substr(__FILE__, 0, 5)) {
|
|
|
|
$tmpExe = sys_get_temp_dir().'/hiddeninput.exe';
|
2013-04-28 15:03:05 +00:00
|
|
|
|
|
|
|
// use stream_copy_to_stream instead of copy
|
|
|
|
// to work around https://bugs.php.net/bug.php?id=64634
|
|
|
|
$source = fopen(__DIR__.'\\hiddeninput.exe', 'r');
|
|
|
|
$target = fopen($tmpExe, 'w+');
|
|
|
|
stream_copy_to_stream($source, $target);
|
|
|
|
fclose($source);
|
|
|
|
fclose($target);
|
|
|
|
unset($source, $target);
|
|
|
|
|
2012-04-01 18:59:50 +00:00
|
|
|
$exe = $tmpExe;
|
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->writeError($question, false);
|
2012-04-01 18:59:50 +00:00
|
|
|
$value = rtrim(shell_exec($exe));
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->writeError('');
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-04-01 19:34:09 +00:00
|
|
|
// clean up
|
|
|
|
if (isset($tmpExe)) {
|
|
|
|
unlink($tmpExe);
|
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2012-06-24 13:55:23 +00:00
|
|
|
if (file_exists('/usr/bin/env')) {
|
|
|
|
// handle other OSs with bash/zsh/ksh/csh if available to hide the answer
|
|
|
|
$test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null";
|
|
|
|
foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) {
|
|
|
|
if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
|
|
|
|
$shell = $sh;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($shell)) {
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->writeError($question, false);
|
2012-10-19 22:21:13 +00:00
|
|
|
$readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword';
|
2012-06-24 13:55:23 +00:00
|
|
|
$command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
|
|
|
|
$value = rtrim(shell_exec($command));
|
2015-02-18 09:03:45 +00:00
|
|
|
$this->writeError('');
|
2012-06-24 13:55:23 +00:00
|
|
|
|
|
|
|
return $value;
|
2012-06-24 09:14:13 +00:00
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
2012-04-01 18:47:31 +00:00
|
|
|
// not able to hide the answer, proceed with normal question handling
|
2012-01-16 13:14:15 +00:00
|
|
|
return $this->ask($question);
|
|
|
|
}
|
|
|
|
}
|