|
5 | 5 | namespace Doctrine\Persistence\Mapping\Driver;
|
6 | 6 |
|
7 | 7 | use Doctrine\Persistence\Mapping\MappingException;
|
8 |
| -use ReflectionClass; |
9 | 8 |
|
| 9 | +use function array_filter; |
10 | 10 | use function array_merge;
|
11 | 11 | use function array_unique;
|
12 |
| -use function assert; |
13 |
| -use function get_declared_classes; |
14 |
| -use function preg_match; |
15 |
| -use function realpath; |
16 |
| -use function str_contains; |
17 |
| -use function str_replace; |
18 | 12 |
|
19 | 13 | /**
|
20 | 14 | * The ColocatedMappingDriver reads the mapping metadata located near the code.
|
21 | 15 | */
|
22 | 16 | trait ColocatedMappingDriver
|
23 | 17 | {
|
24 |
| - /** @var iterable<array-key,string> */ |
25 |
| - private iterable $filePaths; |
| 18 | + private ClassLocator $classLocator; |
26 | 19 |
|
27 | 20 | /**
|
28 | 21 | * The directory paths where to look for mapping files.
|
@@ -123,75 +116,24 @@ public function getAllClassNames(): array
|
123 | 116 | return $this->classNames;
|
124 | 117 | }
|
125 | 118 |
|
126 |
| - if ($this->paths === [] && ! isset($this->filePaths)) { |
127 |
| - throw MappingException::pathRequiredForDriver(static::class); |
128 |
| - } |
129 |
| - |
130 |
| - $dirFilesIterator = new DirectoryFilesIterator($this->paths, $this->fileExtension); |
131 |
| - |
132 |
| - /** @var iterable<string> $filePathsIterator */ |
133 |
| - $filePathsIterator = $this->concatIterables( |
134 |
| - $this->filePaths ?? [], |
135 |
| - new FilePathNameIterator($dirFilesIterator), |
136 |
| - ); |
137 |
| - |
138 |
| - /** @var array<string,true> $includedFiles */ |
139 |
| - $includedFiles = []; |
140 |
| - |
141 |
| - foreach ($filePathsIterator as $sourceFile) { |
142 |
| - if (preg_match('(^phar:)i', $sourceFile) === 0) { |
143 |
| - $sourceFile = realpath($sourceFile); |
144 |
| - assert($sourceFile !== false); |
145 |
| - } |
146 |
| - |
147 |
| - foreach ($this->excludePaths as $excludePath) { |
148 |
| - $realExcludePath = realpath($excludePath); |
149 |
| - assert($realExcludePath !== false); |
150 |
| - $exclude = str_replace('\\', '/', $realExcludePath); |
151 |
| - $current = str_replace('\\', '/', $sourceFile); |
152 |
| - |
153 |
| - if (str_contains($current, $exclude)) { |
154 |
| - continue 2; |
155 |
| - } |
156 |
| - } |
157 |
| - |
158 |
| - require_once $sourceFile; |
159 |
| - |
160 |
| - $includedFiles[$sourceFile] = true; |
161 |
| - } |
| 119 | + if ($this->paths !== []) { |
| 120 | + $classNames = FileClassLocator::createFromDirectories($this->paths, $this->excludePaths, $this->fileExtension)->getClassNames(); |
162 | 121 |
|
163 |
| - $classes = []; |
164 |
| - $declared = get_declared_classes(); |
165 |
| - |
166 |
| - foreach ($declared as $className) { |
167 |
| - $rc = new ReflectionClass($className); |
168 |
| - |
169 |
| - $sourceFile = $rc->getFileName(); |
170 |
| - |
171 |
| - if (! isset($includedFiles[$sourceFile]) || $this->isTransient($className)) { |
172 |
| - continue; |
| 122 | + if (isset($this->classLocator)) { |
| 123 | + $classNames = array_unique([ |
| 124 | + ...$classNames, |
| 125 | + ...$this->classLocator->getClassNames(), |
| 126 | + ]); |
173 | 127 | }
|
174 |
| - |
175 |
| - $classes[] = $className; |
| 128 | + } elseif (isset($this->classLocator)) { |
| 129 | + $classNames = $this->classLocator->getClassNames(); |
| 130 | + } else { |
| 131 | + throw MappingException::pathRequiredForDriver(static::class); |
176 | 132 | }
|
177 | 133 |
|
178 |
| - $this->classNames = $classes; |
179 |
| - |
180 |
| - return $classes; |
181 |
| - } |
182 |
| - |
183 |
| - /** |
184 |
| - * @param iterable<TKey, T> $iterable1 |
185 |
| - * @param iterable<TKey, T> $iterable2 |
186 |
| - * |
187 |
| - * @return iterable<TKey, T> |
188 |
| - * |
189 |
| - * @template TKey |
190 |
| - * @template T |
191 |
| - */ |
192 |
| - private function concatIterables(iterable $iterable1, iterable $iterable2): iterable |
193 |
| - { |
194 |
| - yield from $iterable1; |
195 |
| - yield from $iterable2; |
| 134 | + return $this->classNames = array_filter( |
| 135 | + $classNames, |
| 136 | + fn (string $className): bool => ! $this->isTransient($className), |
| 137 | + ); |
196 | 138 | }
|
197 | 139 | }
|
0 commit comments