Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Processor #162

Merged
merged 26 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5071c21
`File` abstraction.
LastDragon-ru May 16, 2024
1bb4eb5
`Directory` abstraction.
LastDragon-ru May 17, 2024
2bcdd0d
`Processor` implementation.
LastDragon-ru May 17, 2024
ee87c4e
Added `Task::getExtensions()`.
LastDragon-ru May 18, 2024
cdc5532
Directory will return files/directories sorted by name.
LastDragon-ru May 18, 2024
a72d59a
Tests fixes.
LastDragon-ru May 18, 2024
3448b4a
If a file does not exist, the related dependency of the task will be …
LastDragon-ru May 19, 2024
a788da5
Dependencies outside root directory will not be processed.
LastDragon-ru May 19, 2024
3e6e75b
`Tast` will also receive directory of the `$file`.
LastDragon-ru May 19, 2024
8b7027a
More deep cache for resolved dependencies.
LastDragon-ru May 21, 2024
9a70ca7
`Processor` will preserve dependencies keys.
LastDragon-ru May 21, 2024
2aabd20
`Processor::getFileIterator()` will not emit the same file twice.
LastDragon-ru May 21, 2024
5067bb8
`Directory`/`File` implements `Stringable`.
LastDragon-ru May 20, 2024
8fd639e
`Directory` will support `SplFileInfo` too.
LastDragon-ru May 22, 2024
5b1754c
Removed `Tast::getDependency()`, the `Generator` will be used instead.
LastDragon-ru May 23, 2024
bf2303e
Task may return `bool` (in case when no dependencies).
LastDragon-ru May 23, 2024
f937b85
`Generator` key doesn't matter.
LastDragon-ru May 23, 2024
f22eaa5
`File` context removed.
LastDragon-ru May 23, 2024
f0b4344
Fix of determination of the dependency's `$writable`.
LastDragon-ru May 24, 2024
053fc69
`$listener($result)` can be `null`.
LastDragon-ru May 24, 2024
ff53908
`Directory::getRelativePath()`/`File::getRelativePath()` accept `File`.
LastDragon-ru May 24, 2024
7a2e1e2
`Directory::getPath()` accept `$path`.
LastDragon-ru May 24, 2024
208bf0e
`Processor` will throw `FileDependencyNotFound` instead of send `null`.
LastDragon-ru May 25, 2024
a84cb4a
Added file to `CircularDependency`.
LastDragon-ru May 25, 2024
cd7159c
`Processor` will not include dependencies time into task duration.
LastDragon-ru May 25, 2024
7a32528
`Processor` support exclusion by glob.
LastDragon-ru May 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading