Skip to content

Commit 8b501b9

Browse files
committed
Refactor: extract FilePathNameIterator
This change makes it easy for the depending libraries to use iterable of `SplFileInfo`, adapted to the format of `ColocatedMappingDriver` file paths. For example, one could provide an instance of Symfony Finder, adapted with `FilePathNameIterator` without having to reinvent it from scratch.
1 parent a6b38ce commit 8b501b9

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/Persistence/Mapping/Driver/ColocatedMappingDriver.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use AppendIterator;
88
use Doctrine\Persistence\Mapping\MappingException;
99
use FilesystemIterator;
10-
use Generator;
1110
use Iterator;
1211
use RecursiveDirectoryIterator;
1312
use RecursiveIteratorIterator;
@@ -156,7 +155,8 @@ public function getAllClassNames(): array
156155
$filesIterator->append($iterator);
157156
}
158157

159-
$sourceFilePathNames = $this->pathNameIterator($filesIterator);
158+
/** @var iterable<string> $sourceFilePathNames */
159+
$sourceFilePathNames = new FilePathNameIterator($filesIterator);
160160
/** @var array<string,true> $includedFiles */
161161
$includedFiles = [];
162162

@@ -201,16 +201,4 @@ public function getAllClassNames(): array
201201

202202
return $classes;
203203
}
204-
205-
/**
206-
* @param iterable<SplFileInfo> $filesIterator
207-
*
208-
* @return Generator<int,string>
209-
*/
210-
private function pathNameIterator(iterable $filesIterator): Generator
211-
{
212-
foreach ($filesIterator as $file) {
213-
yield $file->getPathname();
214-
}
215-
}
216204
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Persistence\Mapping\Driver;
6+
7+
use Generator;
8+
use IteratorAggregate;
9+
use SplFileInfo;
10+
11+
/** @implements IteratorAggregate<int,string> */
12+
final class FilePathNameIterator implements IteratorAggregate
13+
{
14+
public function __construct(
15+
/** @var iterable<SplFileInfo> */
16+
private readonly iterable $filesIterator,
17+
) {
18+
}
19+
20+
/** @return Generator<int,string> */
21+
public function getIterator(): Generator
22+
{
23+
foreach ($this->filesIterator as $file) {
24+
yield $file->getPathname();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)