Skip to content

Commit

Permalink
Tast will also receive directory of the $file.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed May 19, 2024
1 parent a788da5 commit 3e6e75b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions packages/documentator/src/Processor/Contracts/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public function getExtensions(): array;
*
* @return array<array-key, string>
*/
public function getDependencies(Directory $directory, File $file): array;
public function getDependencies(Directory $root, Directory $directory, File $file): array;

/**
* Performs action on the `$file`.
*
* @param array<string, ?File> $dependencies
*/
public function run(Directory $directory, File $file, array $dependencies): bool;
public function run(Directory $root, Directory $directory, File $file, array $dependencies): bool;
}
4 changes: 4 additions & 0 deletions packages/documentator/src/Processor/FileSystem/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,8 @@ protected function getIterator(

yield from [];
}

public function getRelativePath(self $root): string {
return Path::getRelativePath($root->getPath(), $this->path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,11 @@ public function testGetDirectoriesIterator(): void {
array_map($map, iterator_to_array($directory->getDirectoriesIterator(depth: 0))),
);
}

public function testGetRelativePath(): void {
$internal = new Directory(Path::normalize(__DIR__), false);
$directory = new Directory(Path::join(__DIR__, '..'), true);

self::assertEquals(basename(__DIR__), $internal->getRelativePath($directory));
}
}
18 changes: 10 additions & 8 deletions packages/documentator/src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public function run(string $path, ?Closure $listener = null): void {
$root = new Directory($path, true);

try {
foreach ($this->getIterator($root) as [$file, $dependencies]) {
foreach ($this->getIterator($root) as [$directory, $file, $dependencies]) {
$tasks = $this->tasks[$file->getExtension()] ?? [];
$start = microtime(true);

try {
foreach ($tasks as $task) {
try {
if (!$task->run($root, $file, $dependencies[$task] ?? [])) {
if (!$task->run($root, $directory, $file, $dependencies[$task] ?? [])) {
throw new FileTaskFailed($root, $file, $task);
}
} catch (ProcessorError $exception) {
Expand Down Expand Up @@ -93,29 +93,31 @@ public function run(string $path, ?Closure $listener = null): void {
}

/**
* @return Iterator<array-key, array{File, WeakMap<Task, array<string, ?File>>}>
* @return Iterator<array-key, array{Directory, File, WeakMap<Task, array<string, ?File>>}>
*/
protected function getIterator(Directory $root): Iterator {
$extensions = array_map(static fn ($e) => "*.{$e}", array_keys($this->tasks));
$processed = [];

foreach ($root->getFilesIterator($extensions) as $item) {
foreach ($this->getFileIterator($root, $item, [$item->getPath() => $item]) as [$file, $dependencies]) {
$files = $this->getFileIterator($root, $item, [$item->getPath() => $item]);

foreach ($files as [$directory, $file, $dependencies]) {
if (isset($processed[$file->getPath()])) {
continue;
}

$processed[$file->getPath()] = true;

yield [$file, $dependencies];
yield [$directory, $file, $dependencies];
}
}
}

/**
* @param array<string, File> $stack
*
* @return Iterator<array-key, array{File, WeakMap<Task, array<string, ?File>>}>
* @return Iterator<array-key, array{Directory, File, WeakMap<Task, array<string, ?File>>}>
*/
private function getFileIterator(Directory $root, File $file, array $stack): Iterator {
// Prepare
Expand All @@ -133,7 +135,7 @@ private function getFileIterator(Directory $root, File $file, array $stack): Ite

foreach ($tasks as $task) {
$taskDependencies = [];
$dependencies = $task->getDependencies($directory, $file);
$dependencies = $task->getDependencies($root, $directory, $file);

foreach ($dependencies as $dependency) {
// File?
Expand Down Expand Up @@ -176,6 +178,6 @@ private function getFileIterator(Directory $root, File $file, array $stack): Ite
}

// File
yield [$file, $fileDependencies];
yield [$directory, $file, $fileDependencies];
}
}
44 changes: 22 additions & 22 deletions packages/documentator/src/Processor/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getExtensions(): array {
* @inheritDoc
*/
#[Override]
public function getDependencies(Directory $directory, File $file): array {
public function getDependencies(Directory $root, Directory $directory, File $file): array {
$path = $file->getRelativePath($directory);

return match (true) {
Expand All @@ -71,13 +71,13 @@ public function getDependencies(Directory $directory, File $file): array {
* @inheritDoc
*/
#[Override]
public function run(Directory $directory, File $file, array $dependencies): bool {
public function run(Directory $root, Directory $directory, File $file, array $dependencies): bool {
$this->processed[] = [
Path::getRelativePath(__DIR__, $directory->getPath()),
$directory->getRelativePath($root),
$file->getRelativePath($directory),
array_map(
static function (?File $file) use ($directory): ?string {
return $file?->getRelativePath($directory);
static function (?File $file) use ($root): ?string {
return $file?->getRelativePath($root);
},
$dependencies,
),
Expand Down Expand Up @@ -116,27 +116,27 @@ static function (?File $file) use ($directory): ?string {
self::assertEquals(
[
[
'ProcessorTest',
'b/a/ba.txt',
'b/a',
'ba.txt',
[],
],
[
'ProcessorTest',
'',
'c.txt',
[],
],
[
'ProcessorTest',
'b/b/bb.txt',
'b/b',
'bb.txt',
[
'../../b/a/ba.txt' => 'b/a/ba.txt',
'../../c.txt' => 'c.txt',
'../../../../../README.md' => '../../../README.md',
],
],
[
'ProcessorTest',
'a/a.txt',
'a',
'a.txt',
[
'../b/b/bb.txt' => 'b/b/bb.txt',
'../c.txt' => 'c.txt',
Expand All @@ -145,18 +145,18 @@ static function (?File $file) use ($directory): ?string {
],
],
[
'ProcessorTest',
'a/a/aa.txt',
'a/a',
'aa.txt',
[],
],
[
'ProcessorTest',
'a/b/ab.txt',
'a/b',
'ab.txt',
[],
],
[
'ProcessorTest',
'b/b.txt',
'b',
'b.txt',
[],
],
],
Expand All @@ -178,7 +178,7 @@ public function getExtensions(): array {
* @inheritDoc
*/
#[Override]
public function getDependencies(Directory $directory, File $file): array {
public function getDependencies(Directory $root, Directory $directory, File $file): array {
$path = $file->getRelativePath($directory);

return match (true) {
Expand All @@ -204,7 +204,7 @@ public function getDependencies(Directory $directory, File $file): array {
* @inheritDoc
*/
#[Override]
public function run(Directory $directory, File $file, array $dependencies): bool {
public function run(Directory $root, Directory $directory, File $file, array $dependencies): bool {
return true;
}
};
Expand Down Expand Up @@ -245,7 +245,7 @@ public function getExtensions(): array {
* @inheritDoc
*/
#[Override]
public function getDependencies(Directory $directory, File $file): array {
public function getDependencies(Directory $root, Directory $directory, File $file): array {
$path = $file->getRelativePath($directory);

return match (true) {
Expand All @@ -262,7 +262,7 @@ public function getDependencies(Directory $directory, File $file): array {
* @inheritDoc
*/
#[Override]
public function run(Directory $directory, File $file, array $dependencies): bool {
public function run(Directory $root, Directory $directory, File $file, array $dependencies): bool {
return true;
}
};
Expand Down

0 comments on commit 3e6e75b

Please sign in to comment.