Skip to content

Commit 06a5956

Browse files
authored
Merge pull request doctrine#448 from HypeMC/fix-namespace-matching
Fix `MappingDriverChain` namespace matching
2 parents 8e18bba + 5b75f99 commit 06a5956

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Persistence/Mapping/Driver/MappingDriverChain.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Persistence\Mapping\MappingException;
99

1010
use function array_keys;
11+
use function rtrim;
1112
use function spl_object_hash;
1213
use function strpos;
1314

@@ -73,7 +74,7 @@ public function getDrivers()
7374
public function loadMetadataForClass(string $className, ClassMetadata $metadata)
7475
{
7576
foreach ($this->drivers as $namespace => $driver) {
76-
if (strpos($className, $namespace) === 0) {
77+
if ($this->isInNamespace($className, $namespace)) {
7778
$driver->loadMetadataForClass($className, $metadata);
7879

7980
return;
@@ -105,7 +106,7 @@ public function getAllClassNames()
105106
}
106107

107108
foreach ($driverClasses[$oid] as $className) {
108-
if (strpos($className, $namespace) !== 0) {
109+
if (! $this->isInNamespace($className, $namespace)) {
109110
continue;
110111
}
111112

@@ -128,7 +129,7 @@ public function getAllClassNames()
128129
public function isTransient(string $className)
129130
{
130131
foreach ($this->drivers as $namespace => $driver) {
131-
if (strpos($className, $namespace) === 0) {
132+
if ($this->isInNamespace($className, $namespace)) {
132133
return $driver->isTransient($className);
133134
}
134135
}
@@ -139,4 +140,11 @@ public function isTransient(string $className)
139140

140141
return true;
141142
}
143+
144+
private function isInNamespace(string $className, string $namespace): bool
145+
{
146+
$namespace = rtrim($namespace, '\\') . '\\';
147+
148+
return strpos($className, $namespace) === 0;
149+
}
142150
}

tests/Persistence/Mapping/DriverChainTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
class DriverChainTest extends DoctrineTestCase
1717
{
18-
public function testDelegateToMatchingNamespaceDriver(): void
18+
/**
19+
* @testWith ["Doctrine\\Tests\\Models\\Company"]
20+
* ["Doctrine\\Tests\\Persistence\\Map"]
21+
*/
22+
public function testDelegateToMatchingNamespaceDriver(string $namespace): void
1923
{
2024
$className = DriverChainEntity::class;
2125
$classMetadata = $this->createMock(ClassMetadata::class);
@@ -37,7 +41,7 @@ public function testDelegateToMatchingNamespaceDriver(): void
3741
->with(self::equalTo($className))
3842
->willReturn(true);
3943

40-
$chain->addDriver($driver1, 'Doctrine\Tests\Models\Company');
44+
$chain->addDriver($driver1, $namespace);
4145
$chain->addDriver($driver2, 'Doctrine\Tests\Persistence\Mapping');
4246

4347
$chain->loadMetadataForClass($className, $classMetadata);

0 commit comments

Comments
 (0)