Skip to content

Commit

Permalink
Make progress bars the default output
Browse files Browse the repository at this point in the history
In this change I have reintroduced the progress bar view and made it the
default.
  • Loading branch information
mvriel committed Feb 22, 2019
1 parent 87aefda commit 53f18fc
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 13 deletions.
7 changes: 4 additions & 3 deletions config/packages/dev/monolog.yaml
Expand Up @@ -17,7 +17,8 @@ monolog:
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
verbosity_levels:
VERBOSITY_NORMAL: NOTICE
VERBOSITY_VERBOSE: INFO
VERBOSITY_VERY_VERBOSE: DEBUG
VERBOSITY_NORMAL: ERROR
VERBOSITY_VERBOSE: NOTICE
VERBOSITY_VERY_VERBOSE: INFO
VERBOSITY_DEBUG: DEBUG
formatter: console_formatter
Expand Up @@ -16,8 +16,19 @@
namespace phpDocumentor\Application\Console\Command\Project;

use League\Pipeline\Pipeline;
use Monolog\Logger;
use phpDocumentor\Application\Console\Command\Command;
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
use phpDocumentor\Event\Dispatcher;
use phpDocumentor\Parser\Event\PreFileEvent;
use phpDocumentor\Parser\Event\PreParsingEvent;
use phpDocumentor\Transformer\Event\PostTransformationEvent;
use phpDocumentor\Transformer\Event\PostTransformEvent;
use phpDocumentor\Transformer\Event\PreTransformEvent;
use phpDocumentor\Transformer\Transformer;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -41,25 +52,34 @@
*/
class RunCommand extends Command
{
/**
* @var ProjectDescriptorBuilder
*/
/** @var ProjectDescriptorBuilder */
private $projectDescriptorBuilder;

/**
* @var Pipeline
*/
/** @var Pipeline */
private $pipeline;

/** @var ProgressBar */
private $progressBar;

/** @var ProgressBar */
private $transformerProgressBar;

/** @var LoggerInterface */
private $logger;

/**
* RunCommand constructor.
*/
public function __construct(ProjectDescriptorBuilder $projectDescriptorBuilder, Pipeline $pipeline)
{
public function __construct(
ProjectDescriptorBuilder $projectDescriptorBuilder,
Pipeline $pipeline,
LoggerInterface $logger
) {
parent::__construct('project:run');

$this->projectDescriptorBuilder = $projectDescriptorBuilder;
$this->pipeline = $pipeline;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -246,13 +266,56 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('phpDocumentor ' . $this->getApplication()->getVersion());
$output->writeln('');

$this->observeProgressToShowProgressBars($output);

$pipeLine = $this->pipeline;
$pipeLine($input->getOptions());

if ($output->getVerbosity() === OutputInterface::VERBOSITY_DEBUG) {
file_put_contents('ast.dump', serialize($this->projectDescriptorBuilder->getProjectDescriptor()));
}

$output->writeln('');
$output->writeln('All done!');

return 0;
}

private function observeProgressToShowProgressBars(OutputInterface $output): void
{
if ($output->getVerbosity() !== OutputInterface::VERBOSITY_NORMAL) {
return;
}

Dispatcher::getInstance()->addListener(
'parser.pre',
function (PreParsingEvent $event) use ($output) {
$output->writeln('Parsing files');
$this->progressBar = new ProgressBar($output, $event->getFileCount());
}
);
Dispatcher::getInstance()->addListener(
'parser.file.pre',
function (PreFileEvent $event) {
$this->progressBar->advance();
}
);
Dispatcher::getInstance()->addListener(
Transformer::EVENT_PRE_TRANSFORM,
function (PreTransformEvent $event) use ($output) {
$output->writeln('');
$output->writeln('Applying transformations (can take a while)');
$this->transformerProgressBar = new ProgressBar($output, count($event->getSubject()->getTemplates()->getTransformations()));
}
);
Dispatcher::getInstance()->addListener(
Transformer::EVENT_POST_TRANSFORMATION,
function (PostTransformationEvent $event) {
$this->transformerProgressBar->advance();
}
);
}
}
Expand Up @@ -26,8 +26,8 @@ final class ProjectFactoryFactory
public static function create(iterable $fileMiddlewaresBuilder): ProjectFactory
{
$fileMiddlewares = [];
foreach ($fileMiddlewaresBuilder as $middelware) {
$fileMiddlewares[] = $middelware;
foreach ($fileMiddlewaresBuilder as $middleware) {
$fileMiddlewares[] = $middleware;
}

$strategies = [
Expand Down
39 changes: 39 additions & 0 deletions src/phpDocumentor/Parser/Event/PreParsingEvent.php
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Mike van Riel <mike.vanriel@naenius.com>
* @copyright 2010-2019 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace phpDocumentor\Parser\Event;

use phpDocumentor\Event\EventAbstract;

/**
* Event thrown before the parsing of an individual file.
*/
final class PreParsingEvent extends EventAbstract
{
/** @var integer */
protected $fileCount;

public function setFileCount(int $fileCount): self
{
$this->fileCount = $fileCount;

return $this;
}

public function getFileCount(): int
{
return $this->fileCount;
}
}
8 changes: 8 additions & 0 deletions src/phpDocumentor/Parser/Parser.php
Expand Up @@ -17,6 +17,8 @@

use phpDocumentor\Descriptor\ProjectDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
use phpDocumentor\Event\Dispatcher;
use phpDocumentor\Parser\Event\PreParsingEvent;
use phpDocumentor\Parser\Exception\FilesNotFoundException;
use phpDocumentor\Reflection\ProjectFactory;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -268,6 +270,12 @@ public function parse(ProjectDescriptorBuilder $builder, array $files)

$this->forceRebuildIfSettingsHaveModified($builder);

Dispatcher::getInstance()
->dispatch(
'parser.pre',
PreParsingEvent::createInstance($this)->setFileCount(count($files))
);

/** @var \phpDocumentor\Reflection\Php\Project $project */
$project = $this->projectFactory->create(ProjectDescriptorBuilder::DEFAULT_PROJECT_NAME, $files);
$this->logAfterParsingAllFiles();
Expand Down

0 comments on commit 53f18fc

Please sign in to comment.