Use ConsoleOutput to determine color support

main
johnstevenson 8 years ago
parent 4249bd1456
commit 896d1d71f8

@ -7,12 +7,16 @@ if (PHP_SAPI !== 'cli') {
require __DIR__.'/../src/bootstrap.php'; require __DIR__.'/../src/bootstrap.php';
use Composer\Factory;
use Composer\XdebugHandler; use Composer\XdebugHandler;
use Composer\Console\Application; use Composer\Console\Application;
error_reporting(-1); error_reporting(-1);
$xdebug = new XdebugHandler(); // Create output for XdebugHandler and Application
$output = Factory::createOutput();
$xdebug = new XdebugHandler($output);
$xdebug->check(); $xdebug->check();
unset($xdebug); unset($xdebug);
@ -46,4 +50,4 @@ if (function_exists('ini_set')) {
// run the command application // run the command application
$application = new Application(); $application = new Application();
$application->run(); $application->run(null, $output);

@ -18,8 +18,6 @@ use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Composer\Command; use Composer\Command;
use Composer\Composer; use Composer\Composer;
use Composer\Factory; use Composer\Factory;
@ -96,9 +94,7 @@ class Application extends BaseApplication
public function run(InputInterface $input = null, OutputInterface $output = null) public function run(InputInterface $input = null, OutputInterface $output = null)
{ {
if (null === $output) { if (null === $output) {
$styles = Factory::createAdditionalStyles(); $output = Factory::createOutput();
$formatter = new OutputFormatter(null, $styles);
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
} }
return parent::run($input, $output); return parent::run($input, $output);

@ -28,7 +28,9 @@ use Composer\Util\Silencer;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
use Composer\EventDispatcher\Event; use Composer\EventDispatcher\Event;
use Seld\JsonLint\DuplicateKeyException; use Seld\JsonLint\DuplicateKeyException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;
use Composer\EventDispatcher\EventDispatcher; use Composer\EventDispatcher\EventDispatcher;
use Composer\Autoload\AutoloadGenerator; use Composer\Autoload\AutoloadGenerator;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
@ -225,6 +227,19 @@ class Factory
); );
} }
/**
* Creates a ConsoleOutput instance
*
* @return ConsoleOutput
*/
public static function createOutput()
{
$styles = self::createAdditionalStyles();
$formatter = new OutputFormatter(null, $styles);
return new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
}
/** /**
* @deprecated Use Composer\Repository\RepositoryFactory::defaultRepos instead * @deprecated Use Composer\Repository\RepositoryFactory::defaultRepos instead
*/ */

@ -12,6 +12,8 @@
namespace Composer; namespace Composer;
use Symfony\Component\Console\Output\OutputInterface;
/** /**
* @author John Stevenson <john-stevenson@blueyonder.co.uk> * @author John Stevenson <john-stevenson@blueyonder.co.uk>
*/ */
@ -19,6 +21,7 @@ class XdebugHandler
{ {
const ENV_ALLOW = 'COMPOSER_ALLOW_XDEBUG'; const ENV_ALLOW = 'COMPOSER_ALLOW_XDEBUG';
private $output;
private $loaded; private $loaded;
private $tmpIni; private $tmpIni;
private $scanDir; private $scanDir;
@ -26,8 +29,9 @@ class XdebugHandler
/** /**
* Constructor * Constructor
*/ */
public function __construct() public function __construct(OutputInterface $output)
{ {
$this->output = $output;
$this->loaded = extension_loaded('xdebug'); $this->loaded = extension_loaded('xdebug');
$tmp = sys_get_temp_dir(); $tmp = sys_get_temp_dir();
$this->tmpIni = $tmp.'/composer-php.ini'; $this->tmpIni = $tmp.'/composer-php.ini';
@ -214,6 +218,9 @@ class XdebugHandler
/** /**
* Returns the restart script arguments, adding --ansi if required * Returns the restart script arguments, adding --ansi if required
* *
* If we are a terminal with color support we must ensure that the --ansi
* option is set, because the restarted output is piped.
*
* @param array $args The argv array * @param array $args The argv array
* *
* @return array * @return array
@ -224,7 +231,7 @@ class XdebugHandler
return $args; return $args;
} }
if ($this->isColorTerminal()) { if ($this->output->isDecorated()) {
$offset = count($args) > 1 ? 2: 1; $offset = count($args) > 1 ? 2: 1;
array_splice($args, $offset, 0, '--ansi'); array_splice($args, $offset, 0, '--ansi');
} }
@ -232,30 +239,6 @@ class XdebugHandler
return $args; return $args;
} }
/**
* Returns whether we are a terminal and have colour capabilities
*
* @return bool
*/
private function isColorTerminal()
{
if (function_exists('posix_isatty')) {
$result = posix_isatty(STDOUT);
} else {
// See if STDOUT is a character device (S_IFCHR)
$stat = fstat(STDOUT);
$result = ($stat['mode'] & 0170000) === 0020000;
}
if (defined('PHP_WINDOWS_VERSION_BUILD') && $result) {
$result = false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM');
}
return $result;
}
/** /**
* Escapes a string to be used as a shell argument. * Escapes a string to be used as a shell argument.
* *

@ -11,17 +11,21 @@
namespace Composer\Test\Mock; namespace Composer\Test\Mock;
use Composer\Factory;
use Composer\XdebugHandler; use Composer\XdebugHandler;
class XdebugHandlerMock extends XdebugHandler class XdebugHandlerMock extends XdebugHandler
{ {
public $command; public $command;
public $restarted; public $restarted;
public $output;
public function __construct($loaded) public function __construct($loaded = null)
{ {
parent::__construct(); $this->output = Factory::createOutput();
parent::__construct($this->output);
$loaded = $loaded === null ? true: $loaded;
$class = new \ReflectionClass(get_parent_class($this)); $class = new \ReflectionClass(get_parent_class($this));
$prop = $class->getProperty('loaded'); $prop = $class->getProperty('loaded');
$prop->setAccessible(true); $prop->setAccessible(true);

@ -12,10 +12,13 @@
namespace Composer\Test; namespace Composer\Test;
use Composer\Test\Mock\XdebugHandlerMock as XdebugHandler; use Composer\Test\Mock\XdebugHandlerMock;
/** /**
* @author John Stevenson <john-stevenson@blueyonder.co.uk> * @author John Stevenson <john-stevenson@blueyonder.co.uk>
*
* @backupGlobals disabled
* @runTestsInSeparateProcesses
*/ */
class XdebugHandlerTest extends \PHPUnit_Framework_TestCase class XdebugHandlerTest extends \PHPUnit_Framework_TestCase
{ {
@ -23,7 +26,7 @@ class XdebugHandlerTest extends \PHPUnit_Framework_TestCase
{ {
$loaded = true; $loaded = true;
$xdebug = new XdebugHandler($loaded); $xdebug = new XdebugHandlerMock($loaded);
$xdebug->check(); $xdebug->check();
$this->assertTrue($xdebug->restarted || !defined('PHP_BINARY')); $this->assertTrue($xdebug->restarted || !defined('PHP_BINARY'));
} }
@ -32,7 +35,7 @@ class XdebugHandlerTest extends \PHPUnit_Framework_TestCase
{ {
$loaded = false; $loaded = false;
$xdebug = new XdebugHandler($loaded); $xdebug = new XdebugHandlerMock($loaded);
$xdebug->check(); $xdebug->check();
$this->assertFalse($xdebug->restarted); $this->assertFalse($xdebug->restarted);
} }
@ -40,10 +43,31 @@ class XdebugHandlerTest extends \PHPUnit_Framework_TestCase
public function testNoRestartWhenLoadedAndAllowed() public function testNoRestartWhenLoadedAndAllowed()
{ {
$loaded = true; $loaded = true;
putenv(XdebugHandler::ENV_ALLOW.'=1'); putenv(XdebugHandlerMock::ENV_ALLOW.'=1');
$xdebug = new XdebugHandler($loaded); $xdebug = new XdebugHandlerMock($loaded);
$xdebug->check(); $xdebug->check();
$this->assertFalse($xdebug->restarted); $this->assertFalse($xdebug->restarted);
} }
public function testForceColorSupport()
{
$xdebug = new XdebugHandlerMock();
$xdebug->output->setDecorated(true);
$xdebug->check();
$args = explode(' ', $xdebug->command);
$this->assertTrue(in_array('--ansi', $args) || !defined('PHP_BINARY'));
}
public function testIgnoreColorSupportIfNoAnsi()
{
$xdebug = new XdebugHandlerMock();
$xdebug->output->setDecorated(true);
$_SERVER['argv'][] = '--no-ansi';
$xdebug->check();
$args = explode(' ', $xdebug->command);
$this->assertTrue(!in_array('--ansi', $args) || !defined('PHP_BINARY'));
}
} }

Loading…
Cancel
Save