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