Skip to content

Commit 0203250

Browse files
committed
Refactoring to use Metas instead of the environment
1 parent 1a746b6 commit 0203250

File tree

6 files changed

+112
-151
lines changed

6 files changed

+112
-151
lines changed

src/Command/BuildDocsCommand.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace SymfonyDocsBuilder\Command;
44

55
use Doctrine\RST\Event\PostBuildRenderEvent;
6+
use Doctrine\RST\Meta\CachedMetasLoader;
7+
use Doctrine\RST\Meta\Metas;
68
use Symfony\Component\Console\Command\Command;
79
use Symfony\Component\Console\Helper\ProgressBar;
810
use Symfony\Component\Console\Input\InputArgument;
@@ -80,10 +82,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
8082

8183
$this->missingFilesChecker->checkMissingFiles($this->io);
8284

85+
$metas = $this->getMetas();
8386
if (!$this->buildContext->getParseSubPath()) {
84-
$this->generateJson();
87+
$this->generateJson($metas);
8588
} else {
86-
$this->renderDocForPDF();
89+
$this->renderDocForPDF($metas);
8790
}
8891

8992
$this->io->newLine(2);
@@ -100,19 +103,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
100103
$this->io->success($successMessage);
101104
}
102105

103-
private function generateJson()
106+
private function generateJson(Metas $metas)
104107
{
105108
$this->io->note('Start exporting doc into json files');
106109
$this->progressBar = new ProgressBar($this->output, $this->finder->count());
107110

108-
$jsonGenerator = new JsonGenerator($this->buildContext);
109-
$jsonGenerator->generateJson($this->builder->getDocuments()->getAll(), $this->progressBar);
111+
$jsonGenerator = new JsonGenerator($metas, $this->buildContext);
112+
$jsonGenerator->generateJson($this->progressBar);
110113
}
111114

112-
private function renderDocForPDF()
115+
private function renderDocForPDF(Metas $metas)
113116
{
114-
$htmlForPdfGenerator = new HtmlForPdfGenerator($this->buildContext);
115-
$htmlForPdfGenerator->generateHtmlForPdf($this->builder->getDocuments()->getAll());
117+
$htmlForPdfGenerator = new HtmlForPdfGenerator($metas, $this->buildContext);
118+
$htmlForPdfGenerator->generateHtmlForPdf();
116119
}
117120

118121
public function preBuildRender()
@@ -121,4 +124,17 @@ public function preBuildRender()
121124

122125
$this->io->note('Start rendering in HTML...');
123126
}
127+
128+
private function getMetas(): Metas
129+
{
130+
/*
131+
* TODO - get this from the Builder when it is exposed
132+
* https://github.com/doctrine/rst-parser/pull/97
133+
*/
134+
$metas = new Metas();
135+
$cachedMetasLoader = new CachedMetasLoader();
136+
$cachedMetasLoader->loadCachedMetaEntries($this->buildContext->getHtmlOutputDir(), $metas);
137+
138+
return $metas;
139+
}
124140
}

src/Generator/GeneratorTrait.php

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/Generator/HtmlForPdfGenerator.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22

33
namespace SymfonyDocsBuilder\Generator;
44

5+
use Doctrine\RST\Meta\MetaEntry;
6+
use Doctrine\RST\Meta\Metas;
57
use Symfony\Component\DomCrawler\Crawler;
68
use Symfony\Component\Filesystem\Filesystem;
79
use Symfony\Component\Finder\Finder;
810
use SymfonyDocsBuilder\BuildContext;
911

1012
class HtmlForPdfGenerator
1113
{
12-
use GeneratorTrait;
14+
private $metas;
1315

14-
public function generateHtmlForPdf(array $documents) {
15-
$this->extractEnvironmentsAndCachedMetas($documents);
16+
private $buildContext;
17+
18+
public function __construct(Metas $metas, BuildContext $buildContext)
19+
{
20+
$this->metas = $metas;
21+
$this->buildContext = $buildContext;
22+
}
23+
24+
public function generateHtmlForPdf() {
1625

1726
$finder = new Finder();
1827
$finder->in($this->buildContext->getHtmlOutputDir())
@@ -32,7 +41,7 @@ public function generateHtmlForPdf(array $documents) {
3241

3342
// extracting all files from index's TOC, in the right order
3443
$parserFilename = $this->getParserFilename($indexFile, $this->buildContext->getHtmlOutputDir());
35-
$meta = $this->getMeta($parserFilename);
44+
$meta = $this->getMetaEntry($parserFilename);
3645
$files = current($meta->getTocs());
3746
array_unshift($files, sprintf('%s/index', $this->buildContext->getParseSubPath()));
3847

@@ -41,7 +50,7 @@ public function generateHtmlForPdf(array $documents) {
4150
$htmlDir = $this->buildContext->getHtmlOutputDir();
4251
$relativeImagesPath = str_repeat('../', substr_count($this->buildContext->getParseSubPath(), '/'));
4352
foreach ($files as $file) {
44-
$meta = $this->getMeta($file);
53+
$meta = $this->getMetaEntry($file);
4554

4655
$filename = sprintf('%s/%s.html', $htmlDir, $file);
4756
if (!$fs->exists($filename)) {
@@ -139,4 +148,20 @@ function ($matches) {
139148

140149
return $content;
141150
}
151+
152+
private function getMetaEntry(string $parserFilename): MetaEntry
153+
{
154+
$metaEntry = $this->metas->get($parserFilename);
155+
156+
if (null === $metaEntry) {
157+
throw new \LogicException(sprintf('Could not find MetaEntry for file "%s"', $parserFilename));
158+
}
159+
160+
return $metaEntry;
161+
}
162+
163+
private function getParserFilename(string $filePath, string $inputDir): string
164+
{
165+
return $parserFilename = str_replace([$inputDir.'/', '.html'], ['', ''], $filePath);
166+
}
142167
}

src/Generator/JsonGenerator.php

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,43 @@
44

55
use Doctrine\RST\Environment;
66
use Doctrine\RST\Meta\MetaEntry;
7+
use Doctrine\RST\Meta\Metas;
78
use Symfony\Component\Console\Helper\ProgressBar;
89
use Symfony\Component\DomCrawler\Crawler;
910
use Symfony\Component\Filesystem\Filesystem;
1011
use Symfony\Component\Finder\Finder;
12+
use SymfonyDocsBuilder\BuildContext;
1113

1214
/**
1315
* Class JsonGenerator
1416
*/
1517
class JsonGenerator
1618
{
17-
use GeneratorTrait;
19+
private $metas;
1820

19-
public function generateJson(array $documents, ProgressBar $progressBar)
20-
{
21-
$this->extractEnvironmentsAndCachedMetas($documents);
21+
private $buildContext;
2222

23-
$finder = new Finder();
24-
$finder->in($this->buildContext->getHtmlOutputDir())
25-
->name('*.html')
26-
->files();
23+
public function __construct(Metas $metas, BuildContext $buildContext)
24+
{
25+
$this->metas = $metas;
26+
$this->buildContext = $buildContext;
27+
}
2728

29+
public function generateJson(ProgressBar $progressBar)
30+
{
2831
$fs = new Filesystem();
2932

30-
foreach ($finder as $file) {
31-
$parserFilename = $this->getParserFilename($file->getRealPath(), $this->buildContext->getHtmlOutputDir());
32-
$jsonFilename = str_replace([$this->buildContext->getHtmlOutputDir(), '.html'], [$this->buildContext->getJsonOutputDir(), '.json'], $file->getRealPath());
33-
34-
if ($this->useCacheForFile($parserFilename)) {
35-
if (!file_exists($jsonFilename)) {
36-
throw new \RuntimeException(
37-
sprintf('File %s does not exist although cache is enabled and related environment is not available', $jsonFilename)
38-
);
39-
}
40-
41-
continue;
42-
}
43-
44-
$meta = $this->getMeta($parserFilename);
33+
foreach ($this->metas->getAll() as $filename => $metaEntry) {
34+
$parserFilename = $filename;
35+
$jsonFilename = $this->buildContext->getJsonOutputDir().'/'.$filename.'.json';
4536

46-
$crawler = new Crawler($file->getContents());
37+
$crawler = new Crawler(file_get_contents($this->buildContext->getHtmlOutputDir().'/'.$filename.'.html'));
4738

4839
$data = [
4940
'body' => $crawler->filter('body')->html(),
50-
'title' => $meta->getTitle(),
41+
'title' => $metaEntry->getTitle(),
5142
'current_page_name' => $parserFilename,
52-
'toc' => $this->generateToc($meta, current($meta->getTitles())[1]),
43+
'toc' => $this->generateToc($metaEntry, current($metaEntry->getTitles())[1]),
5344
'next' => $this->guessNext($parserFilename),
5445
'prev' => $this->guessPrev($parserFilename),
5546
'rellinks' => [
@@ -90,14 +81,14 @@ private function generateToc(MetaEntry $metaEntry, ?array $titles): array
9081

9182
private function guessNext(string $parserFilename): ?array
9283
{
93-
$meta = $this->getMeta($parserFilename);
84+
$meta = $this->getMetaEntry($parserFilename);
9485
$parentFile = $meta->getParent();
9586

9687
// if current file is an index, next is the first chapter
9788
if ('index' === $parentFile && \count($tocs = $meta->getTocs()) === 1 && \count($tocs[0]) > 0) {
9889
return [
99-
'title' => $this->getMeta($tocs[0][0])->getTitle(),
100-
'link' => $this->getMeta($tocs[0][0])->getUrl(),
90+
'title' => $this->getMetaEntry($tocs[0][0])->getTitle(),
91+
'link' => $this->getMetaEntry($tocs[0][0])->getUrl(),
10192
];
10293
}
10394

@@ -110,14 +101,14 @@ private function guessNext(string $parserFilename): ?array
110101
$nextFileName = $toc[$indexCurrentFile + 1];
111102

112103
return [
113-
'title' => $this->getMeta($nextFileName)->getTitle(),
114-
'link' => $this->getMeta($nextFileName)->getUrl(),
104+
'title' => $this->getMetaEntry($nextFileName)->getTitle(),
105+
'link' => $this->getMetaEntry($nextFileName)->getUrl(),
115106
];
116107
}
117108

118109
private function guessPrev(string $parserFilename): ?array
119110
{
120-
$meta = $this->getMeta($parserFilename);
111+
$meta = $this->getMetaEntry($parserFilename);
121112
$parentFile = $meta->getParent();
122113

123114
// no prev if parent is an index
@@ -130,8 +121,8 @@ private function guessPrev(string $parserFilename): ?array
130121
// if current file is the first one of the chapter, prev is the direct parent
131122
if (0 === $indexCurrentFile) {
132123
return [
133-
'title' => $this->getMeta($parentFile)->getTitle(),
134-
'link' => $this->getMeta($parentFile)->getUrl(),
124+
'title' => $this->getMetaEntry($parentFile)->getTitle(),
125+
'link' => $this->getMetaEntry($parentFile)->getUrl(),
135126
];
136127
}
137128

@@ -142,21 +133,21 @@ private function guessPrev(string $parserFilename): ?array
142133
$prevFileName = $toc[$indexCurrentFile - 1];
143134

144135
return [
145-
'title' => $this->getMeta($prevFileName)->getTitle(),
146-
'link' => $this->getMeta($prevFileName)->getUrl(),
136+
'title' => $this->getMetaEntry($prevFileName)->getTitle(),
137+
'link' => $this->getMetaEntry($prevFileName)->getUrl(),
147138
];
148139
}
149140

150141
private function getNextPrevInformation(string $parserFilename): ?array
151142
{
152-
$meta = $this->getMeta($parserFilename);
143+
$meta = $this->getMetaEntry($parserFilename);
153144
$parentFile = $meta->getParent();
154145

155146
if (!$parentFile) {
156147
return [null, null];
157148
}
158149

159-
$metaParent = $this->getMeta($parentFile);
150+
$metaParent = $this->getMetaEntry($parentFile);
160151

161152
if (!$metaParent->getTocs() || \count($metaParent->getTocs()) !== 1) {
162153
return [null, null];
@@ -172,4 +163,15 @@ private function getNextPrevInformation(string $parserFilename): ?array
172163

173164
return [$toc, $indexCurrentFile];
174165
}
166+
167+
private function getMetaEntry(string $parserFilename): MetaEntry
168+
{
169+
$metaEntry = $this->metas->get($parserFilename);
170+
171+
if (null === $metaEntry) {
172+
throw new \LogicException(sprintf('Could not find MetaEntry for file "%s"', $parserFilename));
173+
}
174+
175+
return $metaEntry;
176+
}
175177
}

0 commit comments

Comments
 (0)