-
Notifications
You must be signed in to change notification settings - Fork 0
One reader for the PHP files in a directory #650
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a0504af
Test one reader for PHP files in a directory
Firehed 1e8e4ea
Add PhpDirectoryReader
Firehed e5e90bb
ComposerMapBackend walks through PhpDirectoryReader
Firehed 6fc3f6d
Filesystem allowlist admits PhpDirectoryReader
Firehed 7f0e0b1
Add the Filesystem layer
Firehed 60f9bb8
Root does not use Filesystem yet
Firehed 95a0e68
Hold the file-extension filter as internal state
Firehed 5cb3e9f
PhpDirectoryReader iterates via FilesystemIterator
Firehed 04fe907
Leave note in place
Firehed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Firehed\PhpLsp\Filesystem; | ||
|
|
||
| final readonly class DirectoryListing | ||
| { | ||
| /** | ||
| * @param list<string> $files Full paths of the PHP files directly inside | ||
| * @param list<string> $directories Full paths of the directories directly inside | ||
| */ | ||
| public function __construct( | ||
| public string $path, | ||
| public array $files, | ||
| public array $directories, | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Firehed\PhpLsp\Filesystem; | ||
|
|
||
| use FilesystemIterator; | ||
| use SplFileInfo; | ||
|
|
||
| final class PhpDirectoryReader | ||
| { | ||
| /** @var list<non-empty-string> */ | ||
| private array $extensions; | ||
|
|
||
| public function __construct() | ||
| { | ||
| // Future scope: allow other extensions via constructor injection | ||
| $this->extensions = ['php']; | ||
| } | ||
|
|
||
| public function read(string $directory): ?DirectoryListing | ||
| { | ||
| if (!is_dir($directory)) { | ||
| return null; | ||
| } | ||
|
|
||
| $files = []; | ||
| $directories = []; | ||
| foreach (new FilesystemIterator($directory, FilesystemIterator::SKIP_DOTS) as $entry) { | ||
| \assert($entry instanceof SplFileInfo); | ||
| $path = $entry->getPathname(); | ||
| if ($entry->isDir()) { | ||
| $directories[] = $path; | ||
| } elseif ($entry->isFile() && $this->hasWatchedExtension($entry->getFilename())) { | ||
| $files[] = $path; | ||
| } | ||
| } | ||
|
|
||
| return new DirectoryListing($directory, $files, $directories); | ||
| } | ||
|
|
||
| private function hasWatchedExtension(string $entry): bool | ||
| { | ||
| foreach ($this->extensions as $extension) { | ||
| if (str_ends_with($entry, '.' . $extension)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<DirectoryListing> The directory, then every directory beneath it | ||
| */ | ||
| public function walk(string $directory): iterable | ||
| { | ||
| $listing = $this->read($directory); | ||
| if ($listing === null) { | ||
| return; | ||
| } | ||
|
|
||
| yield $listing; | ||
| foreach ($listing->directories as $child) { | ||
| yield from $this->walk($child); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Firehed\PhpLsp\Tests\Filesystem; | ||
|
|
||
| use Firehed\PhpLsp\Filesystem\DirectoryListing; | ||
| use Firehed\PhpLsp\Filesystem\PhpDirectoryReader; | ||
| use Firehed\PhpLsp\Tests\LoadsFixturesTrait; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| #[CoversClass(PhpDirectoryReader::class)] | ||
| #[CoversClass(DirectoryListing::class)] | ||
| final class PhpDirectoryReaderTest extends TestCase | ||
| { | ||
| use LoadsFixturesTrait; | ||
|
|
||
| public function testReadListsPhpFilesAndDirectoriesOneLevelDown(): void | ||
| { | ||
| $root = rtrim($this->fixturePath(''), '/'); | ||
|
|
||
| $listing = (new PhpDirectoryReader())->read($root); | ||
|
|
||
| self::assertNotNull($listing, 'the fixtures root is a directory'); | ||
| self::assertSame($root, $listing->path, 'a listing names the directory it describes'); | ||
| self::assertContains($root . '/NoNamespace.php', $listing->files, 'a PHP file is listed by its full path'); | ||
| self::assertNotContains($root . '/composer.json', $listing->files, 'only PHP files are listed'); | ||
| self::assertContains($root . '/src', $listing->directories, 'a directory is listed by its full path'); | ||
| self::assertNotContains( | ||
| $root . '/src/Domain/User.php', | ||
| $listing->files, | ||
| 'a listing describes one directory, not what is beneath it', | ||
| ); | ||
| } | ||
|
|
||
| public function testReadOfSomethingThatIsNotADirectoryIsNull(): void | ||
| { | ||
| $reader = new PhpDirectoryReader(); | ||
|
|
||
| self::assertNull($reader->read($this->fixturePath('no/such/directory')), 'a missing path has no listing'); | ||
| self::assertNull($reader->read($this->fixturePath('NoNamespace.php')), 'a file has no listing'); | ||
| } | ||
|
|
||
| public function testWalkReachesEveryDirectoryBeneathTheRoot(): void | ||
| { | ||
| $root = $this->fixturePath('src'); | ||
|
|
||
| $paths = []; | ||
| $files = []; | ||
| foreach ((new PhpDirectoryReader())->walk($root) as $listing) { | ||
| $paths[] = $listing->path; | ||
| $files = [...$files, ...$listing->files]; | ||
| } | ||
|
|
||
| self::assertSame($root, $paths[0], 'the walk starts with the root itself'); | ||
| self::assertContains($root . '/Domain', $paths, 'a directory beneath the root is walked'); | ||
| self::assertContains($root . '/Domain/User.php', $files, 'a file in a nested directory is reached'); | ||
| self::assertSame(array_unique($paths), $paths, 'no directory is walked twice'); | ||
| } | ||
|
|
||
| public function testWalkOfSomethingThatIsNotADirectoryIsEmpty(): void | ||
| { | ||
| self::assertSame( | ||
| [], | ||
| iterator_to_array((new PhpDirectoryReader())->walk($this->fixturePath('no/such/directory')), false), | ||
| 'an autoload root that does not exist contributes nothing', | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably leverage the native directory iteration tooling