Skip to content

Commit d120bf5

Browse files
committed
feat(documentator): The Processor (#162)
Small helper to iterate files and their dependencies (= other files) it the right order. (cherry picked from commit 2fdac35)
1 parent cf82d55 commit d120bf5

39 files changed

+1538
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Contracts;
4+
5+
use Generator;
6+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
7+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
8+
use SplFileInfo;
9+
10+
interface Task {
11+
/**
12+
* Returns the file extensions which task is processing.
13+
*
14+
* @return non-empty-list<string>
15+
*/
16+
public function getExtensions(): array;
17+
18+
/**
19+
* Performs action on the `$file`.
20+
*
21+
* Each returned value will be treated as a dependency of the task. It will
22+
* be resolved relative to the directory where the `$file` located,
23+
* processed, and then send back into the generator.
24+
*
25+
* @return Generator<mixed, SplFileInfo|File|string, File, bool>|bool
26+
*/
27+
public function __invoke(Directory $root, File $file): Generator|bool;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;
4+
5+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
6+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
7+
use Throwable;
8+
9+
use function array_map;
10+
use function implode;
11+
use function sprintf;
12+
13+
class CircularDependency extends ProcessorError {
14+
/**
15+
* @param list<File> $stack
16+
*/
17+
public function __construct(
18+
protected Directory $root,
19+
protected readonly File $target,
20+
protected readonly File $dependency,
21+
protected readonly array $stack,
22+
Throwable $previous = null,
23+
) {
24+
parent::__construct(
25+
sprintf(
26+
<<<'MESSAGE'
27+
Circular Dependency detected:
28+
29+
%2$s
30+
! %1$s
31+
32+
(root: `%3$s`)
33+
MESSAGE,
34+
$this->dependency->getRelativePath($this->root),
35+
'* '.implode("\n* ", array_map(fn ($f) => $f->getRelativePath($this->root), $this->stack)),
36+
$this->root->getPath(),
37+
),
38+
$previous,
39+
);
40+
}
41+
42+
public function getRoot(): Directory {
43+
return $this->root;
44+
}
45+
46+
public function getTarget(): File {
47+
return $this->target;
48+
}
49+
50+
public function getDependency(): File {
51+
return $this->dependency;
52+
}
53+
54+
/**
55+
* @return list<File>
56+
*/
57+
public function getStack(): array {
58+
return $this->stack;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;
4+
5+
use LastDragon_ru\LaraASP\Core\Utils\Path;
6+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
7+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
8+
use Throwable;
9+
10+
use function sprintf;
11+
12+
class FileDependencyNotFound extends ProcessorError {
13+
public function __construct(
14+
protected Directory $root,
15+
protected readonly File $target,
16+
protected readonly string $path,
17+
Throwable $previous = null,
18+
) {
19+
parent::__construct(
20+
sprintf(
21+
'Dependency `%s` of `%s` not found (root: `%s`).',
22+
Path::getRelativePath($this->root->getPath(), $this->path),
23+
$this->target->getRelativePath($this->root),
24+
$this->root->getPath(),
25+
),
26+
$previous,
27+
);
28+
}
29+
30+
public function getRoot(): Directory {
31+
return $this->root;
32+
}
33+
34+
public function getPath(): string {
35+
return $this->path;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;
4+
5+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
6+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
7+
use Throwable;
8+
9+
use function sprintf;
10+
11+
class FileSaveFailed extends ProcessorError {
12+
public function __construct(
13+
protected Directory $root,
14+
protected readonly File $target,
15+
Throwable $previous = null,
16+
) {
17+
parent::__construct(
18+
sprintf(
19+
'Failed to save `%s` file (root: `%s`).',
20+
$this->target->getRelativePath($this->root),
21+
$this->root->getPath(),
22+
),
23+
$previous,
24+
);
25+
}
26+
27+
public function getRoot(): Directory {
28+
return $this->root;
29+
}
30+
31+
public function getTarget(): File {
32+
return $this->target;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;
4+
5+
use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Task;
6+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
7+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
8+
use Throwable;
9+
10+
use function sprintf;
11+
12+
class FileTaskFailed extends ProcessorError {
13+
public function __construct(
14+
protected Directory $root,
15+
protected readonly File $target,
16+
protected readonly Task $task,
17+
Throwable $previous = null,
18+
) {
19+
parent::__construct(
20+
sprintf(
21+
'The `%s` task failed for `%s` file (root: `%s`).',
22+
$this->target->getRelativePath($this->root),
23+
$this->task::class,
24+
$this->root->getPath(),
25+
),
26+
$previous,
27+
);
28+
}
29+
30+
public function getRoot(): Directory {
31+
return $this->root;
32+
}
33+
34+
public function getTarget(): File {
35+
return $this->target;
36+
}
37+
38+
public function getTask(): Task {
39+
return $this->task;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;
4+
5+
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
6+
use Throwable;
7+
8+
use function sprintf;
9+
10+
class ProcessingFailed extends ProcessorError {
11+
public function __construct(
12+
protected Directory $root,
13+
Throwable $previous = null,
14+
) {
15+
parent::__construct(
16+
sprintf(
17+
'Processing failed (root: `%s`)',
18+
$this->root->getPath(),
19+
),
20+
$previous,
21+
);
22+
}
23+
24+
public function getRoot(): Directory {
25+
return $this->root;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;
4+
5+
use LastDragon_ru\LaraASP\Documentator\PackageException;
6+
7+
abstract class ProcessorError extends PackageException {
8+
// empty
9+
}

0 commit comments

Comments
 (0)