Skip to content

Commit

Permalink
feat(documentator): The Processor (#162)
Browse files Browse the repository at this point in the history
Small helper to iterate files and their dependencies (= other files) it the right order.

(cherry picked from commit 2fdac35)
  • Loading branch information
LastDragon-ru committed May 26, 2024
1 parent cf82d55 commit d120bf5
Show file tree
Hide file tree
Showing 39 changed files with 1,538 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/documentator/src/Processor/Contracts/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Contracts;

use Generator;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use SplFileInfo;

interface Task {
/**
* Returns the file extensions which task is processing.
*
* @return non-empty-list<string>
*/
public function getExtensions(): array;

/**
* Performs action on the `$file`.
*
* Each returned value will be treated as a dependency of the task. It will
* be resolved relative to the directory where the `$file` located,
* processed, and then send back into the generator.
*
* @return Generator<mixed, SplFileInfo|File|string, File, bool>|bool
*/
public function __invoke(Directory $root, File $file): Generator|bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;

use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use Throwable;

use function array_map;
use function implode;
use function sprintf;

class CircularDependency extends ProcessorError {
/**
* @param list<File> $stack
*/
public function __construct(
protected Directory $root,
protected readonly File $target,
protected readonly File $dependency,
protected readonly array $stack,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
<<<'MESSAGE'
Circular Dependency detected:
%2$s
! %1$s
(root: `%3$s`)
MESSAGE,
$this->dependency->getRelativePath($this->root),
'* '.implode("\n* ", array_map(fn ($f) => $f->getRelativePath($this->root), $this->stack)),
$this->root->getPath(),
),
$previous,
);
}

public function getRoot(): Directory {
return $this->root;
}

public function getTarget(): File {
return $this->target;
}

public function getDependency(): File {
return $this->dependency;
}

/**
* @return list<File>
*/
public function getStack(): array {
return $this->stack;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;

use LastDragon_ru\LaraASP\Core\Utils\Path;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use Throwable;

use function sprintf;

class FileDependencyNotFound extends ProcessorError {
public function __construct(
protected Directory $root,
protected readonly File $target,
protected readonly string $path,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
'Dependency `%s` of `%s` not found (root: `%s`).',
Path::getRelativePath($this->root->getPath(), $this->path),
$this->target->getRelativePath($this->root),
$this->root->getPath(),
),
$previous,
);
}

public function getRoot(): Directory {
return $this->root;
}

public function getPath(): string {
return $this->path;
}
}
34 changes: 34 additions & 0 deletions packages/documentator/src/Processor/Exceptions/FileSaveFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;

use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use Throwable;

use function sprintf;

class FileSaveFailed extends ProcessorError {
public function __construct(
protected Directory $root,
protected readonly File $target,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
'Failed to save `%s` file (root: `%s`).',
$this->target->getRelativePath($this->root),
$this->root->getPath(),
),
$previous,
);
}

public function getRoot(): Directory {
return $this->root;
}

public function getTarget(): File {
return $this->target;
}
}
41 changes: 41 additions & 0 deletions packages/documentator/src/Processor/Exceptions/FileTaskFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;

use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Task;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use Throwable;

use function sprintf;

class FileTaskFailed extends ProcessorError {
public function __construct(
protected Directory $root,
protected readonly File $target,
protected readonly Task $task,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
'The `%s` task failed for `%s` file (root: `%s`).',
$this->target->getRelativePath($this->root),
$this->task::class,
$this->root->getPath(),
),
$previous,
);
}

public function getRoot(): Directory {
return $this->root;
}

public function getTarget(): File {
return $this->target;
}

public function getTask(): Task {
return $this->task;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;

use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\Directory;
use Throwable;

use function sprintf;

class ProcessingFailed extends ProcessorError {
public function __construct(
protected Directory $root,
Throwable $previous = null,
) {
parent::__construct(
sprintf(
'Processing failed (root: `%s`)',
$this->root->getPath(),
),
$previous,
);
}

public function getRoot(): Directory {
return $this->root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Exceptions;

use LastDragon_ru\LaraASP\Documentator\PackageException;

abstract class ProcessorError extends PackageException {
// empty
}
Loading

0 comments on commit d120bf5

Please sign in to comment.