1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\EventListener\Doctrine; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
18
|
|
|
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs; |
19
|
|
|
use Silverback\ApiComponentsBundle\AnnotationReader\PublishableAnnotationReader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class PublishableListener |
25
|
|
|
{ |
26
|
|
|
private PublishableAnnotationReader $publishableHelper; |
27
|
|
|
|
28
|
9 |
|
public function __construct(PublishableAnnotationReader $publishableHelper) |
29
|
|
|
{ |
30
|
9 |
|
$this->publishableHelper = $publishableHelper; |
31
|
9 |
|
} |
32
|
|
|
|
33
|
9 |
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
34
|
|
|
{ |
35
|
|
|
/** @var ClassMetadataInfo $metadata */ |
36
|
9 |
|
$metadata = $eventArgs->getClassMetadata(); |
37
|
9 |
|
if (!$this->publishableHelper->isConfigured($metadata->getName())) { |
38
|
9 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
9 |
|
$configuration = $this->publishableHelper->getConfiguration($metadata->getName()); |
42
|
|
|
|
43
|
9 |
|
$em = $eventArgs->getObjectManager(); |
44
|
9 |
|
if (!$em instanceof EntityManagerInterface) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
9 |
|
$namingStrategy = $em->getConfiguration()->getNamingStrategy(); |
48
|
|
|
|
49
|
9 |
|
if (!$metadata->hasField($configuration->fieldName)) { |
50
|
9 |
|
$metadata->mapField([ |
51
|
9 |
|
'fieldName' => $configuration->fieldName, |
52
|
9 |
|
'type' => 'datetime', |
53
|
|
|
'nullable' => true, |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
9 |
|
if (!$metadata->hasAssociation($configuration->associationName)) { |
58
|
9 |
|
$metadata->mapOneToOne([ |
59
|
9 |
|
'fieldName' => $configuration->associationName, |
60
|
9 |
|
'targetEntity' => $metadata->getName(), |
61
|
|
|
'joinColumns' => [ |
62
|
|
|
[ |
63
|
9 |
|
'name' => $namingStrategy->joinKeyColumnName($metadata->getName()), |
64
|
9 |
|
'referencedColumnName' => $namingStrategy->referenceColumnName(), |
65
|
9 |
|
'onDelete' => 'SET NULL', |
66
|
|
|
'nullable' => true, |
67
|
|
|
], |
68
|
|
|
], |
69
|
9 |
|
'inversedBy' => $configuration->reverseAssociationName, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
|
73
|
9 |
|
if (!$metadata->hasAssociation($configuration->reverseAssociationName)) { |
74
|
9 |
|
$metadata->mapOneToOne([ |
75
|
9 |
|
'fieldName' => $configuration->reverseAssociationName, |
76
|
9 |
|
'targetEntity' => $metadata->getName(), |
77
|
9 |
|
'mappedBy' => $configuration->associationName, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
9 |
|
} |
81
|
|
|
} |
82
|
|
|
|