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

Processor events #209

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading