Skip to content

Commit 0466814

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

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

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 under. */
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public function testAddGetExcludePaths(): void
4444
], $driver->getExcludePaths());
4545
}
4646

47+
public function testSetFileRegex(): void
48+
{
49+
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
50+
self::assertSame('/^.+\.php$/i', $driver->getFileRegex());
51+
52+
$driver->setFileRegex('^(?!.*(Test|Fixture)\.php$).*\.php$');
53+
54+
self::assertSame('^(?!.*(Test|Fixture)\.php$).*\.php$', $driver->getFileRegex());
55+
self::assertSame('', $driver->getFileExtension());
56+
}
57+
58+
/** @deprecated */
4759
public function testGetSetFileExtension(): void
4860
{
4961
$driver = $this->createDriver(__DIR__ . '/_files/colocated');
@@ -52,6 +64,7 @@ public function testGetSetFileExtension(): void
5264
$driver->setFileExtension('.php1');
5365

5466
self::assertSame('.php1', $driver->getFileExtension());
67+
self::assertSame('/^.+\.php1$/i', $driver->getFileRegex());
5568
}
5669

5770
/** @dataProvider pathProvider */

0 commit comments

Comments
 (0)