Skip to content

Commit

Permalink
feat(documentator)!: `\LastDragon_ru\LaraASP\Documentator\Processor\P…
Browse files Browse the repository at this point in the history
…rocessor` events (#209)
  • Loading branch information
LastDragon-ru authored Jan 3, 2025
2 parents a5937b4 + f250f47 commit eb02719
Show file tree
Hide file tree
Showing 26 changed files with 831 additions and 202 deletions.
39 changes: 9 additions & 30 deletions packages/documentator/src/Commands/Preprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use LastDragon_ru\LaraASP\Documentator\Package;
use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Task;
use LastDragon_ru\LaraASP\Documentator\Processor\InstanceFactory;
use LastDragon_ru\LaraASP\Documentator\Processor\Listeners\Console\Writer;
use LastDragon_ru\LaraASP\Documentator\Processor\Processor;
use LastDragon_ru\LaraASP\Documentator\Processor\Result;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\CodeLinks\Task as CodeLinksTask;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Contracts\Instruction;
use LastDragon_ru\LaraASP\Documentator\Processor\Tasks\Preprocess\Contracts\Parameters;
Expand All @@ -36,8 +36,6 @@
use ReflectionClass;
use ReflectionProperty;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
use UnitEnum;

use function array_map;
Expand All @@ -48,7 +46,6 @@
use function is_scalar;
use function ksort;
use function max;
use function mb_strlen;
use function min;
use function rtrim;
use function str_repeat;
Expand Down Expand Up @@ -96,32 +93,14 @@ public function __construct(
}

public function __invoke(Formatter $formatter): void {
$cwd = getcwd();
$path = new DirectoryPath(Cast::toString($this->argument('path') ?? $cwd));
$width = min((new Terminal())->getWidth(), 150);
$exclude = array_map(strval(...), (array) $this->option('exclude'));
$listener = function (FilePath $path, Result $result, float $duration) use ($formatter, $width): void {
[$resultMessage, $resultColor, $resultVerbosity] = match ($result) {
Result::Failed => ['FAIL', 'red', OutputInterface::VERBOSITY_NORMAL],
Result::Success => ['DONE', 'green', OutputInterface::VERBOSITY_NORMAL],
Result::Skipped => ['SKIP', 'gray', OutputInterface::VERBOSITY_VERBOSE],
Result::Missed => ['MISS', 'yellow', OutputInterface::VERBOSITY_VERBOSE],
};

$duration = $formatter->duration($duration);
$length = $width - (mb_strlen((string) $path) + mb_strlen($duration) + mb_strlen($resultMessage) + 5);
$line = $path
.' '.($length > 0 ? '<fg=gray>'.str_repeat('.', $length).'</>' : '')
.' '."<fg=gray>{$duration}</>"
.' '."<fg={$resultColor};options=bold>{$resultMessage}</>";

$this->output->writeln($line, OutputInterface::OUTPUT_NORMAL | $resultVerbosity);
};

$duration = $this->processor()->exclude($exclude)->run($path, listener: $listener);

$this->output->newLine();
$this->output->writeln("<fg=green;options=bold>DONE ({$formatter->duration($duration)})</>");
$cwd = getcwd();
$path = new DirectoryPath(Cast::toString($this->argument('path') ?? $cwd));
$exclude = array_map(strval(...), (array) $this->option('exclude'));

$this->processor()
->listen((new Writer($this->output, $formatter))(...))
->exclude($exclude)
->run($path);
}

private function processor(): Processor {
Expand Down
21 changes: 21 additions & 0 deletions packages/documentator/src/Processor/Events/DependencyResolved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Dependency;

readonly class DependencyResolved implements Event {
public function __construct(
/**
* @var class-string<Dependency<*>>
*/
public string $dependency,
/**
* @var non-empty-string
*/
public string $path,
public DependencyResolvedResult $result,
) {
// empty
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

enum DependencyResolvedResult {
case Success;
case Missed;
case Failed;
case Null;
}
7 changes: 7 additions & 0 deletions packages/documentator/src/Processor/Events/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

interface Event {
// empty
}
11 changes: 11 additions & 0 deletions packages/documentator/src/Processor/Events/FileFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

readonly class FileFinished implements Event {
public function __construct(
public FileFinishedResult $result,
) {
// empty
}
}
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\Events;

enum FileFinishedResult {
case Success;
case Skipped;
case Failed;
}
14 changes: 14 additions & 0 deletions packages/documentator/src/Processor/Events/FileStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

readonly class FileStarted implements Event {
public function __construct(
/**
* @var non-empty-string
*/
public string $path,
) {
// empty
}
}
11 changes: 11 additions & 0 deletions packages/documentator/src/Processor/Events/ProcessingFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

readonly class ProcessingFinished implements Event {
public function __construct(
public ProcessingFinishedResult $result,
) {
// empty
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

enum ProcessingFinishedResult {
case Success;
case Failed;
}
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\Events;

readonly class ProcessingStarted implements Event {
public function __construct() {
// empty
}
}
11 changes: 11 additions & 0 deletions packages/documentator/src/Processor/Events/TaskFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

readonly class TaskFinished implements Event {
public function __construct(
public TaskFinishedResult $result,
) {
// empty
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

enum TaskFinishedResult {
case Success;
case Failed;
}
16 changes: 16 additions & 0 deletions packages/documentator/src/Processor/Events/TaskStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Task;

readonly class TaskStarted implements Event {
public function __construct(
/**
* @var class-string<Task>
*/
public string $task,
) {
// empty
}
}
Loading

0 comments on commit eb02719

Please sign in to comment.