|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiGen\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Console\Helper\ProgressBarInterface; |
|
6
|
|
|
use ApiGen\Contracts\Generator\GeneratorQueueInterface; |
|
7
|
|
|
use ApiGen\Contracts\Generator\StepCounterInterface; |
|
8
|
|
|
use ApiGen\Contracts\Generator\TemplateGenerators\ConditionalTemplateGeneratorInterface; |
|
9
|
|
|
use ApiGen\Contracts\Generator\TemplateGenerators\TemplateGeneratorInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class GeneratorQueue implements GeneratorQueueInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ProgressBarInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $progressBar; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var TemplateGeneratorInterface[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $queue = []; |
|
22
|
|
|
|
|
23
|
8 |
|
public function __construct(ProgressBarInterface $progressBar) |
|
24
|
|
|
{ |
|
25
|
8 |
|
$this->progressBar = $progressBar; |
|
26
|
8 |
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
public function run(): void |
|
29
|
|
|
{ |
|
30
|
2 |
|
$this->progressBar->init($this->getStepCount()); |
|
31
|
|
|
|
|
32
|
2 |
|
foreach ($this->getAllowedQueue() as $templateGenerator) { |
|
33
|
2 |
|
$templateGenerator->generate(); |
|
34
|
|
|
} |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
8 |
|
public function addToQueue(TemplateGeneratorInterface $templateGenerator): void |
|
38
|
|
|
{ |
|
39
|
8 |
|
$this->queue[] = $templateGenerator; |
|
40
|
8 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return TemplateGeneratorInterface[] |
|
44
|
|
|
*/ |
|
45
|
|
|
private function getAllowedQueue(): array |
|
46
|
|
|
{ |
|
47
|
4 |
|
return array_filter($this->queue, function (TemplateGeneratorInterface $generator) { |
|
48
|
4 |
|
if ($generator instanceof ConditionalTemplateGeneratorInterface) { |
|
49
|
1 |
|
return $generator->isAllowed(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
return true; |
|
53
|
4 |
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
private function getStepCount(): int |
|
57
|
|
|
{ |
|
58
|
3 |
|
$steps = 0; |
|
59
|
3 |
|
foreach ($this->getAllowedQueue() as $templateGenerator) { |
|
60
|
3 |
|
if ($templateGenerator instanceof StepCounterInterface) { |
|
61
|
2 |
|
$steps += $templateGenerator->getStepCount(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
return $steps; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|