Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/guides/src/Compiler/Passes/GlobalMenuPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public function run(array $documents, CompilerContextInterface $compilerContext)
}

$rootDocument = null;
$rootFile = $rootDocumentEntry->getFile();
foreach ($documents as $document) {
if ($document->getDocumentEntry() === $rootDocumentEntry) {
if ($document->getDocumentEntry()->getFile() === $rootFile) {
$rootDocument = $document;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function run(array $documents, CompilerContextInterface $compilerContext)

public function isMissingInToctree(DocumentEntryNode $documentEntry, ProjectNode $projectNode): bool
{
return $documentEntry->getParent() === null && $documentEntry !== $projectNode->getRootDocumentEntry();
return $documentEntry->getParent() === null
&& $documentEntry->getFile() !== $projectNode->getRootDocumentEntry()->getFile();
}
}
22 changes: 22 additions & 0 deletions packages/guides/src/Nodes/ProjectNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ final class ProjectNode extends CompoundNode
/** @var array<string, array<string, InternalTarget>> */
private array $internalLinkTargets = [];

/** Cached root document entry for O(1) lookup */
private DocumentEntryNode|null $rootDocumentEntry = null;

/** @var DocumentEntryNode[] */
private array $documentEntries = [];
private DateTimeImmutable $lastRendered;
Expand Down Expand Up @@ -182,6 +185,10 @@ public function getAllInternalTargets(): array

public function addDocumentEntry(DocumentEntryNode $documentEntry): void
{
if ($documentEntry->isRoot()) {
$this->rootDocumentEntry = $documentEntry;
}

$this->documentEntries[$documentEntry->getFile()] = $documentEntry;
}

Expand All @@ -193,8 +200,15 @@ public function getAllDocumentEntries(): array

public function getRootDocumentEntry(): DocumentEntryNode
{
if ($this->rootDocumentEntry !== null) {
return $this->rootDocumentEntry;
}

// Fallback: scan entries (handles setDocumentEntries with numeric keys)
foreach ($this->documentEntries as $documentEntry) {
if ($documentEntry->isRoot()) {
$this->rootDocumentEntry = $documentEntry;

return $documentEntry;
}
}
Expand All @@ -205,6 +219,12 @@ public function getRootDocumentEntry(): DocumentEntryNode
/** @throws DocumentEntryNotFound */
public function getDocumentEntry(string $file): DocumentEntryNode
{
// O(1) lookup - documentEntries is keyed by file path (from addDocumentEntry)
if (isset($this->documentEntries[$file])) {
return $this->documentEntries[$file];
}

// Fallback: iterate for numerically-indexed arrays (from setDocumentEntries)
foreach ($this->documentEntries as $documentEntry) {
if ($documentEntry->getFile() === $file) {
return $documentEntry;
Expand All @@ -218,6 +238,7 @@ public function getDocumentEntry(string $file): DocumentEntryNode
public function setDocumentEntries(array $documentEntries): void
{
$this->documentEntries = $documentEntries;
$this->rootDocumentEntry = null; // Invalidate cache
}

public function findDocumentEntry(string $filePath): DocumentEntryNode|null
Expand All @@ -228,6 +249,7 @@ public function findDocumentEntry(string $filePath): DocumentEntryNode|null
public function reset(): void
{
$this->documentEntries = [];
$this->rootDocumentEntry = null;
}

public function getLastRendered(): DateTimeImmutable
Expand Down
3 changes: 2 additions & 1 deletion packages/guides/src/RenderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ public function getProjectNode(): ProjectNode

public function getDocumentNodeForEntry(DocumentEntryNode $entryNode): DocumentNode
{
$file = $entryNode->getFile();
foreach ($this->allDocuments as $child) {
if ($child->getDocumentEntry() === $entryNode) {
if ($child->getDocumentEntry()->getFile() === $file) {
return $child;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/guides/src/Renderer/DocumentTreeIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public function __construct(

public function current(): DocumentNode
{
$file = $this->levelNodes[$this->position]->getFile();
foreach ($this->documents as $document) {
if ($document->getDocumentEntry() === $this->levelNodes[$this->position]) {
if ($document->getDocumentEntry()->getFile() === $file) {
return $document;
}
}
Expand Down