-
-
Notifications
You must be signed in to change notification settings - Fork 74
Add support for file path iterables in ColocatedMappingDriver
#432
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,27 +4,15 @@ | |
|
|
||
| namespace Doctrine\Persistence\Mapping\Driver; | ||
|
|
||
| use AppendIterator; | ||
| use Doctrine\Persistence\Mapping\MappingException; | ||
| use FilesystemIterator; | ||
| use Generator; | ||
| use Iterator; | ||
| use RecursiveDirectoryIterator; | ||
| use RecursiveIteratorIterator; | ||
| use ReflectionClass; | ||
| use RegexIterator; | ||
| use SplFileInfo; | ||
|
|
||
| use function array_merge; | ||
| use function array_unique; | ||
| use function assert; | ||
| use function get_declared_classes; | ||
| use function in_array; | ||
| use function is_dir; | ||
| use function preg_match; | ||
| use function preg_quote; | ||
| use function realpath; | ||
| use function sprintf; | ||
| use function str_contains; | ||
| use function str_replace; | ||
|
|
||
|
|
@@ -33,8 +21,11 @@ | |
| */ | ||
| trait ColocatedMappingDriver | ||
| { | ||
| /** @var iterable<array-key,string> */ | ||
| private iterable $filePaths; | ||
|
|
||
| /** | ||
| * The paths where to look for mapping files. | ||
| * The directory paths where to look for mapping files. | ||
| * | ||
| * @var array<int, string> | ||
| */ | ||
|
|
@@ -51,7 +42,7 @@ trait ColocatedMappingDriver | |
| protected string $fileExtension = '.php'; | ||
|
|
||
| /** | ||
| * Cache for getAllClassNames(). | ||
| * Cache for {@see getAllClassNames()}. | ||
| * | ||
| * @var array<int, string>|null | ||
| * @phpstan-var list<class-string>|null | ||
|
|
@@ -79,7 +70,7 @@ public function getPaths(): array | |
| } | ||
|
|
||
| /** | ||
| * Append exclude lookup paths to metadata driver. | ||
| * Append exclude lookup paths to a metadata driver. | ||
| * | ||
| * @param string[] $paths | ||
| */ | ||
|
|
@@ -132,35 +123,22 @@ public function getAllClassNames(): array | |
| return $this->classNames; | ||
| } | ||
|
|
||
| if ($this->paths === []) { | ||
| if ($this->paths === [] && ! isset($this->filePaths)) { | ||
| throw MappingException::pathRequiredForDriver(static::class); | ||
| } | ||
|
|
||
| /** @var AppendIterator<array-key,SplFileInfo,Iterator<array-key,SplFileInfo>> $filesIterator */ | ||
| $filesIterator = new AppendIterator(); | ||
|
|
||
| foreach ($this->paths as $path) { | ||
| if (! is_dir($path)) { | ||
| throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); | ||
| } | ||
| $dirFilesIterator = new DirectoryFilesIterator($this->paths, $this->fileExtension); | ||
|
|
||
| /** @var Iterator<array-key,SplFileInfo> $iterator */ | ||
| $iterator = new RegexIterator( | ||
| new RecursiveIteratorIterator( | ||
| new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), | ||
| RecursiveIteratorIterator::LEAVES_ONLY, | ||
| ), | ||
| sprintf('/%s$/', preg_quote($this->fileExtension, '/')), | ||
| RegexIterator::MATCH, | ||
| ); | ||
|
|
||
| $filesIterator->append($iterator); | ||
| } | ||
| /** @var iterable<string> $filePathsIterator */ | ||
| $filePathsIterator = $this->concatIterables( | ||
| $this->filePaths ?? [], | ||
| new FilePathNameIterator($dirFilesIterator), | ||
| ); | ||
|
|
||
| $sourceFilePathNames = $this->pathNameIterator($filesIterator); | ||
| $includedFiles = []; | ||
| /** @var array<string,true> $includedFiles */ | ||
| $includedFiles = []; | ||
|
|
||
| foreach ($sourceFilePathNames as $sourceFile) { | ||
| foreach ($filePathsIterator as $sourceFile) { | ||
| if (preg_match('(^phar:)i', $sourceFile) === 0) { | ||
| $sourceFile = realpath($sourceFile); | ||
| assert($sourceFile !== false); | ||
|
|
@@ -179,7 +157,7 @@ public function getAllClassNames(): array | |
|
|
||
| require_once $sourceFile; | ||
|
|
||
| $includedFiles[] = $sourceFile; | ||
| $includedFiles[$sourceFile] = true; | ||
| } | ||
|
|
||
| $classes = []; | ||
|
|
@@ -190,7 +168,7 @@ public function getAllClassNames(): array | |
|
|
||
| $sourceFile = $rc->getFileName(); | ||
|
|
||
| if (! in_array($sourceFile, $includedFiles, true) || $this->isTransient($className)) { | ||
| if (! isset($includedFiles[$sourceFile]) || $this->isTransient($className)) { | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -203,14 +181,19 @@ public function getAllClassNames(): array | |
| } | ||
|
|
||
| /** | ||
| * @param iterable<SplFileInfo> $filesIterator | ||
| * @internal | ||
| * | ||
| * @return Generator<int,string> | ||
| * @param iterable<TKey, T> $iterable1 | ||
| * @param iterable<TKey, T> $iterable2 | ||
| * | ||
| * @return iterable<TKey, T> | ||
| * | ||
| * @template TKey | ||
| * @template T | ||
| */ | ||
| private function pathNameIterator(iterable $filesIterator): Generator | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, removing a private method from a trait is a breaking change. It could be used by the class that uses it in an other package. But this code has not been released yet. That's why I thing it's better not introducing |
||
| private function concatIterables(iterable $iterable1, iterable $iterable2): iterable | ||
| { | ||
| foreach ($filesIterator as $file) { | ||
| yield $file->getPathname(); | ||
| } | ||
| yield from $iterable1; | ||
| yield from $iterable2; | ||
| } | ||
| } | ||
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,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Persistence\Mapping\Driver; | ||
|
|
||
| use AppendIterator; | ||
| use Doctrine\Persistence\Mapping\MappingException; | ||
| use FilesystemIterator; | ||
| use Iterator; | ||
| use IteratorAggregate; | ||
| use RecursiveDirectoryIterator; | ||
| use RecursiveIteratorIterator; | ||
| use RegexIterator; | ||
| use SplFileInfo; | ||
|
|
||
| use function is_dir; | ||
| use function preg_quote; | ||
| use function sprintf; | ||
|
|
||
| /** @implements IteratorAggregate<array-key,SplFileInfo> */ | ||
| final class DirectoryFilesIterator implements IteratorAggregate | ||
| { | ||
| public function __construct( | ||
| /** @var list<string> */ | ||
| private readonly array $paths, | ||
| private readonly string $fileExtension = '.php', | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @return Iterator<array-key,SplFileInfo> | ||
| * | ||
| * @throws MappingException | ||
| */ | ||
| public function getIterator(): Iterator | ||
| { | ||
| /** @var AppendIterator<array-key,SplFileInfo,Iterator<array-key,SplFileInfo>> $filesIterator */ | ||
| $filesIterator = new AppendIterator(); | ||
|
|
||
| foreach ($this->paths as $path) { | ||
| if (! is_dir($path)) { | ||
| throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); | ||
| } | ||
|
|
||
| /** @var Iterator<array-key,SplFileInfo> $iterator */ | ||
| $iterator = new RegexIterator( | ||
| new RecursiveIteratorIterator( | ||
| new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), | ||
| RecursiveIteratorIterator::LEAVES_ONLY, | ||
| ), | ||
| sprintf('/%s$/', preg_quote($this->fileExtension, '/')), | ||
| RegexIterator::MATCH, | ||
| ); | ||
|
|
||
| $filesIterator->append($iterator); | ||
| } | ||
|
|
||
| return $filesIterator; | ||
| } | ||
| } |
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,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Doctrine\Persistence\Mapping\Driver; | ||
|
|
||
| use Generator; | ||
| use IteratorAggregate; | ||
| use SplFileInfo; | ||
|
|
||
| /** @implements IteratorAggregate<array-key,string> */ | ||
| final class FilePathNameIterator implements IteratorAggregate | ||
| { | ||
| public function __construct( | ||
| /** @var iterable<SplFileInfo> */ | ||
| private readonly iterable $filesIterator, | ||
| ) { | ||
| } | ||
|
|
||
| /** @return Generator<array-key,string> */ | ||
| public function getIterator(): Generator | ||
| { | ||
| foreach ($this->filesIterator as $key => $file) { | ||
| yield $key => $file->getPathname(); | ||
| } | ||
| } | ||
| } |
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.
The
AppendIteratorwould be perfect to replaceconcatIterablesand avoid this private method to the class that uses the trait.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.
I also considered doing it this way, but the problem is that
ArrayIteratordoesn't accept a genericiterable(norTraversable). Although it works well witharray|ArrayObject, that's the only input type it supports. It won't work with$filePathsbeing someTraversableinstead.