Skip to content

Commit 54b7451

Browse files
committed
[TASK] Improve progress bar
* Display a progress bar during parsing * Display one progress bar per renderer * Introduce an event once the files have been collected for parsing This gives me progress bars like this after rendering: Parsing: 16/16 [============================] 100% Parsed 16 files in 0.98 seconds Rendering: 16/16 [============================] 100% Output format html: Rendered 16 documents in 0.71 seconds Rendering: 16/16 [============================] 100% Output format interlink: Rendered 16 documents in 0.00 seconds During rendering and during parsing the currently handled file is displayed.
1 parent 42a0633 commit 54b7451

File tree

1 file changed

+87
-10
lines changed

1 file changed

+87
-10
lines changed

src/Command/Run.php

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
use Monolog\Logger;
1414
use phpDocumentor\Guides\Cli\Logger\SpyProcessor;
1515
use phpDocumentor\Guides\Compiler\CompilerContext;
16+
use phpDocumentor\Guides\Event\PostCollectFilesForParsingEvent;
17+
use phpDocumentor\Guides\Event\PostParseDocument;
18+
use phpDocumentor\Guides\Event\PostParseProcess;
1619
use phpDocumentor\Guides\Event\PostRenderDocument;
20+
use phpDocumentor\Guides\Event\PostRenderProcess;
21+
use phpDocumentor\Guides\Event\PreParseDocument;
22+
use phpDocumentor\Guides\Event\PreRenderDocument;
23+
use phpDocumentor\Guides\Event\PreRenderProcess;
1724
use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
1825
use phpDocumentor\Guides\Handlers\ParseDirectoryCommand;
1926
use phpDocumentor\Guides\Handlers\ParseFileCommand;
@@ -39,6 +46,7 @@
3946
use function implode;
4047
use function is_countable;
4148
use function is_dir;
49+
use function microtime;
4250
use function pathinfo;
4351
use function realpath;
4452
use function sprintf;
@@ -117,6 +125,80 @@ public function __construct(
117125
);
118126
}
119127

128+
public function registerProgressBar(ConsoleOutputInterface $output): void
129+
{
130+
$parsingProgressBar = new ProgressBar($output->section());
131+
$parsingProgressBar->setFormat('Parsing: %current%/%max% [%bar%] %percent:3s%% %message%');
132+
$parsingStartTime = microtime(true);
133+
$this->eventDispatcher->addListener(
134+
PostCollectFilesForParsingEvent::class,
135+
static function (PostCollectFilesForParsingEvent $event) use ($parsingProgressBar, &$parsingStartTime): void {
136+
// Each File needs to be first parsed then rendered
137+
$parsingStartTime = microtime(true);
138+
$parsingProgressBar->setMaxSteps(count($event->getFiles()));
139+
},
140+
);
141+
$this->eventDispatcher->addListener(
142+
PreParseDocument::class,
143+
static function (PreParseDocument $event) use ($parsingProgressBar): void {
144+
$parsingProgressBar->setMessage('Parsing file: ' . $event->getFileName());
145+
$parsingProgressBar->display();
146+
},
147+
);
148+
$this->eventDispatcher->addListener(
149+
PostParseDocument::class,
150+
static function (PostParseDocument $event) use ($parsingProgressBar): void {
151+
$parsingProgressBar->advance();
152+
},
153+
);
154+
$this->eventDispatcher->addListener(
155+
PostParseProcess::class,
156+
static function (PostParseProcess $event) use ($parsingProgressBar, $parsingStartTime): void {
157+
$parsingTimeElapsed = microtime(true) - $parsingStartTime;
158+
$parsingProgressBar->setMessage(sprintf(
159+
'Parsed %s files in %.2f seconds',
160+
$parsingProgressBar->getMaxSteps(),
161+
$parsingTimeElapsed,
162+
));
163+
$parsingProgressBar->finish();
164+
},
165+
);
166+
$that = $this;
167+
$this->eventDispatcher->addListener(
168+
PreRenderProcess::class,
169+
static function (PreRenderProcess $event) use ($that, $output): void {
170+
$renderingProgressBar = new ProgressBar($output->section(), count($event->getCommand()->getDocumentArray()));
171+
$renderingProgressBar->setFormat('Rendering: %current%/%max% [%bar%] %percent:3s%% Output format ' . $event->getCommand()->getOutputFormat() . ': %message%');
172+
$renderingStartTime = microtime(true);
173+
$that->eventDispatcher->addListener(
174+
PreRenderDocument::class,
175+
static function (PreRenderDocument $event) use ($renderingProgressBar): void {
176+
$renderingProgressBar->setMessage('Rendering: ' . $event->getCommand()->getFileDestination());
177+
$renderingProgressBar->display();
178+
},
179+
);
180+
$that->eventDispatcher->addListener(
181+
PostRenderDocument::class,
182+
static function (PostRenderDocument $event) use ($renderingProgressBar): void {
183+
$renderingProgressBar->advance();
184+
},
185+
);
186+
$that->eventDispatcher->addListener(
187+
PostRenderProcess::class,
188+
static function (PostRenderProcess $event) use ($renderingProgressBar, $renderingStartTime): void {
189+
$renderingElapsedTime = microtime(true) - $renderingStartTime;
190+
$renderingProgressBar->setMessage(sprintf(
191+
'Rendered %s documents in %.2f seconds',
192+
$renderingProgressBar->getMaxSteps(),
193+
$renderingElapsedTime,
194+
));
195+
$renderingProgressBar->finish();
196+
},
197+
);
198+
},
199+
);
200+
}
201+
120202
private function getSettingsOverridenWithInput(InputInterface $input): ProjectSettings
121203
{
122204
$settings = $this->settingsManager->getProjectSettings();
@@ -200,6 +282,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
200282

201283
$documents = [];
202284

285+
286+
if ($output instanceof ConsoleOutputInterface && $settings->isShowProgressBar()) {
287+
$this->registerProgressBar($output);
288+
}
289+
203290
if ($settings->getInputFile() === '') {
204291
$documents = $this->commandBus->handle(
205292
new ParseDirectoryCommand(
@@ -231,16 +318,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
231318

232319
$outputFormats = $settings->getOutputFormats();
233320

234-
if ($output instanceof ConsoleOutputInterface && $settings->isShowProgressBar()) {
235-
$progressBar = new ProgressBar($output->section(), count($documents));
236-
$this->eventDispatcher->addListener(
237-
PostRenderDocument::class,
238-
static function (PostRenderDocument $event) use ($progressBar): void {
239-
$progressBar->advance();
240-
},
241-
);
242-
}
243-
244321
foreach ($outputFormats as $format) {
245322
$this->commandBus->handle(
246323
new RenderCommand(

0 commit comments

Comments
 (0)