Skip to content

Commit 6ea264a

Browse files
committed
Feature: add support for ClassLocator from doctrine/persistence 4.1
The changes integrate doctrine/persistence#433 ClassLocator allows clients to pass any iterable of classes they might want.
1 parent d956cd8 commit 6ea264a

File tree

7 files changed

+54
-18
lines changed

7 files changed

+54
-18
lines changed

.phpstan/phpstan-baseline.neon

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,24 @@ parameters:
4040
message: "#^Call to an undefined method Doctrine\\\\ODM\\\\PHPCR\\\\Query\\\\Builder\\\\AbstractNode\\:\\:setConverter\\(\\)\\.$#"
4141
count: 1
4242
path: ../tests/Doctrine/Tests/ODM/PHPCR/Query/Builder/QueryBuilderTest.php
43+
44+
-
45+
message: '#^Access to an undefined property Doctrine\\ODM\\PHPCR\\Mapping\\Driver\\AttributeDriver\:\:\$classLocator\.$#'
46+
identifier: property.notFound
47+
count: 1
48+
path: ../lib/Doctrine/ODM/PHPCR/Mapping/Driver/AttributeDriver.php
49+
reportUnmatched: false
50+
51+
-
52+
message: '#^Class Doctrine\\Persistence\\Mapping\\Driver\\ClassLocator not found\.$#'
53+
identifier: class.notFound
54+
count: 1
55+
path: ../lib/Doctrine/ODM/PHPCR/Mapping/Driver/AttributeDriver.php
56+
reportUnmatched: false
57+
58+
-
59+
message: '#^Parameter \$paths of method Doctrine\\ODM\\PHPCR\\Mapping\\Driver\\AttributeDriver\:\:__construct\(\) has invalid type Doctrine\\Persistence\\Mapping\\Driver\\ClassLocator\.$#'
60+
identifier: class.notFound
61+
count: 2
62+
path: ../lib/Doctrine/ODM/PHPCR/Mapping/Driver/AttributeDriver.php
63+
reportUnmatched: false

lib/Doctrine/ODM/PHPCR/Decorator/DocumentManagerDecorator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ODM\PHPCR\ChildrenCollection;
88
use Doctrine\ODM\PHPCR\Configuration;
99
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
10+
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata as PhpcrClassMetadata;
1011
use Doctrine\ODM\PHPCR\Proxy\ProxyFactory;
1112
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
1213
use Doctrine\ODM\PHPCR\Query\Query;
@@ -38,6 +39,12 @@ public function __construct(DocumentManagerInterface $wrapped)
3839
$this->wrapped = $wrapped;
3940
}
4041

42+
/** @noinspection SenselessMethodDuplicationInspection - return type is made more specific to match {@see DocumentManagerInterface::getClassMetadata()} */
43+
public function getClassMetadata(string $className): PhpcrClassMetadata
44+
{
45+
return $this->wrapped->getClassMetadata($className);
46+
}
47+
4148
public function setTranslationStrategy(string $key, TranslationStrategyInterface $strategy): void
4249
{
4350
$this->wrapped->setTranslationStrategy($key, $strategy);

lib/Doctrine/ODM/PHPCR/DocumentManagerInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@
4545
interface DocumentManagerInterface extends ObjectManager
4646
{
4747
/**
48-
* @{@inheritDoc}
49-
*
50-
* Overwritten to tighten return type. We can't tighten the return type declaration because of Doctrine\Persistence\ObjectManagerDecorator.
48+
* {@inheritdoc}
5149
*
52-
* @return PhpcrClassMetadata
50+
* Overwritten to tighten the return type.
5351
*/
54-
public function getClassMetadata(string $className);
52+
public function getClassMetadata(string $className): PhpcrClassMetadata;
5553

5654
/**
5755
* Add or replace a translation strategy.

lib/Doctrine/ODM/PHPCR/Mapping/ClassMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ public function getAssociationTargetClass($fieldName): ?string
13171317
return $this->mappings[$fieldName]['targetDocument'];
13181318
}
13191319

1320-
public function getAssociationMappedByTargetField($assocName)
1320+
public function getAssociationMappedByTargetField($assocName): string
13211321
{
13221322
throw new BadMethodCallException(sprintf(
13231323
'%s not yet implemented in "%s"',
@@ -1326,7 +1326,7 @@ public function getAssociationMappedByTargetField($assocName)
13261326
));
13271327
}
13281328

1329-
public function isAssociationInverseSide($assocName)
1329+
public function isAssociationInverseSide($assocName): bool
13301330
{
13311331
throw new BadMethodCallException(sprintf(
13321332
'%s not yet implemented in "%s"',

lib/Doctrine/ODM/PHPCR/Mapping/Driver/AttributeDriver.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
2828
use Doctrine\ODM\PHPCR\Mapping\MappingException;
2929
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
30+
use Doctrine\Persistence\Mapping\Driver\ClassLocator;
3031
use Doctrine\Persistence\Mapping\Driver\ColocatedMappingDriver;
3132
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
3233

@@ -55,15 +56,20 @@ class AttributeDriver implements MappingDriver
5556
private AttributeReader $reader;
5657

5758
/**
58-
* @param array<string> $paths
59+
* @param array<string>|ClassLocator $paths directory paths or class locator
5960
*/
60-
public function __construct(array $paths)
61+
public function __construct(array|ClassLocator $paths)
6162
{
6263
$this->reader = new AttributeReader();
63-
$this->addPaths($paths);
64+
65+
if ($paths instanceof ClassLocator) {
66+
$this->classLocator = $paths;
67+
} else {
68+
$this->addPaths($paths);
69+
}
6470
}
6571

66-
public function isTransient($className)
72+
public function isTransient($className): bool
6773
{
6874
$classAttributes = $this->reader->getClassAttributes(new \ReflectionClass($className));
6975

lib/Doctrine/ODM/PHPCR/Mapping/Driver/YamlDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private function addMappingFromReference(ClassMetadata $class, string $fieldName
262262
}
263263
}
264264

265-
protected function loadMappingFile($file)
265+
protected function loadMappingFile($file): array
266266
{
267267
if (!is_file($file)) {
268268
throw new InvalidArgumentException(sprintf('File "%s" not found', $file));

tests/Doctrine/Tests/ODM/PHPCR/Mapping/AttributeDriverTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
namespace Doctrine\Tests\ODM\PHPCR\Mapping;
44

55
use Doctrine\ODM\PHPCR\Mapping\Driver\AttributeDriver;
6+
use Doctrine\Persistence\Mapping\Driver\FileClassLocator;
67
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
78

89
/**
910
* @group mapping
1011
*/
1112
class AttributeDriverTest extends AbstractMappingDriverTest
1213
{
13-
protected function loadDriver(): AttributeDriver
14+
/** @param list<string> $paths */
15+
protected function loadDriver(array $paths = []): AttributeDriver
1416
{
15-
return new AttributeDriver([]);
17+
// Available in Doctrine Persistence 4.1+
18+
if (class_exists(FileClassLocator::class)) {
19+
$paths = FileClassLocator::createFromDirectories($paths);
20+
}
21+
22+
return new AttributeDriver($paths);
1623
}
1724

1825
protected function loadDriverForTestMappingDocuments(): MappingDriver
1926
{
20-
$attributeDriver = $this->loadDriver();
21-
$attributeDriver->addPaths([__DIR__.'/Model']);
22-
23-
return $attributeDriver;
27+
return $this->loadDriver([__DIR__.'/Model']);
2428
}
2529

2630
/**

0 commit comments

Comments
 (0)