1: <?php declare(strict_types = 1);
2:
3: namespace ApiGen\Renderer\Latte;
4:
5: use ApiGen\Index\Index;
6: use ApiGen\Renderer;
7: use ApiGen\Renderer\Filter;
8: use ApiGen\Renderer\Latte\Template\ConfigParameters;
9: use ApiGen\Scheduler;
10: use ApiGen\Scheduler\SchedulerFactory;
11: use Nette\Utils\FileSystem;
12: use Nette\Utils\Finder;
13: use Symfony\Component\Console\Helper\ProgressBar;
14:
15:
16: class LatteRenderer implements Renderer
17: {
18: public function __construct(
19: protected SchedulerFactory $schedulerFactory,
20: protected Filter $filter,
21: protected string $title,
22: protected string $version,
23: protected string $outputDir,
24: ) {
25: }
26:
27:
28: public function render(ProgressBar $progressBar, Index $index): void
29: {
30: $this->prepareOutputDir();
31: $scheduler = $this->createScheduler($index);
32:
33: foreach ($this->getRenderTasks($index) as $task) {
34: $scheduler->schedule($task);
35: $progressBar->setMaxSteps($progressBar->getMaxSteps() + 1);
36: }
37:
38: foreach ($scheduler->process() as $path) {
39: $progressBar->setMessage($path);
40: $progressBar->advance();
41: }
42: }
43:
44:
45: protected function prepareOutputDir(): void
46: {
47: FileSystem::delete($this->outputDir);
48: FileSystem::createDir($this->outputDir);
49: }
50:
51:
52: /**
53: * @return Scheduler<LatteRenderTask, string>
54: */
55: protected function createScheduler(Index $index): Scheduler
56: {
57: $context = new LatteRenderTaskContext($index, new ConfigParameters($this->title, $this->version));
58: return $this->schedulerFactory->create(LatteRenderTaskHandlerFactory::class, $context);
59: }
60:
61:
62: /**
63: * @return iterable<LatteRenderTask>
64: */
65: protected function getRenderTasks(Index $index): iterable
66: {
67: yield from $this->getAssetsCopyTasks();
68: yield from $this->getUnitRenderTasks();
69: yield from $this->getNamespaceRenderTasks($index);
70: yield from $this->getClassLikeRenderTasks($index);
71: yield from $this->getFunctionRenderTasks($index);
72: yield from $this->getSourceRenderTasks($index);
73: }
74:
75:
76: /**
77: * @return iterable<LatteRenderTask>
78: */
79: protected function getAssetsCopyTasks(): iterable
80: {
81: foreach (Finder::findFiles(__DIR__ . '/Template/assets/*') as $asset) {
82: yield new LatteRenderTask(LatteRenderTaskType::Asset, $asset->getPathname());
83: }
84: }
85:
86:
87: /**
88: * @return iterable<LatteRenderTask>
89: */
90: protected function getUnitRenderTasks(): iterable
91: {
92: yield new LatteRenderTask(LatteRenderTaskType::ElementsJs);
93: yield new LatteRenderTask(LatteRenderTaskType::Index);
94:
95: if ($this->filter->filterTreePage()) {
96: yield new LatteRenderTask(LatteRenderTaskType::Tree);
97: }
98:
99: if ($this->filter->filterSitemapPage()) {
100: yield new LatteRenderTask(LatteRenderTaskType::Sitemap);
101: }
102: }
103:
104:
105: /**
106: * @return iterable<LatteRenderTask>
107: */
108: protected function getNamespaceRenderTasks(Index $index): iterable
109: {
110: foreach ($index->namespace as $namespaceKey => $namespace) {
111: if ($this->filter->filterNamespacePage($namespace)) {
112: yield new LatteRenderTask(LatteRenderTaskType::Namespace, $namespaceKey);
113: }
114: }
115: }
116:
117:
118: /**
119: * @return iterable<LatteRenderTask>
120: */
121: protected function getClassLikeRenderTasks(Index $index): iterable
122: {
123: foreach ($index->classLike as $classLikeKey => $classLike) {
124: if ($this->filter->filterClassLikePage($classLike)) {
125: yield new LatteRenderTask(LatteRenderTaskType::ClassLike, $classLikeKey);
126: }
127: }
128: }
129:
130:
131: /**
132: * @return iterable<LatteRenderTask>
133: */
134: protected function getFunctionRenderTasks(Index $index): iterable
135: {
136: foreach ($index->function as $functionKey => $function) {
137: if ($this->filter->filterFunctionPage($function)) {
138: yield new LatteRenderTask(LatteRenderTaskType::Function, $functionKey);
139: }
140: }
141: }
142:
143:
144: /**
145: * @return iterable<LatteRenderTask>
146: */
147: protected function getSourceRenderTasks(Index $index): iterable
148: {
149: foreach ($index->files as $fileKey => $file) {
150: if ($this->filter->filterSourcePage($file)) {
151: yield new LatteRenderTask(LatteRenderTaskType::Source, $fileKey);
152: }
153: }
154: }
155: }
156: