|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\EntityManager\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
|
6
|
|
|
use Doctrine\Common\Persistence\Mapping\ReflectionService; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
|
9
|
|
|
|
|
10
|
|
|
class ClassMetadataFactoryWithEntityFactories extends ClassMetadataFactory implements EntityFactoryAware |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var EntityFactoryInterface[] */ |
|
13
|
|
|
public static $entityFactories = []; |
|
14
|
|
|
/** @var GenericFactoryInterface|null */ |
|
15
|
|
|
public static $genericFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** @var EntityManagerInterface */ |
|
18
|
|
|
private $entityManager; |
|
19
|
|
|
|
|
20
|
|
|
public function setEntityManager(EntityManagerInterface $entityManager) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::setEntityManager($entityManager); |
|
23
|
|
|
$this->entityManager = $entityManager; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function newClassMetadataInstance($className): ClassMetadata |
|
27
|
|
|
{ |
|
28
|
|
|
return new ClassMetadataWithEntityFactories( |
|
29
|
|
|
$className, |
|
30
|
|
|
$this->entityManager->getConfiguration()->getNamingStrategy(), |
|
31
|
|
|
$this->getEntityFactories() |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function addEntityFactory(string $name, EntityFactoryInterface $entityFactory): void |
|
36
|
|
|
{ |
|
37
|
|
|
self::$entityFactories[$name] = $entityFactory; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function addGenericFactory(GenericFactoryInterface $genericFactory): void |
|
41
|
|
|
{ |
|
42
|
|
|
self::$genericFactory = $genericFactory; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return EntityFactoryInterface[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getEntityFactories(): array |
|
49
|
|
|
{ |
|
50
|
|
|
return self::$entityFactories; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function wakeupReflection(ClassMetadata $class, ReflectionService $reflectionService): void |
|
54
|
|
|
{ |
|
55
|
|
|
parent::wakeupReflection($class, $reflectionService); |
|
56
|
|
|
if ($class instanceof ClassMetadataWithEntityFactories) { |
|
57
|
|
|
$class->setFactories(self::$entityFactories, self::$genericFactory); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|