Skip to content

Commit d8493bf

Browse files
committed
Move progress bar configuration to separate class
By moving the progress bar configuration into a separate class we can make the run command more lean. This will allow some improvements in the future.
1 parent 8e7c01f commit d8493bf

File tree

3 files changed

+129
-81
lines changed

3 files changed

+129
-81
lines changed

packages/guides-cli/resources/config/services.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Monolog\Logger;
66
use phpDocumentor\Guides\Cli\Application;
7+
use phpDocumentor\Guides\Cli\Command\ProgressBarSubscriber;
78
use phpDocumentor\Guides\Cli\Command\Run;
89
use phpDocumentor\Guides\Cli\Command\WorkingDirectorySwitcher;
910
use Psr\Clock\ClockInterface;
@@ -41,5 +42,7 @@
4142
->public()
4243

4344
->set(WorkingDirectorySwitcher::class)
44-
->tag('event_listener', ['event' => ConsoleEvents::COMMAND, 'method' => '__invoke']);
45+
->tag('event_listener', ['event' => ConsoleEvents::COMMAND, 'method' => '__invoke'])
46+
47+
->set(ProgressBarSubscriber::class);
4548
};
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\Cli\Command;
15+
16+
use phpDocumentor\Guides\Event\PostCollectFilesForParsingEvent;
17+
use phpDocumentor\Guides\Event\PostParseDocument;
18+
use phpDocumentor\Guides\Event\PostParseProcess;
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;
24+
use Symfony\Component\Console\Helper\ProgressBar;
25+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
26+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27+
28+
use function count;
29+
use function microtime;
30+
use function sprintf;
31+
32+
/** @internal */
33+
final class ProgressBarSubscriber
34+
{
35+
public function subscribe(ConsoleOutputInterface $output, EventDispatcherInterface $dispatcher): void
36+
{
37+
$this->registerParserProgressBar($output, $dispatcher);
38+
$this->registerRenderProgressBar($output, $dispatcher);
39+
}
40+
41+
private function registerParserProgressBar(ConsoleOutputInterface $output, EventDispatcherInterface $dispatcher): void
42+
{
43+
$parsingProgressBar = new ProgressBar($output->section());
44+
$parsingProgressBar->setFormat('Parsing: %current%/%max% [%bar%] %percent:3s%% %message%');
45+
$parsingStartTime = microtime(true);
46+
$dispatcher->addListener(
47+
PostCollectFilesForParsingEvent::class,
48+
static function (PostCollectFilesForParsingEvent $event) use ($parsingProgressBar, &$parsingStartTime): void {
49+
// Each File needs to be first parsed then rendered
50+
$parsingStartTime = microtime(true);
51+
$parsingProgressBar->setMaxSteps(count($event->getFiles()));
52+
},
53+
);
54+
$dispatcher->addListener(
55+
PreParseDocument::class,
56+
static function (PreParseDocument $event) use ($parsingProgressBar): void {
57+
$parsingProgressBar->setMessage('Parsing file: ' . $event->getFileName());
58+
$parsingProgressBar->display();
59+
},
60+
);
61+
$dispatcher->addListener(
62+
PostParseDocument::class,
63+
static function (PostParseDocument $event) use ($parsingProgressBar): void {
64+
$parsingProgressBar->advance();
65+
},
66+
);
67+
$dispatcher->addListener(
68+
PostParseProcess::class,
69+
static function (PostParseProcess $event) use ($parsingProgressBar, $parsingStartTime): void {
70+
$parsingTimeElapsed = microtime(true) - $parsingStartTime;
71+
$parsingProgressBar->setMessage(sprintf(
72+
'Parsed %s files in %.2f seconds',
73+
$parsingProgressBar->getMaxSteps(),
74+
$parsingTimeElapsed,
75+
));
76+
$parsingProgressBar->finish();
77+
},
78+
);
79+
}
80+
81+
private function registerRenderProgressBar(ConsoleOutputInterface $output, EventDispatcherInterface $dispatcher): void
82+
{
83+
$dispatcher->addListener(
84+
PreRenderProcess::class,
85+
static function (PreRenderProcess $event) use ($dispatcher, $output): void {
86+
$renderingProgressBar = new ProgressBar($output->section(), count($event->getCommand()->getDocumentArray()));
87+
$renderingProgressBar->setFormat('Rendering: %current%/%max% [%bar%] %percent:3s%% Output format ' . $event->getCommand()->getOutputFormat() . ': %message%');
88+
$renderingStartTime = microtime(true);
89+
$dispatcher->addListener(
90+
PreRenderDocument::class,
91+
static function (PreRenderDocument $event) use ($renderingProgressBar): void {
92+
$renderingProgressBar->setMessage('Rendering: ' . $event->getCommand()->getFileDestination());
93+
$renderingProgressBar->display();
94+
},
95+
);
96+
$dispatcher->addListener(
97+
PostRenderDocument::class,
98+
static function (PostRenderDocument $event) use ($renderingProgressBar): void {
99+
$renderingProgressBar->advance();
100+
},
101+
);
102+
$dispatcher->addListener(
103+
PostRenderProcess::class,
104+
static function (PostRenderProcess $event) use ($renderingProgressBar, $renderingStartTime): void {
105+
$renderingElapsedTime = microtime(true) - $renderingStartTime;
106+
$renderingProgressBar->setMessage(sprintf(
107+
'Rendered %s documents in %.2f seconds',
108+
$renderingProgressBar->getMaxSteps(),
109+
$renderingElapsedTime,
110+
));
111+
$renderingProgressBar->finish();
112+
},
113+
);
114+
},
115+
);
116+
}
117+
}

packages/guides-cli/src/Command/Run.php

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Guides\Cli\Command;
1515

16+
use Doctrine\Deprecations\Deprecation;
1617
use Flyfinder\Path;
1718
use Flyfinder\Specification\InPath;
1819
use Flyfinder\Specification\NotSpecification;
@@ -25,15 +26,7 @@
2526
use phpDocumentor\FileSystem\FlySystemAdapter;
2627
use phpDocumentor\Guides\Cli\Logger\SpyProcessor;
2728
use phpDocumentor\Guides\Compiler\CompilerContext;
28-
use phpDocumentor\Guides\Event\PostCollectFilesForParsingEvent;
29-
use phpDocumentor\Guides\Event\PostParseDocument;
30-
use phpDocumentor\Guides\Event\PostParseProcess;
3129
use phpDocumentor\Guides\Event\PostProjectNodeCreated;
32-
use phpDocumentor\Guides\Event\PostRenderDocument;
33-
use phpDocumentor\Guides\Event\PostRenderProcess;
34-
use phpDocumentor\Guides\Event\PreParseDocument;
35-
use phpDocumentor\Guides\Event\PreRenderDocument;
36-
use phpDocumentor\Guides\Event\PreRenderProcess;
3730
use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
3831
use phpDocumentor\Guides\Handlers\ParseDirectoryCommand;
3932
use phpDocumentor\Guides\Handlers\ParseFileCommand;
@@ -46,7 +39,6 @@
4639
use Psr\Log\LogLevel;
4740
use RuntimeException;
4841
use Symfony\Component\Console\Command\Command;
49-
use Symfony\Component\Console\Helper\ProgressBar;
5042
use Symfony\Component\Console\Input\InputArgument;
5143
use Symfony\Component\Console\Input\InputInterface;
5244
use Symfony\Component\Console\Input\InputOption;
@@ -63,7 +55,6 @@
6355
use function implode;
6456
use function is_countable;
6557
use function is_dir;
66-
use function microtime;
6758
use function pathinfo;
6859
use function sprintf;
6960
use function strtoupper;
@@ -77,6 +68,7 @@ public function __construct(
7768
private readonly SettingsManager $settingsManager,
7869
private readonly ClockInterface $clock,
7970
private readonly EventDispatcher $eventDispatcher,
71+
private readonly ProgressBarSubscriber $progressBarSubscriber,
8072
) {
8173
parent::__construct('run');
8274

@@ -156,76 +148,12 @@ public function __construct(
156148

157149
public function registerProgressBar(ConsoleOutputInterface $output): void
158150
{
159-
$parsingProgressBar = new ProgressBar($output->section());
160-
$parsingProgressBar->setFormat('Parsing: %current%/%max% [%bar%] %percent:3s%% %message%');
161-
$parsingStartTime = microtime(true);
162-
$this->eventDispatcher->addListener(
163-
PostCollectFilesForParsingEvent::class,
164-
static function (PostCollectFilesForParsingEvent $event) use ($parsingProgressBar, &$parsingStartTime): void {
165-
// Each File needs to be first parsed then rendered
166-
$parsingStartTime = microtime(true);
167-
$parsingProgressBar->setMaxSteps(count($event->getFiles()));
168-
},
169-
);
170-
$this->eventDispatcher->addListener(
171-
PreParseDocument::class,
172-
static function (PreParseDocument $event) use ($parsingProgressBar): void {
173-
$parsingProgressBar->setMessage('Parsing file: ' . $event->getFileName());
174-
$parsingProgressBar->display();
175-
},
176-
);
177-
$this->eventDispatcher->addListener(
178-
PostParseDocument::class,
179-
static function (PostParseDocument $event) use ($parsingProgressBar): void {
180-
$parsingProgressBar->advance();
181-
},
182-
);
183-
$this->eventDispatcher->addListener(
184-
PostParseProcess::class,
185-
static function (PostParseProcess $event) use ($parsingProgressBar, $parsingStartTime): void {
186-
$parsingTimeElapsed = microtime(true) - $parsingStartTime;
187-
$parsingProgressBar->setMessage(sprintf(
188-
'Parsed %s files in %.2f seconds',
189-
$parsingProgressBar->getMaxSteps(),
190-
$parsingTimeElapsed,
191-
));
192-
$parsingProgressBar->finish();
193-
},
194-
);
195-
$that = $this;
196-
$this->eventDispatcher->addListener(
197-
PreRenderProcess::class,
198-
static function (PreRenderProcess $event) use ($that, $output): void {
199-
$renderingProgressBar = new ProgressBar($output->section(), count($event->getCommand()->getDocumentArray()));
200-
$renderingProgressBar->setFormat('Rendering: %current%/%max% [%bar%] %percent:3s%% Output format ' . $event->getCommand()->getOutputFormat() . ': %message%');
201-
$renderingStartTime = microtime(true);
202-
$that->eventDispatcher->addListener(
203-
PreRenderDocument::class,
204-
static function (PreRenderDocument $event) use ($renderingProgressBar): void {
205-
$renderingProgressBar->setMessage('Rendering: ' . $event->getCommand()->getFileDestination());
206-
$renderingProgressBar->display();
207-
},
208-
);
209-
$that->eventDispatcher->addListener(
210-
PostRenderDocument::class,
211-
static function (PostRenderDocument $event) use ($renderingProgressBar): void {
212-
$renderingProgressBar->advance();
213-
},
214-
);
215-
$that->eventDispatcher->addListener(
216-
PostRenderProcess::class,
217-
static function (PostRenderProcess $event) use ($renderingProgressBar, $renderingStartTime): void {
218-
$renderingElapsedTime = microtime(true) - $renderingStartTime;
219-
$renderingProgressBar->setMessage(sprintf(
220-
'Rendered %s documents in %.2f seconds',
221-
$renderingProgressBar->getMaxSteps(),
222-
$renderingElapsedTime,
223-
));
224-
$renderingProgressBar->finish();
225-
},
226-
);
227-
},
151+
Deprecation::trigger(
152+
'phpdocumentor/guides-cli',
153+
'https://github.com/phpdocumentor/guides/issues/33',
154+
'Progressbar will be registered via settings',
228155
);
156+
$this->progressBarSubscriber->subscribe($output, $this->eventDispatcher);
229157
}
230158

231159
private function getSettingsOverriddenWithInput(InputInterface $input): ProjectSettings
@@ -323,7 +251,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
323251

324252

325253
if ($output instanceof ConsoleOutputInterface && $settings->isShowProgressBar()) {
326-
$this->registerProgressBar($output);
254+
$this->progressBarSubscriber->subscribe($output, $this->eventDispatcher);
327255
}
328256

329257
if ($settings->getInputFile() === '') {

0 commit comments

Comments
 (0)