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;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Helper\HelperSet;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
class ConsoleIO implements IOInterface
|
|
|
|
{
|
|
|
|
protected $input;
|
|
|
|
protected $output;
|
|
|
|
protected $helperSet;
|
2012-11-07 12:33:50 +00:00
|
|
|
protected $authentications = array();
|
2012-03-06 18:08:15 +00:00
|
|
|
protected $lastMessage;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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()
|
|
|
|
{
|
2012-11-02 17:12:08 +00:00
|
|
|
return $this->output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE;
|
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)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
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
|
|
|
{
|
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
|
|
|
|
$size = strlen(strip_tags($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
|
|
|
|
$this->write(str_repeat("\x08", $size), false);
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-03-06 18:08:15 +00:00
|
|
|
// write the new message
|
2012-01-18 09:35:02 +00:00
|
|
|
$this->write($messages, false);
|
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
|
|
|
|
$this->write(str_repeat(' ', $fill), false);
|
|
|
|
// move the cursor back
|
|
|
|
$this->write(str_repeat("\x08", $fill), false);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($newline) {
|
2012-01-18 09:35:02 +00:00
|
|
|
$this->write('');
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
2012-03-06 18:08:15 +00:00
|
|
|
$this->lastMessage = $messages;
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function ask($question, $default = null)
|
|
|
|
{
|
|
|
|
return $this->helperSet->get('dialog')->ask($this->output, $question, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function askConfirmation($question, $default = true)
|
|
|
|
{
|
|
|
|
return $this->helperSet->get('dialog')->askConfirmation($this->output, $question, $default);
|
|
|
|
}
|
|
|
|
|
2012-01-17 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-16 13:14:15 +00:00
|
|
|
public function askAndValidate($question, $validator, $attempts = false, $default = null)
|
|
|
|
{
|
|
|
|
return $this->helperSet->get('dialog')->askAndValidate($this->output, $question, $validator, $attempts, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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')) {
|
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';
|
|
|
|
copy($exe, $tmpExe);
|
|
|
|
$exe = $tmpExe;
|
|
|
|
}
|
2012-01-16 13:14:15 +00:00
|
|
|
|
2012-01-18 12:07:49 +00:00
|
|
|
$this->write($question, false);
|
2012-04-01 18:59:50 +00:00
|
|
|
$value = rtrim(shell_exec($exe));
|
2012-03-25 15:35:47 +00:00
|
|
|
$this->write('');
|
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)) {
|
|
|
|
$this->write($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));
|
|
|
|
$this->write('');
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-11-07 12:33:50 +00:00
|
|
|
public function getAuthentications()
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-11-07 12:33:50 +00:00
|
|
|
return $this->authentications;
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-11-07 12:33:50 +00:00
|
|
|
public function hasAuthentication($repositoryName)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-11-07 12:33:50 +00:00
|
|
|
$auths = $this->getAuthentications();
|
2012-05-06 15:17:36 +00:00
|
|
|
|
2012-01-17 13:53:50 +00:00
|
|
|
return isset($auths[$repositoryName]);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-11-07 12:33:50 +00:00
|
|
|
public function getAuthentication($repositoryName)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-11-07 12:33:50 +00:00
|
|
|
$auths = $this->getAuthentications();
|
2012-05-06 15:17:36 +00:00
|
|
|
|
2012-01-16 13:14:15 +00:00
|
|
|
return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-11-07 12:33:50 +00:00
|
|
|
public function setAuthentication($repositoryName, $username, $password = null)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-11-07 12:33:50 +00:00
|
|
|
$this->authentications[$repositoryName] = array('username' => $username, 'password' => $password);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
}
|