|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\DoctrineDbServiceProvider\Driver; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface; |
|
8
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
|
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
10
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
|
11
|
|
|
|
|
12
|
|
|
class ClassMapDriver implements MappingDriver |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array<string, string> |
|
16
|
|
|
*/ |
|
17
|
|
|
private $classMap; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param array<string, string> $classMap |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public function __construct(array $classMap) |
|
23
|
|
|
{ |
|
24
|
6 |
|
$this->classMap = $classMap; |
|
25
|
6 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $className |
|
29
|
|
|
* |
|
30
|
|
|
* @throws MappingException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function loadMetadataForClass($className, ClassMetadataInterface $metadata): void |
|
33
|
4 |
|
{ |
|
34
|
|
|
if (false === $metadata instanceof ClassMetadata) { |
|
35
|
4 |
|
throw new MappingException( |
|
36
|
1 |
|
sprintf('Metadata is of class "%s" instead of "%s"', get_class($metadata), ClassMetadata::class) |
|
37
|
1 |
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (false === isset($this->classMap[$className])) { |
|
41
|
3 |
|
throw new MappingException( |
|
42
|
1 |
|
sprintf('No configured mapping for document "%s"', $className) |
|
43
|
1 |
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$mappingClassName = $this->classMap[$className]; |
|
47
|
2 |
|
|
|
48
|
|
|
if (false === ($mapping = new $mappingClassName()) instanceof ClassMapMappingInterface) { |
|
49
|
2 |
|
throw new MappingException( |
|
50
|
1 |
|
sprintf('Class "%s" does not implement the "%s"', $mappingClassName, ClassMapMappingInterface::class) |
|
51
|
1 |
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$mapping->configureMapping($metadata); |
|
55
|
1 |
|
} |
|
56
|
1 |
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array<string> |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getAllClassNames(): array |
|
61
|
1 |
|
{ |
|
62
|
|
|
return array_keys($this->classMap); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $className |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isTransient($className): bool |
|
69
|
|
|
{ |
|
70
|
|
|
if (isset($this->classMap[$className])) { |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
1 |
|
|
|
74
|
1 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|