Skip to content

Commit 0540afc

Browse files
committed
Implement file regex search for ColocatedMappingDriver
1 parent 650780b commit 0540afc

File tree

6 files changed

+120
-17
lines changed

6 files changed

+120
-17
lines changed

UPGRADE.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@ awareness about deprecated code.
66
- Use of our low-overhead runtime deprecation API, details:
77
https://github.com/doctrine/deprecations/
88

9+
# Upgrade to 4.1
10+
11+
## Added `ColocatedMappingDriver::$fileRegex`
12+
13+
It is now possible to find mapping files by regex rather than by extension:
14+
15+
Before:
16+
17+
```php
18+
$driver->setFileExtension('.php'); // Deprecated
19+
```
20+
21+
After:
22+
23+
```php
24+
$driver->setFileRegex('/^.+\.php$/');
25+
```
26+
27+
## Deprecated `ColocatedMappingDriver::$fileExtension` in favor of `$fileRegex`
28+
29+
Property `Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver::$fileExtension` and `getFileExtension()`,
30+
`setFileExtension()` methods are deprecated.
31+
You should use `Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver::$fileRegex` and `getFileRegex()`,
32+
`setFileRegex()` instead.
33+
934
# Upgrade to 4.0
1035

1136
## BC Break: Removed `StaticReflectionService`
@@ -89,7 +114,7 @@ Use `LifecycleEventArgs::getObject()` instead.
89114

90115
- `AbstractClassMetadataFactory::getFqcnFromAlias()` is removed.
91116
- `ClassMetadataFactory` methods now require their `$className` argument to be an
92-
actual FQCN.
117+
actual FQCN.
93118

94119
## BC Break: removed `ObjectManager::merge()`
95120

src/Persistence/Mapping/Driver/ColocatedMappingDriver.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ trait ColocatedMappingDriver
4343
*/
4444
protected array $excludePaths = [];
4545

46-
/** The file extension of mapping documents. */
46+
/** The regex used to match mapping files. */
47+
protected string $fileRegex = '/^.+\.php$/i';
48+
49+
/**
50+
* The file extension of mapping documents.
51+
*
52+
* @deprecated Use {@see $fileRegex} instead.
53+
*/
4754
protected string $fileExtension = '.php';
4855

4956
/**
@@ -94,16 +101,38 @@ public function getExcludePaths(): array
94101
return $this->excludePaths;
95102
}
96103

97-
/** Gets the file extension used to look for mapping files under. */
104+
/** Gets the file regex used to look for mapping files under. */
105+
public function getFileRegex(): string
106+
{
107+
return $this->fileRegex;
108+
}
109+
110+
/** Sets the file regex used to look for mapping files with. */
111+
public function setFileRegex(string $fileRegex): void
112+
{
113+
$this->fileRegex = $fileRegex;
114+
$this->fileExtension = '';
115+
}
116+
117+
/**
118+
* Gets the file extension used to look for mapping files under.
119+
*
120+
* @deprecated Use {@see getFileRegex()} instead.
121+
*/
98122
public function getFileExtension(): string
99123
{
100124
return $this->fileExtension;
101125
}
102126

103-
/** Sets the file extension used to look for mapping files under. */
127+
/**
128+
* Sets the file extension used to look for mapping files under.
129+
*
130+
* @deprecated Use {@see setFileRegex()} instead.
131+
*/
104132
public function setFileExtension(string $fileExtension): void
105133
{
106134
$this->fileExtension = $fileExtension;
135+
$this->fileRegex = '/^.+' . preg_quote($fileExtension) . '$/i';
107136
}
108137

109138
/**
@@ -145,7 +174,7 @@ public function getAllClassNames(): array
145174
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
146175
RecursiveIteratorIterator::LEAVES_ONLY,
147176
),
148-
'/^.+' . preg_quote($this->fileExtension) . '$/i',
177+
$this->fileRegex,
149178
RecursiveRegexIterator::GET_MATCH,
150179
);
151180

tests/Persistence/Mapping/ColocatedMappingDriverTest.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
namespace Doctrine\Tests\Persistence\Mapping;
66

7+
use Doctrine\Entity;
8+
use Doctrine\EntityFixture;
79
use Doctrine\Persistence\Mapping\ClassMetadata;
810
use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver;
911
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
1012
use Doctrine\TestClass;
1113
use Generator;
1214
use PHPUnit\Framework\TestCase;
1315

14-
use function array_values;
16+
use function sort;
1517

1618
class ColocatedMappingDriverTest extends TestCase
1719
{
@@ -44,6 +46,18 @@ public function testAddGetExcludePaths(): void
4446
], $driver->getExcludePaths());
4547
}
4648

49+
public function testGetSetFileRegex(): void
50+
{
51+
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
52+
self::assertSame('/^.+\.php$/i', $driver->getFileRegex());
53+
54+
$driver->setFileRegex('/^(?!.*Test\.php$).*\.php$/');
55+
56+
self::assertSame('/^(?!.*Test\.php$).*\.php$/', $driver->getFileRegex());
57+
self::assertSame('', $driver->getFileExtension());
58+
}
59+
60+
/** @deprecated */
4761
public function testGetSetFileExtension(): void
4862
{
4963
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
@@ -52,6 +66,7 @@ public function testGetSetFileExtension(): void
5266
$driver->setFileExtension('.php1');
5367

5468
self::assertSame('.php1', $driver->getFileExtension());
69+
self::assertSame('/^.+\.php1$/i', $driver->getFileRegex());
5570
}
5671

5772
/** @dataProvider pathProvider */
@@ -61,7 +76,18 @@ public function testGetAllClassNames(string $path): void
6176

6277
$classes = $driver->getAllClassNames();
6378

64-
self::assertSame([TestClass::class], $classes);
79+
sort($classes);
80+
self::assertSame([Entity::class, EntityFixture::class], $classes);
81+
}
82+
83+
public function testGetAllClassNamesWithRegex(): void
84+
{
85+
$noFixturesRegex = '/^(?!.*Fixture\.php$).*\.php$/';
86+
$driver = $this->createDriver(__DIR__ . '/_files/colocated', $noFixturesRegex);
87+
88+
$classes = $driver->getAllClassNames();
89+
90+
self::assertSame([Entity::class], $classes);
6591
}
6692

6793
/** @return Generator<string, array{string}> */
@@ -71,20 +97,29 @@ public static function pathProvider(): Generator
7197
yield 'winding path' => [__DIR__ . '/../Mapping/_files/colocated'];
7298
}
7399

74-
private function createDriver(string $path): MyDriver
100+
private function createDriver(string $path, string|null $fileRegex = null): MyDriver
75101
{
76-
return new MyDriver($path);
102+
return new MyDriver([$path], $fileRegex);
77103
}
78104
}
79105

80106
final class MyDriver implements MappingDriver
81107
{
82108
use ColocatedMappingDriver;
83109

84-
/** @param string ...$paths One or multiple paths where mapping classes can be found. */
85-
public function __construct(string ...$paths)
110+
/**
111+
* @param non-empty-list<string> $paths One or multiple paths where mapping classes can be found.
112+
* @param string|null $fileRegex The regex used to look for mapping files with.
113+
*/
114+
public function __construct(array $paths, string|null $fileRegex = null)
86115
{
87-
$this->addPaths(array_values($paths));
116+
$this->addPaths($paths);
117+
118+
if ($fileRegex === null) {
119+
return;
120+
}
121+
122+
$this->setFileRegex($fileRegex);
88123
}
89124

90125
/**
@@ -96,6 +131,6 @@ public function loadMetadataForClass($className, ClassMetadata $metadata): void
96131

97132
public function isTransient(string $className): bool
98133
{
99-
return $className !== TestClass::class;
134+
return $className === TestClass::class;
100135
}
101136
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine;
6+
7+
class Entity
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine;
6+
7+
class EntityFixture
8+
{
9+
}

tests/Persistence/Mapping/_files/colocated/TestClass.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,3 @@
77
class TestClass
88
{
99
}
10-
11-
class Entity
12-
{
13-
}

0 commit comments

Comments
 (0)