Skip to content

Commit accf732

Browse files
committed
Register Section Entries
1 parent 5f6cd7d commit accf732

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\Compiler\NodeTransformers;
6+
7+
use phpDocumentor\Guides\Compiler\CompilerContext;
8+
use phpDocumentor\Guides\Compiler\NodeTransformer;
9+
use phpDocumentor\Guides\Nodes\DocumentTree\SectionEntryNode;
10+
use phpDocumentor\Guides\Nodes\Node;
11+
use phpDocumentor\Guides\Nodes\SectionNode;
12+
13+
use function array_pop;
14+
use function assert;
15+
use function end;
16+
use function sizeof;
17+
18+
/** @implements NodeTransformer<Node> */
19+
class SectionEntryRegistrationTransformer implements NodeTransformer
20+
{
21+
/** @var SectionEntryNode $sectionStack */
22+
private array $sectionStack = [];
23+
24+
public function enterNode(Node $node, CompilerContext $compilerContext): Node
25+
{
26+
if (!$node instanceof SectionNode) {
27+
return $node;
28+
}
29+
30+
$sectionEntryNode = new SectionEntryNode($node->getTitle());
31+
if (sizeof($this->sectionStack) === 0) {
32+
$compilerContext->getDocumentNode()->getDocumentEntry()->addSection($sectionEntryNode);
33+
} else {
34+
$parentSection = end($this->sectionStack);
35+
assert($parentSection instanceof SectionEntryNode);
36+
$parentSection->addChild($sectionEntryNode);
37+
}
38+
39+
$this->sectionStack[] = $sectionEntryNode;
40+
41+
return $node;
42+
}
43+
44+
public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
45+
{
46+
if (!$node instanceof SectionNode) {
47+
return $node;
48+
}
49+
50+
array_pop($this->sectionStack);
51+
52+
return $node;
53+
}
54+
55+
public function supports(Node $node): bool
56+
{
57+
return $node instanceof SectionNode;
58+
}
59+
60+
public function getPriority(): int
61+
{
62+
// After DocumentEntryRegistrationTransformer
63+
return 4900;
64+
}
65+
}

packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class DocumentEntryNode extends AbstractNode implements Entry
1313
{
1414
/** @var Entry[] */
1515
private array $entries = [];
16+
/** @var SectionEntryNode[] */
17+
private array $sections = [];
1618
private DocumentEntryNode|null $parent = null;
1719

1820
public function __construct(private readonly string $file, private readonly TitleNode $titleNode)
@@ -45,6 +47,17 @@ public function setParent(DocumentEntryNode|null $parent): void
4547
$this->parent = $parent;
4648
}
4749

50+
/** @return SectionEntryNode[] */
51+
public function getSections(): array
52+
{
53+
return $this->sections;
54+
}
55+
56+
public function addSection(SectionEntryNode $section): void
57+
{
58+
$this->sections[] = $section;
59+
}
60+
4861
public function getFile(): string
4962
{
5063
return $this->file;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\Compiler\NodeTransformers;
6+
7+
use phpDocumentor\Guides\Compiler\CompilerContext;
8+
use phpDocumentor\Guides\Nodes\DocumentNode;
9+
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
10+
use phpDocumentor\Guides\Nodes\DocumentTree\SectionEntryNode;
11+
use phpDocumentor\Guides\Nodes\ProjectNode;
12+
use phpDocumentor\Guides\Nodes\SectionNode;
13+
use phpDocumentor\Guides\Nodes\TitleNode;
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
16+
17+
class SectionEntryRegistrationTransformerTest extends TestCase
18+
{
19+
private CompilerContext $context;
20+
21+
protected function setUp(): void
22+
{
23+
$this->context = self::getCompilerContext('some/path');
24+
}
25+
26+
private static function getCompilerContext(string $path): CompilerContext
27+
{
28+
$context = new CompilerContext(new ProjectNode());
29+
$document = new DocumentNode('123', $path);
30+
$document = $document->withDocumentEntry(new DocumentEntryNode($path, TitleNode::emptyNode()));
31+
32+
return $context->withShadowTree($document);
33+
}
34+
35+
public function testSectionGetsRegistered(): void
36+
{
37+
$node = new SectionNode(TitleNode::emptyNode());
38+
39+
$transformer = new SectionEntryRegistrationTransformer();
40+
41+
$transformer->enterNode($node, $this->context);
42+
$transformer->leaveNode($node, $this->context);
43+
self::assertCount(1, $this->context->getDocumentNode()->getDocumentEntry()->getSections());
44+
self::assertInstanceOf(SectionEntryNode::class, $this->context->getDocumentNode()->getDocumentEntry()->getSections()[0]);
45+
}
46+
}

0 commit comments

Comments
 (0)