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

FileSystem events and console logger improvements. #210

Merged
merged 8 commits into from
Jan 7, 2025
7 changes: 5 additions & 2 deletions packages/documentator/src/Processor/Contracts/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Traversable;

/**
* Task dependency (= another file).
* Task dependency (= another file or directory).
*
* @template TValue of Traversable<mixed, Directory|File>|Directory|File|null
*/
Expand All @@ -23,5 +23,8 @@ interface Dependency {
*/
public function __invoke(FileSystem $fs): mixed;

public function getPath(): DirectoryPath|FilePath;
/**
* Used only for events. Relative path will be resolved based on {@see FileSystem::$input}.
*/
public function getPath(FileSystem $fs): Directory|DirectoryPath|File|FilePath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): DirectoryPath {
return match (true) {
$this->directory instanceof Directory => $this->directory->getPath(),
is_string($this->directory) => new DirectoryPath($this->directory),
default => $this->directory,
};
public function getPath(FileSystem $fs): Directory|DirectoryPath {
return is_string($this->directory)
? new DirectoryPath($this->directory)
: $this->directory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use function array_map;
use function basename;
use function iterator_to_array;
use function sprintf;

/**
* @internal
Expand All @@ -26,9 +25,12 @@ public function testGetPath(): void {
$directory = $filesystem->getDirectory(__DIR__);
$path = $directory->getPath();

self::assertEquals('path/to/directory', (string) (new DirectoryIterator('path/to/directory'))->getPath());
self::assertEquals((string) $directory, (string) (new DirectoryIterator($directory))->getPath());
self::assertEquals((string) $path, (string) (new DirectoryIterator($path))->getPath());
self::assertEquals(
'path/to/directory',
(string) (new DirectoryIterator('path/to/directory'))->getPath($filesystem),
);
self::assertEquals((string) $directory, (string) (new DirectoryIterator($directory))->getPath($filesystem));
self::assertEquals((string) $path, (string) (new DirectoryIterator($path))->getPath($filesystem));
}

public function testInvoke(): void {
Expand Down Expand Up @@ -59,12 +61,7 @@ public function testInvokeNotFound(): void {
$path = 'path/to/directory';

self::expectException(DependencyUnresolvable::class);
self::expectExceptionMessage(
sprintf(
'Dependency `%s` not found.',
$path,
),
);
self::expectExceptionMessage('Dependency not found.');

iterator_to_array(
(new DirectoryIterator($path))($fs),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): DirectoryPath {
return match (true) {
is_string($this->reference) => new DirectoryPath($this->reference),
default => $this->reference,
};
public function getPath(FileSystem $fs): DirectoryPath {
return is_string($this->reference)
? new DirectoryPath($this->reference)
: $this->reference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use LastDragon_ru\LaraASP\Documentator\Testing\Package\WithProcessor;
use PHPUnit\Framework\Attributes\CoversClass;

use function sprintf;

/**
* @internal
*/
Expand All @@ -18,10 +16,14 @@ final class DirectoryReferenceTest extends TestCase {
use WithProcessor;

public function testGetPath(): void {
$path = (new DirectoryPath(__DIR__))->getNormalizedPath();
$filesystem = $this->getFileSystem(__DIR__);
$path = (new DirectoryPath(__DIR__))->getNormalizedPath();

self::assertEquals('path/to/directory', (string) (new DirectoryReference('path/to/directory'))->getPath());
self::assertEquals((string) $path, (string) (new DirectoryReference($path))->getPath());
self::assertEquals(
'path/to/directory',
(string) (new DirectoryReference('path/to/directory'))->getPath($filesystem),
);
self::assertEquals((string) $path, (string) (new DirectoryReference($path))->getPath($filesystem));
}

public function testInvoke(): void {
Expand All @@ -41,12 +43,7 @@ public function testInvokeNotFound(): void {
$path = 'path/to/directory';

self::expectException(DependencyUnresolvable::class);
self::expectExceptionMessage(
sprintf(
'Dependency `%s` not found.',
$path,
),
);
self::expectExceptionMessage('Dependency not found.');

(new DirectoryReference($path))($fs);
}
Expand Down
11 changes: 6 additions & 5 deletions packages/documentator/src/Processor/Dependencies/FileCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Override;
use Stringable;

use function is_string;

/**
* @implements Dependency<File>
*/
Expand All @@ -26,10 +28,9 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): FilePath {
return match (true) {
$this->file instanceof FilePath => $this->file,
default => new FilePath($this->file),
};
public function getPath(FileSystem $fs): FilePath {
return $fs->output->getPath(
is_string($this->file) ? new FilePath($this->file) : $this->file,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Mockery;
use PHPUnit\Framework\Attributes\CoversClass;

use function dirname;

/**
* @internal
*/
Expand All @@ -18,10 +20,14 @@ final class FileCreateTest extends TestCase {
use WithProcessor;

public function testGetPath(): void {
$path = (new FilePath(__FILE__))->getNormalizedPath();
$filesystem = $this->getFileSystem(dirname(__DIR__), __DIR__);
$path = (new FilePath(__FILE__))->getNormalizedPath();

self::assertEquals('path/to/file', (string) (new FileCreate('path/to/file', ''))->getPath());
self::assertEquals((string) $path, (string) (new FileCreate($path, ''))->getPath());
self::assertEquals(
(string) $filesystem->output->getFilePath('path/to/file'),
(string) (new FileCreate('path/to/file', ''))->getPath($filesystem),
);
self::assertEquals((string) $path, (string) (new FileCreate($path, ''))->getPath($filesystem));
}

public function testInvoke(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): DirectoryPath {
return match (true) {
$this->directory instanceof Directory => $this->directory->getPath(),
is_string($this->directory) => new DirectoryPath($this->directory),
default => $this->directory,
};
public function getPath(FileSystem $fs): Directory|DirectoryPath {
return is_string($this->directory)
? new DirectoryPath($this->directory)
: $this->directory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function array_map;
use function basename;
use function iterator_to_array;
use function sprintf;

/**
* @internal
Expand All @@ -27,9 +26,9 @@ public function testGetPath(): void {
$directory = $filesystem->getDirectory(__DIR__);
$path = $directory->getPath();

self::assertEquals('path/to/directory', (string) (new FileIterator('path/to/directory'))->getPath());
self::assertEquals((string) $directory, (string) (new FileIterator($directory))->getPath());
self::assertEquals((string) $path, (string) (new FileIterator($path))->getPath());
self::assertEquals('path/to/directory', (string) (new FileIterator('path/to/directory'))->getPath($filesystem));
self::assertEquals((string) $directory, (string) (new FileIterator($directory))->getPath($filesystem));
self::assertEquals((string) $path, (string) (new FileIterator($path))->getPath($filesystem));
}

public function testInvoke(): void {
Expand Down Expand Up @@ -62,12 +61,7 @@ public function testInvokeNotFound(): void {
$path = 'path/to/directory';

self::expectException(DependencyUnresolvable::class);
self::expectExceptionMessage(
sprintf(
'Dependency `%s` not found.',
$path,
),
);
self::expectExceptionMessage('Dependency not found.');

iterator_to_array(
(new FileIterator($path))($fs),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): FilePath {
return match (true) {
is_string($this->reference) => new FilePath($this->reference),
default => $this->reference,
};
public function getPath(FileSystem $fs): FilePath {
return is_string($this->reference)
? new FilePath($this->reference)
: $this->reference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\Attributes\CoversClass;

use function basename;
use function sprintf;

/**
* @internal
Expand All @@ -19,10 +18,11 @@ final class FileReferenceTest extends TestCase {
use WithProcessor;

public function testGetPath(): void {
$path = (new FilePath(__FILE__))->getNormalizedPath();
$filesystem = $this->getFileSystem(__DIR__);
$path = (new FilePath(__FILE__))->getNormalizedPath();

self::assertEquals('path/to/file', (string) (new FileReference('path/to/file'))->getPath());
self::assertEquals((string) $path, (string) (new FileReference($path))->getPath());
self::assertEquals('path/to/file', (string) (new FileReference('path/to/file'))->getPath($filesystem));
self::assertEquals((string) $path, (string) (new FileReference($path))->getPath($filesystem));
}

public function testInvoke(): void {
Expand All @@ -43,12 +43,7 @@ public function testInvokeNotFound(): void {
$path = 'path/to/file';

self::expectException(DependencyUnresolvable::class);
self::expectExceptionMessage(
sprintf(
'Dependency `%s` not found.',
$path,
),
);
self::expectExceptionMessage('Dependency not found.');

(new FileReference($path))($fs);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/documentator/src/Processor/Dependencies/FileSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LastDragon_ru\LaraASP\Documentator\Processor\Dependencies;

use LastDragon_ru\LaraASP\Core\Path\FilePath;
use LastDragon_ru\LaraASP\Documentator\Processor\Contracts\Dependency;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\File;
use LastDragon_ru\LaraASP\Documentator\Processor\FileSystem\FileSystem;
Expand All @@ -28,7 +27,7 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): FilePath {
return $this->file->getPath();
public function getPath(FileSystem $fs): File {
return $this->file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testGetPath(): void {
$fs = $this->getFileSystem(__DIR__);
$file = $fs->getFile(__FILE__);

self::assertEquals((string) $file, (string) (new FileSave($file, ''))->getPath());
self::assertEquals((string) $file, (string) (new FileSave($file, ''))->getPath($fs));
}

public function testInvoke(): void {
Expand Down
4 changes: 2 additions & 2 deletions packages/documentator/src/Processor/Dependencies/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __invoke(FileSystem $fs): mixed {
}

#[Override]
public function getPath(): DirectoryPath|FilePath {
return $this->dependency->getPath();
public function getPath(FileSystem $fs): Directory|DirectoryPath|File|FilePath {
return $this->dependency->getPath($fs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ final class OptionalTest extends TestCase {
use WithProcessor;

public function testGetPath(): void {
$filesystem = $this->getFileSystem(__DIR__);
$dependency = new FileReference('path/to/file');
$optional = new Optional($dependency);

self::assertEquals($dependency->getPath(), $optional->getPath());
self::assertEquals($dependency->getPath($filesystem), $optional->getPath($filesystem));
}

public function testInvoke(): void {
Expand Down
13 changes: 13 additions & 0 deletions packages/documentator/src/Processor/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor;

use LastDragon_ru\LaraASP\Core\Observer\Dispatcher as CoreDispatcher;
use LastDragon_ru\LaraASP\Documentator\Processor\Events\Event;

/**
* @extends CoreDispatcher<Event>
*/
class Dispatcher extends CoreDispatcher {
// empty
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

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
*/
Expand Down
15 changes: 15 additions & 0 deletions packages/documentator/src/Processor/Events/FileSystemModified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Processor\Events;

readonly class FileSystemModified implements Event {
public function __construct(
/**
* @var non-empty-string
*/
public string $path,
public FileSystemModifiedType $type,
) {
// 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 FileSystemModifiedType {
case Created;
case Updated;
}
Loading
Loading