|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
7
|
|
|
use gossi\codegen\model\PhpClass; |
|
8
|
|
|
use gossi\codegen\model\PhpInterface; |
|
9
|
|
|
use gossi\codegen\model\PhpTrait; |
|
10
|
|
|
|
|
11
|
|
|
class EntityFieldSetter |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var CodeHelper |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $codeHelper; |
|
17
|
|
|
|
|
18
|
32 |
|
public function __construct(CodeHelper $codeHelper) |
|
19
|
|
|
{ |
|
20
|
32 |
|
$this->codeHelper = $codeHelper; |
|
21
|
32 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $fieldFqn |
|
25
|
|
|
* @param string $entityFqn |
|
26
|
|
|
* |
|
27
|
|
|
* @throws DoctrineStaticMetaException |
|
28
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
29
|
|
|
*/ |
|
30
|
52 |
|
public function setEntityHasField(string $entityFqn, string $fieldFqn): void |
|
31
|
|
|
{ |
|
32
|
|
|
try { |
|
33
|
52 |
|
$entityReflection = new \ReflectionClass($entityFqn); |
|
34
|
52 |
|
$entity = PhpClass::fromFile($entityReflection->getFileName()); |
|
35
|
52 |
|
$fieldReflection = new \ReflectionClass($fieldFqn); |
|
36
|
52 |
|
$field = PhpTrait::fromFile($fieldReflection->getFileName()); |
|
37
|
52 |
|
$fieldInterfaceFqn = \str_replace( |
|
38
|
52 |
|
['Traits', 'Trait'], |
|
39
|
52 |
|
['Interfaces', 'Interface'], |
|
40
|
52 |
|
$fieldFqn |
|
41
|
|
|
); |
|
42
|
52 |
|
$fieldInterfaceReflection = new \ReflectionClass($fieldInterfaceFqn); |
|
43
|
52 |
|
$this->checkInterfaceLooksLikeField($fieldInterfaceReflection); |
|
44
|
52 |
|
$fieldInterface = PhpInterface::fromFile($fieldInterfaceReflection->getFileName()); |
|
45
|
|
|
} catch (\Exception $e) { |
|
46
|
|
|
throw new DoctrineStaticMetaException( |
|
47
|
|
|
'Failed loading the entity or field from FQN: '.$e->getMessage(), |
|
48
|
|
|
$e->getCode(), |
|
49
|
|
|
$e |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
52 |
|
$entity->addTrait($field); |
|
53
|
52 |
|
$entity->addInterface($fieldInterface); |
|
54
|
52 |
|
$this->codeHelper->generate($entity, $entityReflection->getFileName()); |
|
55
|
52 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param \ReflectionClass $fieldInterfaceReflection |
|
59
|
|
|
*/ |
|
60
|
52 |
|
protected function checkInterfaceLooksLikeField(\ReflectionClass $fieldInterfaceReflection): void |
|
61
|
|
|
{ |
|
62
|
|
|
$notFound = [ |
|
63
|
52 |
|
'PROP_', |
|
64
|
|
|
'DEFAULT_', |
|
65
|
|
|
]; |
|
66
|
52 |
|
$consts = $fieldInterfaceReflection->getConstants(); |
|
67
|
52 |
|
foreach (\array_keys($consts) as $name) { |
|
68
|
52 |
|
foreach ($notFound as $key => $prefix) { |
|
69
|
52 |
|
if (0 === strpos($name, $prefix)) { |
|
70
|
52 |
|
unset($notFound[$key]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
52 |
|
if ([] !== $notFound) { |
|
75
|
|
|
throw new \InvalidArgumentException( |
|
76
|
|
|
'Field '.$fieldInterfaceReflection->getName() |
|
77
|
|
|
.' does not look like a field interface, failed to find the following const prefixes: ' |
|
78
|
|
|
."\n".print_r($notFound, true)); |
|
79
|
|
|
} |
|
80
|
52 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|