1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\EventListener\Doctrine; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
17
|
|
|
use Doctrine\ORM\Mapping\NamingStrategy; |
18
|
|
|
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs; |
19
|
|
|
use Silverback\ApiComponentBundle\Publishable\PublishableHelper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Vincent Chalamon <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class PublishableListener |
25
|
|
|
{ |
26
|
|
|
private PublishableHelper $publishableHelper; |
27
|
|
|
|
28
|
|
|
public function __construct(PublishableHelper $publishableHelper) |
29
|
|
|
{ |
30
|
|
|
$this->publishableHelper = $publishableHelper; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void |
34
|
|
|
{ |
35
|
|
|
/** @var ClassMetadataInfo $metadata */ |
36
|
|
|
$metadata = $eventArgs->getClassMetadata(); |
37
|
|
|
if (!$this->publishableHelper->isPublishable($metadata->getName())) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$configuration = $this->publishableHelper->getConfiguration($metadata->getName()); |
42
|
|
|
/** @var NamingStrategy $namingStrategy */ |
43
|
|
|
$namingStrategy = $eventArgs |
44
|
|
|
->getEntityManager() |
|
|
|
|
45
|
|
|
->getConfiguration() |
46
|
|
|
->getNamingStrategy(); |
47
|
|
|
|
48
|
|
|
if (!$metadata->hasField($configuration->fieldName)) { |
49
|
|
|
$metadata->mapField([ |
50
|
|
|
'fieldName' => $configuration->fieldName, |
51
|
|
|
'type' => 'date', |
52
|
|
|
'nullable' => true, |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$metadata->hasAssociation($configuration->associationName)) { |
57
|
|
|
$metadata->mapOneToOne([ |
58
|
|
|
'fieldName' => $configuration->associationName, |
59
|
|
|
'targetEntity' => $metadata->getName(), |
60
|
|
|
'joinColumns' => [ |
61
|
|
|
[ |
62
|
|
|
'name' => $namingStrategy->joinKeyColumnName($metadata->getName()), |
63
|
|
|
'referencedColumnName' => $namingStrategy->referenceColumnName(), |
64
|
|
|
'onDelete' => 'SET NULL', |
65
|
|
|
], |
66
|
|
|
], |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|