Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dceb030

Browse files
committed
[Store] Add maxDepth to RstToctreeLoader for scoped indexing
1 parent 121b38f commit dceb030

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
0.9
5+
---
6+
7+
* Add `maxDepth` constructor parameter to `RstToctreeLoader` to limit toctree recursion depth
8+
49
0.8
510
---
611

src/Document/Loader/RstToctreeLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public function __construct(
2929
private RstLoader $rstLoader = new RstLoader(),
3030
private bool $throwOnMissingEntry = false,
3131
private LoggerInterface $logger = new NullLogger(),
32+
private ?int $maxDepth = null,
3233
) {
34+
if (null !== $maxDepth && $maxDepth < 0) {
35+
throw new InvalidArgumentException(\sprintf('The maximum depth must be a non-negative integer or null, %d given.', $maxDepth));
36+
}
3337
}
3438

3539
/**
@@ -63,6 +67,10 @@ private function processFile(string $path, int $depth, string $rootDir): iterabl
6367

6468
yield from $this->rstLoader->loadContent($content, $path, ['depth' => $depth]);
6569

70+
if (null !== $this->maxDepth && $depth >= $this->maxDepth) {
71+
return;
72+
}
73+
6674
foreach ($this->parseToctreeEntries($content, \dirname($path), $rootDir) as $entryPath) {
6775
yield from $this->processFile($entryPath, $depth + 1, $rootDir);
6876
}

tests/Document/Loader/RstToctreeLoaderTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,58 @@ public function testLoadToctreeSkipsMissingEntryByDefault()
270270
}
271271
}
272272

273+
public function testConstructorRejectsNegativeMaxDepth()
274+
{
275+
$this->expectException(InvalidArgumentException::class);
276+
$this->expectExceptionMessage('The maximum depth must be a non-negative integer or null, -1 given.');
277+
new RstToctreeLoader(maxDepth: -1);
278+
}
279+
280+
public function testMaxDepthZeroLoadsOnlySourceFile()
281+
{
282+
$loader = new RstToctreeLoader(maxDepth: 0);
283+
$documents = iterator_to_array($loader->load($this->fixturesDir.'/with_absolute_toctree/index.rst'), false);
284+
285+
$titles = array_map(
286+
static fn (EmbeddableDocumentInterface $doc): string => $doc->getMetadata()->getTitle() ?? '',
287+
$documents,
288+
);
289+
290+
$this->assertContains('Main Documentation', $titles);
291+
$this->assertNotContains('Getting Started', $titles);
292+
$this->assertNotContains('Setup', $titles);
293+
}
294+
295+
public function testMaxDepthOneFollowsDirectEntriesOnly()
296+
{
297+
$loader = new RstToctreeLoader(maxDepth: 1);
298+
$documents = iterator_to_array($loader->load($this->fixturesDir.'/with_absolute_toctree/index.rst'), false);
299+
300+
$titles = array_map(
301+
static fn (EmbeddableDocumentInterface $doc): string => $doc->getMetadata()->getTitle() ?? '',
302+
$documents,
303+
);
304+
305+
$this->assertContains('Main Documentation', $titles);
306+
$this->assertContains('Getting Started', $titles);
307+
$this->assertNotContains('Setup', $titles);
308+
}
309+
310+
public function testNullMaxDepthLoadsEntireTree()
311+
{
312+
$loader = new RstToctreeLoader(maxDepth: null);
313+
$documents = iterator_to_array($loader->load($this->fixturesDir.'/with_absolute_toctree/index.rst'), false);
314+
315+
$titles = array_map(
316+
static fn (EmbeddableDocumentInterface $doc): string => $doc->getMetadata()->getTitle() ?? '',
317+
$documents,
318+
);
319+
320+
$this->assertContains('Main Documentation', $titles);
321+
$this->assertContains('Getting Started', $titles);
322+
$this->assertContains('Setup', $titles);
323+
}
324+
273325
public function testLoadSectionOverflowCreatesMultipleChunks()
274326
{
275327
// Create a temporary file with a very long section

0 commit comments

Comments
 (0)