|
13 | 13 | use Monolog\Logger;
|
14 | 14 | use phpDocumentor\Guides\Cli\Logger\SpyProcessor;
|
15 | 15 | use phpDocumentor\Guides\Compiler\CompilerContext;
|
| 16 | +use phpDocumentor\Guides\Event\PostCollectFilesForParsingEvent; |
| 17 | +use phpDocumentor\Guides\Event\PostParseDocument; |
| 18 | +use phpDocumentor\Guides\Event\PostParseProcess; |
16 | 19 | 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; |
17 | 24 | use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
|
18 | 25 | use phpDocumentor\Guides\Handlers\ParseDirectoryCommand;
|
19 | 26 | use phpDocumentor\Guides\Handlers\ParseFileCommand;
|
|
39 | 46 | use function implode;
|
40 | 47 | use function is_countable;
|
41 | 48 | use function is_dir;
|
| 49 | +use function microtime; |
42 | 50 | use function pathinfo;
|
43 | 51 | use function realpath;
|
44 | 52 | use function sprintf;
|
@@ -117,6 +125,80 @@ public function __construct(
|
117 | 125 | );
|
118 | 126 | }
|
119 | 127 |
|
| 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 | + |
120 | 202 | private function getSettingsOverridenWithInput(InputInterface $input): ProjectSettings
|
121 | 203 | {
|
122 | 204 | $settings = $this->settingsManager->getProjectSettings();
|
@@ -200,6 +282,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
200 | 282 |
|
201 | 283 | $documents = [];
|
202 | 284 |
|
| 285 | + |
| 286 | + if ($output instanceof ConsoleOutputInterface && $settings->isShowProgressBar()) { |
| 287 | + $this->registerProgressBar($output); |
| 288 | + } |
| 289 | + |
203 | 290 | if ($settings->getInputFile() === '') {
|
204 | 291 | $documents = $this->commandBus->handle(
|
205 | 292 | new ParseDirectoryCommand(
|
@@ -231,16 +318,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
231 | 318 |
|
232 | 319 | $outputFormats = $settings->getOutputFormats();
|
233 | 320 |
|
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 |
| - |
244 | 321 | foreach ($outputFormats as $format) {
|
245 | 322 | $this->commandBus->handle(
|
246 | 323 | new RenderCommand(
|
|
0 commit comments