1
0
Fork 0
composer/src/Composer/IO/ConsoleIO.php

276 lines
7.9 KiB
PHP
Raw Normal View History

<?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\ConsoleOutputInterface;
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;
2012-01-17 22:07:33 +00:00
/**
* The Input/Output helper.
*
* @author François Pluchino <francois.pluchino@opendisplay.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ConsoleIO extends BaseIO
{
protected $input;
protected $output;
protected $helperSet;
protected $lastMessage;
protected $lastMessageErr;
private $startTime;
private $verbosityMap;
/**
* 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-17 09:29:54 +00:00
public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet)
{
$this->input = $input;
$this->output = $output;
$this->helperSet = $helperSet;
$this->verbosityMap = array(
self::QUIET => OutputInterface::VERBOSITY_QUIET,
self::NORMAL => OutputInterface::VERBOSITY_NORMAL,
self::VERBOSE => OutputInterface::VERBOSITY_VERBOSE,
self::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE,
self::DEBUG => OutputInterface::VERBOSITY_DEBUG,
);
}
public function enableDebugging($startTime)
{
$this->startTime = $startTime;
}
/**
* {@inheritDoc}
*/
public function isInteractive()
{
return $this->input->isInteractive();
}
2012-04-26 12:54:34 +00:00
/**
* {@inheritDoc}
*/
public function isDecorated()
{
return $this->output->isDecorated();
}
/**
* {@inheritDoc}
*/
public function isVerbose()
{
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
}
/**
* {@inheritDoc}
*/
public function isVeryVerbose()
{
2015-04-30 10:10:17 +00:00
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE;
}
/**
* {@inheritDoc}
*/
public function isDebug()
{
2015-04-30 10:10:17 +00:00
return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG;
}
/**
* {@inheritDoc}
*/
public function write($messages, $newline = true, $verbosity = self::NORMAL)
{
$this->doWrite($messages, $newline, false, $verbosity);
}
/**
* {@inheritDoc}
*/
public function writeError($messages, $newline = true, $verbosity = self::NORMAL)
{
$this->doWrite($messages, $newline, true, $verbosity);
}
2015-02-18 09:03:45 +00:00
/**
2016-01-09 17:59:22 +00:00
* @param array|string $messages
* @param bool $newline
* @param bool $stderr
* @param int $verbosity
2015-02-18 09:03:45 +00:00
*/
private function doWrite($messages, $newline, $stderr, $verbosity)
{
$sfVerbosity = $this->verbosityMap[$verbosity];
if ($sfVerbosity > $this->output->getVerbosity()) {
return;
}
if (null !== $this->startTime) {
$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);
}
if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
$this->output->getErrorOutput()->write($messages, $newline, $sfVerbosity);
$this->lastMessageErr = join($newline ? "\n" : '', (array) $messages);
2015-04-15 00:21:03 +00:00
return;
}
$this->output->write($messages, $newline, $sfVerbosity);
$this->lastMessage = join($newline ? "\n" : '', (array) $messages);
}
/**
* {@inheritDoc}
*/
public function overwrite($messages, $newline = true, $size = null, $verbosity = self::NORMAL)
{
$this->doOverwrite($messages, $newline, $size, false, $verbosity);
2015-02-18 09:03:45 +00:00
}
/**
* {@inheritDoc}
*/
public function overwriteError($messages, $newline = true, $size = null, $verbosity = self::NORMAL)
2015-02-18 09:03:45 +00:00
{
$this->doOverwrite($messages, $newline, $size, true, $verbosity);
2015-02-18 09:03:45 +00:00
}
/**
2016-01-09 17:59:22 +00:00
* @param array|string $messages
* @param bool $newline
* @param int|null $size
* @param bool $stderr
* @param int $verbosity
2015-02-18 09:03:45 +00:00
*/
private function doOverwrite($messages, $newline, $size, $stderr, $verbosity)
2015-02-18 09:03:45 +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));
}
// ...let's fill its length with backspaces
$this->doWrite(str_repeat("\x08", $size), false, $stderr, $verbosity);
// write the new message
$this->doWrite($messages, false, $stderr, $verbosity);
$fill = $size - strlen(strip_tags($messages));
if ($fill > 0) {
// whitespace whatever has left
$this->doWrite(str_repeat(' ', $fill), false, $stderr, $verbosity);
// move the cursor back
$this->doWrite(str_repeat("\x08", $fill), false, $stderr, $verbosity);
}
if ($newline) {
$this->doWrite('', true, $stderr, $verbosity);
}
if ($stderr) {
$this->lastMessageErr = $messages;
} else {
$this->lastMessage = $messages;
}
}
/**
* {@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);
}
/**
* {@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-17 22:07:33 +00:00
/**
* {@inheritDoc}
*/
2015-04-30 10:09:56 +00:00
public function askAndValidate($question, $validator, $attempts = null, $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);
$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);
}
/**
* {@inheritDoc}
*/
public function askAndHideAnswer($question)
{
$this->writeError($question, false);
return \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
}
}