1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Traits; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata as DoctrineClassMetaData; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\UsesPHPMetaDataInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
10
|
|
|
|
11
|
|
|
trait UsesPHPMetaDataTrait |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var DoctrineStaticMeta |
16
|
|
|
*/ |
17
|
|
|
private static $doctrineStaticMeta; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Loads the class and property meta data in the class |
21
|
|
|
* |
22
|
|
|
* This is the method called by Doctrine to load the meta data |
23
|
|
|
* |
24
|
|
|
* @param DoctrineClassMetaData $metaData |
25
|
|
|
* |
26
|
|
|
* @throws DoctrineStaticMetaException |
27
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
28
|
|
|
*/ |
29
|
|
|
public static function loadMetadata(DoctrineClassMetaData $metaData): void |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
self::getDoctrineStaticMeta()->setMetaData($metaData)->buildMetaData(); |
33
|
|
|
} catch (\Exception $e) { |
34
|
|
|
throw new DoctrineStaticMetaException( |
35
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
36
|
|
|
$e->getCode(), |
37
|
|
|
$e |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return DoctrineStaticMeta |
44
|
|
|
* @throws \ReflectionException |
45
|
|
|
*/ |
46
|
|
|
public static function getDoctrineStaticMeta(): DoctrineStaticMeta |
47
|
|
|
{ |
48
|
|
|
if (null === self::$doctrineStaticMeta) { |
49
|
|
|
self::$doctrineStaticMeta = new DoctrineStaticMeta(self::class); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return self::$doctrineStaticMeta; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* In the class itself, we need to specify the repository class name |
57
|
|
|
* |
58
|
|
|
* @param ClassMetadataBuilder $builder |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
abstract protected static function setCustomRepositoryClass(ClassMetadataBuilder $builder); |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Find and run all init methods |
67
|
|
|
* - defined in relationship traits and generally to init ArrayCollection properties |
68
|
|
|
* |
69
|
|
|
* @throws \ReflectionException |
70
|
|
|
*/ |
71
|
|
|
protected function runInitMethods(): void |
72
|
|
|
{ |
73
|
|
|
$reflectionClass = self::getDoctrineStaticMeta()->getReflectionClass(); |
74
|
|
|
$methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PRIVATE); |
75
|
|
|
foreach ($methods as $method) { |
76
|
|
|
if ($method instanceof \ReflectionMethod) { |
77
|
|
|
$method = $method->getName(); |
78
|
|
|
} |
79
|
|
|
if (\ts\stringContains($method, UsesPHPMetaDataInterface::METHOD_PREFIX_INIT) |
80
|
|
|
&& \ts\stringStartsWith($method, UsesPHPMetaDataInterface::METHOD_PREFIX_INIT) |
81
|
|
|
) { |
82
|
|
|
$this->$method(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|