Skip to content

Commit 6abf1a2

Browse files
committed
Making JsonGenerator not fail with missing metas
Will happen when there are broken references
1 parent a241073 commit 6abf1a2

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/Command/BuildDocsCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ private function generateJson(Metas $metas)
129129
$this->progressBar = new ProgressBar($this->output, $this->finder->count());
130130

131131
$jsonGenerator = new JsonGenerator($metas, $this->buildContext);
132+
$jsonGenerator->setOutput($this->io);
132133
$jsonGenerator->generateJson($this->progressBar);
133134
}
134135

src/Generator/JsonGenerator.php

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@
66
use Doctrine\RST\Meta\MetaEntry;
77
use Doctrine\RST\Meta\Metas;
88
use Symfony\Component\Console\Helper\ProgressBar;
9+
use Symfony\Component\Console\Style\SymfonyStyle;
910
use Symfony\Component\DomCrawler\Crawler;
1011
use Symfony\Component\Filesystem\Filesystem;
1112
use Symfony\Component\Finder\Finder;
1213
use SymfonyDocsBuilder\BuildContext;
1314

14-
/**
15-
* Class JsonGenerator
16-
*/
1715
class JsonGenerator
1816
{
1917
private $metas;
2018

2119
private $buildContext;
2220

21+
/** @var SymfonyStyle|null */
22+
private $output;
23+
2324
public function __construct(Metas $metas, BuildContext $buildContext)
2425
{
2526
$this->metas = $metas;
@@ -60,6 +61,11 @@ public function generateJson(ProgressBar $progressBar)
6061
$progressBar->finish();
6162
}
6263

64+
public function setOutput(SymfonyStyle $output)
65+
{
66+
$this->output = $output;
67+
}
68+
6369
private function generateToc(MetaEntry $metaEntry, ?array $titles): array
6470
{
6571
if (null === $titles) {
@@ -81,14 +87,21 @@ private function generateToc(MetaEntry $metaEntry, ?array $titles): array
8187

8288
private function guessNext(string $parserFilename): ?array
8389
{
84-
$meta = $this->getMetaEntry($parserFilename);
90+
$meta = $this->getMetaEntry($parserFilename, true);
91+
8592
$parentFile = $meta->getParent();
8693

8794
// if current file is an index, next is the first chapter
8895
if ('index' === $parentFile && \count($tocs = $meta->getTocs()) === 1 && \count($tocs[0]) > 0) {
96+
$firstChapterMeta = $this->getMetaEntry($tocs[0][0]);
97+
98+
if (null === $firstChapterMeta) {
99+
return null;
100+
}
101+
89102
return [
90-
'title' => $this->getMetaEntry($tocs[0][0])->getTitle(),
91-
'link' => $this->getMetaEntry($tocs[0][0])->getUrl(),
103+
'title' => $firstChapterMeta->getTitle(),
104+
'link' => $firstChapterMeta->getUrl(),
92105
];
93106
}
94107

@@ -100,15 +113,21 @@ private function guessNext(string $parserFilename): ?array
100113

101114
$nextFileName = $toc[$indexCurrentFile + 1];
102115

116+
$nextMeta = $this->getMetaEntry($nextFileName);
117+
118+
if (null === $nextMeta) {
119+
return null;
120+
}
121+
103122
return [
104-
'title' => $this->getMetaEntry($nextFileName)->getTitle(),
105-
'link' => $this->getMetaEntry($nextFileName)->getUrl(),
123+
'title' => $nextMeta->getTitle(),
124+
'link' => $nextMeta->getUrl(),
106125
];
107126
}
108127

109128
private function guessPrev(string $parserFilename): ?array
110129
{
111-
$meta = $this->getMetaEntry($parserFilename);
130+
$meta = $this->getMetaEntry($parserFilename, true);
112131
$parentFile = $meta->getParent();
113132

114133
// no prev if parent is an index
@@ -120,9 +139,15 @@ private function guessPrev(string $parserFilename): ?array
120139

121140
// if current file is the first one of the chapter, prev is the direct parent
122141
if (0 === $indexCurrentFile) {
142+
$parentMeta = $this->getMetaEntry($parentFile);
143+
144+
if (null === $parentMeta) {
145+
return null;
146+
}
147+
123148
return [
124-
'title' => $this->getMetaEntry($parentFile)->getTitle(),
125-
'link' => $this->getMetaEntry($parentFile)->getUrl(),
149+
'title' => $parentMeta->getTitle(),
150+
'link' => $parentMeta->getUrl(),
126151
];
127152
}
128153

@@ -132,15 +157,21 @@ private function guessPrev(string $parserFilename): ?array
132157

133158
$prevFileName = $toc[$indexCurrentFile - 1];
134159

160+
$prevMeta = $this->getMetaEntry($prevFileName);
161+
162+
if (null === $prevMeta) {
163+
return null;
164+
}
165+
135166
return [
136-
'title' => $this->getMetaEntry($prevFileName)->getTitle(),
137-
'link' => $this->getMetaEntry($prevFileName)->getUrl(),
167+
'title' => $prevMeta->getTitle(),
168+
'link' => $prevMeta->getUrl(),
138169
];
139170
}
140171

141172
private function getNextPrevInformation(string $parserFilename): ?array
142173
{
143-
$meta = $this->getMetaEntry($parserFilename);
174+
$meta = $this->getMetaEntry($parserFilename, true);
144175
$parentFile = $meta->getParent();
145176

146177
if (!$parentFile) {
@@ -149,7 +180,7 @@ private function getNextPrevInformation(string $parserFilename): ?array
149180

150181
$metaParent = $this->getMetaEntry($parentFile);
151182

152-
if (!$metaParent->getTocs() || \count($metaParent->getTocs()) !== 1) {
183+
if (null === $metaParent || !$metaParent->getTocs() || \count($metaParent->getTocs()) !== 1) {
153184
return [null, null];
154185
}
155186

@@ -164,12 +195,21 @@ private function getNextPrevInformation(string $parserFilename): ?array
164195
return [$toc, $indexCurrentFile];
165196
}
166197

167-
private function getMetaEntry(string $parserFilename): MetaEntry
198+
private function getMetaEntry(string $parserFilename, bool $throwOnMissing = false): ?MetaEntry
168199
{
169200
$metaEntry = $this->metas->get($parserFilename);
170201

202+
// this is possible if there are invalid references
171203
if (null === $metaEntry) {
172-
throw new \LogicException(sprintf('Could not find MetaEntry for file "%s"', $parserFilename));
204+
$message = sprintf('Could not find MetaEntry for file "%s"', $parserFilename);
205+
206+
if ($throwOnMissing) {
207+
throw new \Exception($message);
208+
}
209+
210+
if ($this->output) {
211+
$this->output->note($message);
212+
}
173213
}
174214

175215
return $metaEntry;

0 commit comments

Comments
 (0)