Skip to content

Commit

Permalink
Move process outputter to logic to dedicated class, output process to…
Browse files Browse the repository at this point in the history
… STDERR, add color output to process output
  • Loading branch information
SpacePossum committed Apr 2, 2015
1 parent 2727520 commit 2bc83ad
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 53 deletions.
22 changes: 4 additions & 18 deletions Symfony/CS/Console/Command/FixCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use Symfony\CS\Config\Config;
use Symfony\CS\ConfigInterface;
use Symfony\CS\Console\ConfigurationResolver;
use Symfony\CS\Console\Output\ProcessOutput;
use Symfony\CS\ErrorsManager;
use Symfony\CS\Fixer;
use Symfony\CS\FixerFileProcessedEvent;
use Symfony\CS\FixerInterface;
use Symfony\CS\Linter\Linter;
use Symfony\CS\Utils;
Expand Down Expand Up @@ -343,7 +343,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->resolve()
;

$config = $resolver->getConfig();
$config = $resolver->getConfig();
$configFile = $resolver->getConfigFile();

if ($configFile && 'txt' === $input->getOption('format')) {
Expand All @@ -359,31 +359,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
$showProgress = $resolver->getProgress();

if ($showProgress) {
$fileProcessedEventListener = function (FixerFileProcessedEvent $event) use ($output) {
$output->write($event->getStatusAsString());
};

$this->fixer->setEventDispatcher($this->eventDispatcher);
$this->eventDispatcher->addListener(FixerFileProcessedEvent::NAME, $fileProcessedEventListener);
$progressOutput = new ProcessOutput($this->eventDispatcher);
}

$this->stopwatch->start('fixFiles');
$changed = $this->fixer->fix($config, $resolver->isDryRun(), $input->getOption('diff'));
$this->stopwatch->stop('fixFiles');

if ($showProgress) {
$progressOutput->printLegend();
$this->fixer->setEventDispatcher(null);
$this->eventDispatcher->removeListener(FixerFileProcessedEvent::NAME, $fileProcessedEventListener);
$output->writeln('');

$legend = array();
foreach (FixerFileProcessedEvent::getStatusMap() as $status) {
if ($status['symbol'] && $status['description']) {
$legend[] = $status['symbol'].'-'.$status['description'];
}
}

$output->writeln('Legend: '.implode(', ', array_unique($legend)));
}

$i = 1;
Expand Down
96 changes: 96 additions & 0 deletions Symfony/CS/Console/Output/ProcessOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the PHP CS utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\CS\Console\Output;

use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\CS\FixerFileProcessedEvent;

/**
* Output writer to show the process of a FixCommand.
*
* @internal
*/
class ProcessOutput
{
/**
* File statuses map.
*
* @var array
*/
private static $eventStatusMap = array(
FixerFileProcessedEvent::STATUS_UNKNOWN => array('symbol' => '?', 'format' => '%s', 'description' => 'unknown'),
FixerFileProcessedEvent::STATUS_INVALID => array('symbol' => 'I', 'format' => '<bg=red>%s</bg=red>', 'description' => 'invalid file syntax, file ignored'),
FixerFileProcessedEvent::STATUS_SKIPPED => array('symbol' => 'S', 'format' => '<fg=cyan>%s</fg=cyan>', 'description' => 'Skipped'),
FixerFileProcessedEvent::STATUS_NO_CHANGES => array('symbol' => '.', 'format' => '%s', 'description' => 'no changes'),
FixerFileProcessedEvent::STATUS_FIXED => array('symbol' => 'F', 'format' => '<fg=green>%s</fg=green>', 'description' => 'fixed'),
FixerFileProcessedEvent::STATUS_EXCEPTION => array('symbol' => 'E', 'format' => '<bg=red>%s</bg=red>', 'description' => 'error'),
FixerFileProcessedEvent::STATUS_LINT => array('symbol' => 'E', 'format' => '<bg=red>%s</bg=red>', 'description' => 'error'),
);

/**
* Event dispatcher instance.
*
* @var EventDispatcher
*/
private $eventDispatcher;

/**
* Stream output instance.
*
* @var StreamOutput
*/
private $output;

/**
* File pointer of type stream.
*
* @var resource
*/
private $stdErr;

public function __construct(EventDispatcher $dispatcher)
{
$this->stdErr = fopen('php://stderr', 'w');
$this->output = new StreamOutput($this->stdErr);
$this->eventDispatcher = $dispatcher;
$this->eventDispatcher->addListener(FixerFileProcessedEvent::NAME, array($this, 'onFixerFileProcessed'));
}

public function onFixerFileProcessed(FixerFileProcessedEvent $event)
{
$status = self::$eventStatusMap[$event->getStatus()];
$this->output->write($this->output->isDecorated() ? sprintf($status['format'], $status['symbol']) : $status['symbol']);
}

public function printLegend()
{
$symbols = array();

foreach (self::$eventStatusMap as $status) {
$symbol = $status['symbol'];
if ('' === $symbol || isset($symbols[$symbol])) {
continue;
}

$symbols[$symbol] = sprintf('%s-%s', $this->output->isDecorated() ? sprintf($status['format'], $symbol) : $symbol, $status['description']);
}

$this->output->write(sprintf("\nLegend: %s\n", implode(', ', $symbols)));
}

public function __destruct()
{
fclose($this->stdErr);
$this->eventDispatcher->removeListener(FixerFileProcessedEvent::NAME, array($this, 'onFixerFileProcessed'));
}
}
35 changes: 0 additions & 35 deletions Symfony/CS/FixerFileProcessedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ class FixerFileProcessedEvent extends Event
const STATUS_EXCEPTION = 5;
const STATUS_LINT = 6;

/**
* File statuses map.
*
* @var array
*/
private static $statusMap = array(
self::STATUS_UNKNOWN => array('symbol' => '?', 'description' => 'unknown'),
self::STATUS_INVALID => array('symbol' => 'I', 'description' => 'invalid file syntax, file ignored'),
self::STATUS_SKIPPED => array('symbol' => '', 'description' => ''),
self::STATUS_NO_CHANGES => array('symbol' => '.', 'description' => 'no changes'),
self::STATUS_FIXED => array('symbol' => 'F', 'description' => 'fixed'),
self::STATUS_EXCEPTION => array('symbol' => 'E', 'description' => 'error'),
self::STATUS_LINT => array('symbol' => 'E', 'description' => 'error'),
);

/**
* File status.
*
Expand All @@ -67,16 +52,6 @@ public static function create()
return new static();
}

/**
* Get status map.
*
* @return array
*/
public static function getStatusMap()
{
return self::$statusMap;
}

/**
* Get status.
*
Expand All @@ -87,16 +62,6 @@ public function getStatus()
return $this->status;
}

/**
* Get status as string.
*
* @return string
*/
public function getStatusAsString()
{
return self::$statusMap[$this->status]['symbol'];
}

/**
* Set status.
*
Expand Down

0 comments on commit 2bc83ad

Please sign in to comment.