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