From 53f18fccd740dc7ec97605adf8973ee0dd918dd0 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Fri, 22 Feb 2019 20:57:36 +0100 Subject: [PATCH] Make progress bars the default output In this change I have reintroduced the progress bar view and made it the default. --- config/packages/dev/monolog.yaml | 7 +- .../Console/Command/Project/RunCommand.php | 79 +++++++++++++++++-- .../Parser/ProjectFactoryFactory.php | 4 +- .../Parser/Event/PreParsingEvent.php | 39 +++++++++ src/phpDocumentor/Parser/Parser.php | 8 ++ 5 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 src/phpDocumentor/Parser/Event/PreParsingEvent.php diff --git a/config/packages/dev/monolog.yaml b/config/packages/dev/monolog.yaml index 673a948c58..dea0ebf96c 100644 --- a/config/packages/dev/monolog.yaml +++ b/config/packages/dev/monolog.yaml @@ -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 diff --git a/src/phpDocumentor/Application/Console/Command/Project/RunCommand.php b/src/phpDocumentor/Application/Console/Command/Project/RunCommand.php index 32e58e03dd..6bb4c5375b 100644 --- a/src/phpDocumentor/Application/Console/Command/Project/RunCommand.php +++ b/src/phpDocumentor/Application/Console/Command/Project/RunCommand.php @@ -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; @@ -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; } /** @@ -246,6 +266,11 @@ 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()); @@ -253,6 +278,44 @@ protected function execute(InputInterface $input, OutputInterface $output): int 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(); + } + ); + } } diff --git a/src/phpDocumentor/Application/Parser/ProjectFactoryFactory.php b/src/phpDocumentor/Application/Parser/ProjectFactoryFactory.php index 53565aafec..b01414c46e 100644 --- a/src/phpDocumentor/Application/Parser/ProjectFactoryFactory.php +++ b/src/phpDocumentor/Application/Parser/ProjectFactoryFactory.php @@ -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 = [ diff --git a/src/phpDocumentor/Parser/Event/PreParsingEvent.php b/src/phpDocumentor/Parser/Event/PreParsingEvent.php new file mode 100644 index 0000000000..4e725d8b8d --- /dev/null +++ b/src/phpDocumentor/Parser/Event/PreParsingEvent.php @@ -0,0 +1,39 @@ + + * @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; + } +} diff --git a/src/phpDocumentor/Parser/Parser.php b/src/phpDocumentor/Parser/Parser.php index 3b339921cf..048c8c876c 100644 --- a/src/phpDocumentor/Parser/Parser.php +++ b/src/phpDocumentor/Parser/Parser.php @@ -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; @@ -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();