Skip to content

Commit 37735d6

Browse files
authored
Merge pull request #804 from phpDocumentor/bugfix/menu-warning
[BUGFIX] Generate Warning when menu entry was not found
2 parents d160f4c + 959c35f commit 37735d6

File tree

10 files changed

+72
-4
lines changed

10 files changed

+72
-4
lines changed

packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/GlobMenuEntryNodeTransformer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public function supports(Node $node): bool
8888

8989
public function getPriority(): int
9090
{
91-
// After DocumentEntryTransformer
92-
return 4500;
91+
// After GlobMenuEntryNodeTransformer
92+
// Before SubInternalMenuEntry
93+
return 4000;
9394
}
9495

9596
/** @param String[] $globExclude */

packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/InternalMenuEntryNodeTransformer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function assert;
1616
use function explode;
1717
use function implode;
18+
use function sprintf;
1819

1920
class InternalMenuEntryNodeTransformer extends AbstractMenuEntryNodeTransformer
2021
{
@@ -36,6 +37,7 @@ protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $entryNo
3637
$documentEntries = $compilerContext->getProjectNode()->getAllDocumentEntries();
3738
$currentPath = $compilerContext->getDocumentNode()->getFilePath();
3839
$maxDepth = (int) $currentMenu->getOption('maxdepth', self::DEFAULT_MAX_LEVELS);
40+
3941
foreach ($documentEntries as $documentEntry) {
4042
if (
4143
!self::matches($documentEntry->getFile(), $entryNode, $currentPath)
@@ -65,7 +67,9 @@ protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $entryNo
6567
return [$newEntryNode];
6668
}
6769

68-
return [$entryNode];
70+
$this->logger->warning(sprintf('Menu entry "%s" was not found in the document tree. Ignoring it. ', $entryNode->getUrl()), $compilerContext->getLoggerInformation());
71+
72+
return [];
6973
}
7074

7175
private static function matches(string $actualFile, InternalMenuEntryNode $parsedMenuEntryNode, string $currentFile): bool

packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/SubInternalMenuEntryNodeTransformer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
namespace phpDocumentor\Guides\Compiler\NodeTransformers\MenuNodeTransformers;
66

77
use phpDocumentor\Guides\Compiler\CompilerContext;
8+
use phpDocumentor\Guides\Exception\DocumentEntryNotFound;
89
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
910
use phpDocumentor\Guides\Nodes\Menu\InternalMenuEntryNode;
1011
use phpDocumentor\Guides\Nodes\Menu\MenuEntryNode;
1112
use phpDocumentor\Guides\Nodes\Menu\MenuNode;
1213
use phpDocumentor\Guides\Nodes\Node;
1314

1415
use function assert;
16+
use function sprintf;
1517

1618
class SubInternalMenuEntryNodeTransformer extends AbstractMenuEntryNodeTransformer
1719
{
@@ -31,6 +33,14 @@ protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $entryNo
3133
{
3234
assert($entryNode instanceof InternalMenuEntryNode);
3335
$maxDepth = (int) $currentMenu->getOption('maxdepth', self::DEFAULT_MAX_LEVELS);
36+
try {
37+
$documentEntryOfMenuEntry = $compilerContext->getProjectNode()->getDocumentEntry($entryNode->getUrl());
38+
} catch (DocumentEntryNotFound) {
39+
$this->logger->warning(sprintf('Menu entry "%s" was not found in the document tree. Ignoring it. ', $entryNode->getUrl()), $compilerContext->getLoggerInformation());
40+
41+
return [];
42+
}
43+
3444
$documentEntryOfMenuEntry = $compilerContext->getProjectNode()->getDocumentEntry($entryNode->getUrl());
3545
$this->addSubEntries($currentMenu, $compilerContext, $entryNode, $documentEntryOfMenuEntry, $entryNode->getLevel() + 1, $maxDepth);
3646

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\Exception;
6+
7+
use Exception;
8+
9+
final class DocumentEntryNotFound extends Exception
10+
{
11+
}

packages/guides/src/Nodes/ProjectNode.php

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

77
use DateTimeImmutable;
88
use Exception;
9+
use phpDocumentor\Guides\Exception\DocumentEntryNotFound;
910
use phpDocumentor\Guides\Meta\CitationTarget;
1011
use phpDocumentor\Guides\Meta\InternalTarget;
1112
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
@@ -163,6 +164,7 @@ public function getRootDocumentEntry(): DocumentEntryNode
163164
throw new Exception('No root document entry was found');
164165
}
165166

167+
/** @throws DocumentEntryNotFound */
166168
public function getDocumentEntry(string $file): DocumentEntryNode
167169
{
168170
foreach ($this->documentEntries as $documentEntry) {
@@ -171,7 +173,7 @@ public function getDocumentEntry(string $file): DocumentEntryNode
171173
}
172174
}
173175

174-
throw new Exception('No document Entry found for file ' . $file);
176+
throw new DocumentEntryNotFound('No document Entry found for file ' . $file);
175177
}
176178

177179
/** @param DocumentEntryNode[] $documentEntries */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- content start -->
2+
<div class="section" id="document-title">
3+
<h1>Document Title</h1>
4+
5+
<div class="toc">
6+
<p class="caption"><strong>Table of Contents</strong></p>
7+
<ul class="menu-level">
8+
<li class="toc-item"><a href="/page1.html#page-1">Page 1</a></li>
9+
10+
<li class="toc-item"><a href="/page2.html#page-2">Page 2</a></li>
11+
12+
</ul>
13+
</div>
14+
15+
</div>
16+
17+
<!-- content end -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.WARNING: Menu entry "page3" was not found in the document tree. Ignoring it. {"rst-file":"index.rst"} []
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Document Title
2+
==============
3+
4+
.. toctree::
5+
:caption: **Table of Contents**
6+
:titlesonly:
7+
8+
page1
9+
page2
10+
page3
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Page 1
2+
======
3+
4+
Chapter 1
5+
---------
6+
7+
Chapter 2
8+
---------
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Page 2
2+
======
3+
4+
Lorem Ipsum Dolor

0 commit comments

Comments
 (0)