Skip to content

Commit

Permalink
feat(documentator): `\LastDragon_ru\LaraASP\Documentator\Processor\Pr…
Browse files Browse the repository at this point in the history
…ocessor` allow resolve circular dependency when target outside `$output` directory.
  • Loading branch information
LastDragon-ru committed Jan 29, 2025
1 parent dd46e64 commit 9fa4aae
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/documentator/src/Processor/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,23 @@ private function file(File $file): void {
return;
}

// Event
$pathname = $this->fs->getPathname($file);

$this->dispatcher->notify(new FileStarted($pathname));

// Circular?
if (isset($this->stack[$path])) {
$this->dispatcher->notify(new FileFinished(FileFinishedResult::Failed));

throw new DependencyCircularDependency($file, array_values($this->stack));
// The $file cannot be changed if it is placed outside the output
// directory, so we can return it safely in this case.
if ($this->fs->output->isInside($file->getPath())) {
$this->dispatcher->notify(new FileStarted($this->fs->getPathname($file)));
$this->dispatcher->notify(new FileFinished(FileFinishedResult::Failed));

throw new DependencyCircularDependency($file, array_values($this->stack));
} else {
return;
}
}

// Event
$this->dispatcher->notify(new FileStarted($this->fs->getPathname($file)));

// Skipped?
if ($this->isSkipped($file)) {
$this->dispatcher->notify(new FileFinished(FileFinishedResult::Skipped));
Expand Down
59 changes: 59 additions & 0 deletions packages/documentator/src/Processor/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,65 @@ public function testRunCircularDependencySelf(): void {
->task($task)
->run($input);
}

public function testRunCircularDependencyNotWritable(): void {
$events = [];
$output = (new DirectoryPath(self::getTestData()->path('b')))->getNormalizedPath();
$input = (new DirectoryPath(self::getTestData()->path('a')))->getNormalizedPath();
$task = new ProcessorTest__Task([
'aa.txt' => ['../a.txt'],
]);

(new Processor($this->app()->make(ContainerResolver::class)))
->task($task)
->exclude(['excluded.txt', '**/**/excluded.txt'])
->listen(
static function (Event $event) use (&$events): void {
$events[] = $event;
},
)
->run($input, $output);

self::assertEquals(
[
new ProcessingStarted(),
new FileStarted('→ a.txt'),
new TaskStarted($task::class),
new TaskFinished(TaskFinishedResult::Success),
new FileFinished(FileFinishedResult::Success),
new FileStarted('→ a/aa.txt'),
new TaskStarted($task::class),
new DependencyResolved('→ a.txt', DependencyResolvedResult::Success),
new TaskFinished(TaskFinishedResult::Success),
new FileFinished(FileFinishedResult::Success),
new FileStarted('→ b/ab.txt'),
new TaskStarted($task::class),
new TaskFinished(TaskFinishedResult::Success),
new FileFinished(FileFinishedResult::Success),
new ProcessingFinished(ProcessingFinishedResult::Success),
],
$events,
);
self::assertEquals(
[
[
(string) $input->getFilePath('a.txt'),
[],
],
[
(string) $input->getFilePath('a/aa.txt'),
[
'../a.txt' => (string) $input->getFilePath('a.txt'),
],
],
[
(string) $input->getFilePath('b/ab.txt'),
[],
],
],
$task->processed,
);
}
}

// @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
Expand Down

0 comments on commit 9fa4aae

Please sign in to comment.