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\Input\InputDefinition;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
|
|
|
|
use Symfony\Component\Console\Helper\HelperSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Input/Output helper.
|
|
|
|
*
|
|
|
|
* @author François Pluchino <francois.pluchino@opendisplay.com>
|
|
|
|
*/
|
|
|
|
class ConsoleIO implements IOInterface
|
|
|
|
{
|
|
|
|
protected $input;
|
|
|
|
protected $output;
|
|
|
|
protected $helperSet;
|
2012-01-17 13:53:50 +00:00
|
|
|
protected $authorizations = array();
|
2012-01-16 13:14:15 +00:00
|
|
|
protected $lastUsername;
|
|
|
|
protected $lastPassword;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function write($messages, $newline = false, $type = 0)
|
|
|
|
{
|
|
|
|
$this->output->write($messages, $newline, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function writeln($messages, $type = 0)
|
|
|
|
{
|
|
|
|
$this->output->writeln($messages, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function overwrite($messages, $size = 80, $newline = false, $type = 0)
|
|
|
|
{
|
|
|
|
for ($place = $size; $place > 0; $place--) {
|
|
|
|
$this->write("\x08");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->write($messages, false, $type);
|
|
|
|
|
|
|
|
for ($place = ($size - strlen($messages)); $place > 0; $place--) {
|
|
|
|
$this->write(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up the end line
|
|
|
|
for ($place = ($size - strlen($messages)); $place > 0; $place--) {
|
|
|
|
$this->write("\x08");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($newline) {
|
|
|
|
$this->writeln('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function overwriteln($messages, $size = 80, $type = 0)
|
|
|
|
{
|
|
|
|
$this->overwrite($messages, $size, true, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function setVerbosity($level)
|
|
|
|
{
|
|
|
|
$this->output->setVerbosity($level);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getVerbosity()
|
|
|
|
{
|
|
|
|
return $this->output->getVerbosity();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function setDecorated($decorated)
|
|
|
|
{
|
|
|
|
$this->output->setDecorated($decorated);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isDecorated()
|
|
|
|
{
|
|
|
|
return $this->output->isDecorated();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function setFormatter(OutputFormatterInterface $formatter)
|
|
|
|
{
|
|
|
|
$this->output->setFormatter($formatter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getFormatter()
|
|
|
|
{
|
|
|
|
return $this->output->getFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// for windows OS (does not hide the answer in the popup, but it never appears in the STDIN history)
|
|
|
|
if (preg_match('/^win/i', PHP_OS)) {
|
|
|
|
$vbscript = sys_get_temp_dir() . '/prompt_password.vbs';
|
|
|
|
file_put_contents($vbscript,
|
|
|
|
'wscript.echo(Inputbox("' . addslashes($question) . '","'
|
|
|
|
. addslashes($question) . '", ""))');
|
|
|
|
$command = "cscript //nologo " . escapeshellarg($vbscript);
|
|
|
|
|
|
|
|
$this->write($question);
|
|
|
|
|
|
|
|
$value = rtrim(shell_exec($command));
|
|
|
|
unlink($vbscript);
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($value); ++$i) {
|
|
|
|
$this->write('*');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->writeln('');
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for other OS with shell_exec (hide the answer)
|
2012-01-17 09:29:54 +00:00
|
|
|
if (rtrim(shell_exec($command)) === 'OK') {
|
2012-01-16 13:14:15 +00:00
|
|
|
$command = "/usr/bin/env bash -c 'echo OK'";
|
|
|
|
|
|
|
|
$this->write($question);
|
|
|
|
|
|
|
|
$command = "/usr/bin/env bash -c 'read -s mypassword && echo \$mypassword'";
|
|
|
|
$value = rtrim(shell_exec($command));
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($value); ++$i) {
|
|
|
|
$this->write('*');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->writeln('');
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for other OS without shell_exec (does not hide the answer)
|
|
|
|
$this->writeln('');
|
|
|
|
|
|
|
|
return $this->ask($question);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getLastUsername()
|
|
|
|
{
|
|
|
|
return $this->lastUsername;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getLastPassword()
|
|
|
|
{
|
|
|
|
return $this->lastPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-17 13:53:50 +00:00
|
|
|
public function getAuthorizations()
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-01-17 13:53:50 +00:00
|
|
|
return $this->authorizations;
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-17 13:53:50 +00:00
|
|
|
public function hasAuthorization($repositoryName)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-01-17 13:53:50 +00:00
|
|
|
$auths = $this->getAuthorizations();
|
|
|
|
return isset($auths[$repositoryName]);
|
2012-01-16 13:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-17 13:53:50 +00:00
|
|
|
public function getAuthorization($repositoryName)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-01-17 13:53:50 +00:00
|
|
|
$auths = $this->getAuthorizations();
|
2012-01-16 13:14:15 +00:00
|
|
|
return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-17 13:53:50 +00:00
|
|
|
public function setAuthorization($repositoryName, $username, $password = null)
|
2012-01-16 13:14:15 +00:00
|
|
|
{
|
2012-01-17 13:53:50 +00:00
|
|
|
$auths = $this->getAuthorizations();
|
2012-01-16 13:14:15 +00:00
|
|
|
$auths[$repositoryName] = array('username' => $username, 'password' => $password);
|
|
|
|
|
2012-01-17 13:53:50 +00:00
|
|
|
$this->authorizations = $auths;
|
2012-01-16 13:14:15 +00:00
|
|
|
$this->lastUsername = $username;
|
|
|
|
$this->lastPassword = $password;
|
|
|
|
}
|
|
|
|
}
|