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[] |
16
|
|
|
*/ |
17
|
|
|
private $classMap; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param array|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
|
|
|
* @param ClassMetadataInterface $metadata |
30
|
|
|
* |
31
|
|
|
* @throws MappingException |
32
|
|
|
*/ |
33
|
4 |
|
public function loadMetadataForClass($className, ClassMetadataInterface $metadata) |
34
|
|
|
{ |
35
|
4 |
|
if (false === $metadata instanceof ClassMetadata) { |
36
|
1 |
|
throw new MappingException( |
37
|
1 |
|
sprintf('Metadata is of class "%s" instead of "%s"', get_class($metadata), ClassMetadata::class) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
if (false === isset($this->classMap[$className])) { |
42
|
1 |
|
throw new MappingException( |
43
|
1 |
|
sprintf('No configured mapping for document "%s"', $className) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$mappingClassName = $this->classMap[$className]; |
48
|
|
|
|
49
|
2 |
|
if (false === ($mapping = new $mappingClassName()) instanceof ClassMapMappingInterface) { |
50
|
1 |
|
throw new MappingException( |
51
|
1 |
|
sprintf('Class "%s" does not implement the "%s"', $mappingClassName, ClassMapMappingInterface::class) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$mapping->configureMapping($metadata); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
1 |
|
public function getAllClassNames(): array |
62
|
|
|
{ |
63
|
1 |
|
return array_keys($this->classMap); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $className |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
1 |
|
public function isTransient($className): bool |
72
|
|
|
{ |
73
|
1 |
|
if (isset($this->classMap[$className])) { |
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|