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\Generator\AbstractGenerator; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FileCreationTransaction; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FindAndReplaceHelper; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
13
|
|
|
use gossi\codegen\model\PhpClass; |
14
|
|
|
use gossi\codegen\model\PhpInterface; |
15
|
|
|
use gossi\codegen\model\PhpTrait; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class EntityFieldSetter |
20
|
|
|
* |
21
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field |
22
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
23
|
|
|
*/ |
24
|
|
|
class EntityFieldSetter extends AbstractGenerator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var AbstractTestFakerDataProviderUpdater |
28
|
|
|
*/ |
29
|
|
|
protected $updater; |
30
|
|
|
|
31
|
83 |
|
public function __construct( |
32
|
|
|
Filesystem $filesystem, |
33
|
|
|
FileCreationTransaction $fileCreationTransaction, |
34
|
|
|
NamespaceHelper $namespaceHelper, |
35
|
|
|
Config $config, |
36
|
|
|
CodeHelper $codeHelper, |
37
|
|
|
PathHelper $pathHelper, |
38
|
|
|
FindAndReplaceHelper $findAndReplaceHelper, |
39
|
|
|
AbstractTestFakerDataProviderUpdater $updater |
40
|
|
|
) { |
41
|
83 |
|
parent::__construct($filesystem, $fileCreationTransaction, $namespaceHelper, $config, $codeHelper, $pathHelper, |
42
|
83 |
|
$findAndReplaceHelper); |
43
|
83 |
|
$this->updater = $updater; |
44
|
83 |
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $fieldFqn |
49
|
|
|
* @param string $entityFqn |
50
|
|
|
* |
51
|
|
|
* @throws DoctrineStaticMetaException |
52
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
53
|
|
|
*/ |
54
|
70 |
|
public function setEntityHasField(string $entityFqn, string $fieldFqn): void |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
70 |
|
$entityReflection = new \ts\Reflection\ReflectionClass($entityFqn); |
58
|
70 |
|
$entity = PhpClass::fromFile($entityReflection->getFileName()); |
59
|
70 |
|
$entityInterfaceFqn = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn); |
60
|
70 |
|
$entityInterfaceReflection = new \ts\Reflection\ReflectionClass($entityInterfaceFqn); |
61
|
70 |
|
$entityInterface = PhpInterface::fromFile($entityInterfaceReflection->getFileName()); |
62
|
70 |
|
$fieldReflection = new \ts\Reflection\ReflectionClass($fieldFqn); |
63
|
70 |
|
$field = PhpTrait::fromFile($fieldReflection->getFileName()); |
64
|
70 |
|
$fieldInterfaceFqn = \str_replace( |
65
|
70 |
|
['Traits', 'Trait'], |
66
|
70 |
|
['Interfaces', 'Interface'], |
67
|
70 |
|
$fieldFqn |
68
|
|
|
); |
69
|
70 |
|
$fieldInterfaceReflection = new \ts\Reflection\ReflectionClass($fieldInterfaceFqn); |
70
|
70 |
|
$this->checkInterfaceLooksLikeField($fieldInterfaceReflection); |
71
|
70 |
|
$fieldInterface = PhpInterface::fromFile($fieldInterfaceReflection->getFileName()); |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
throw new DoctrineStaticMetaException( |
74
|
|
|
'Failed loading the entity or field from FQN: ' . $e->getMessage(), |
75
|
|
|
$e->getCode(), |
76
|
|
|
$e |
77
|
|
|
); |
78
|
|
|
} |
79
|
70 |
|
if ($this->alreadyUsingFieldWithThisShortName($entity, $field)) { |
80
|
1 |
|
throw new \InvalidArgumentException('Entity already has a field with this short name'); |
81
|
|
|
} |
82
|
70 |
|
$entity->addTrait($field); |
83
|
70 |
|
$this->codeHelper->generate($entity, $entityReflection->getFileName()); |
84
|
70 |
|
$entityInterface->addInterface($fieldInterface); |
85
|
70 |
|
$this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName()); |
86
|
70 |
|
if ($this->fieldHasFakerProvider($fieldReflection)) { |
87
|
33 |
|
$this->updater->updateFakerProviderArray($this->pathToProjectRoot, $fieldFqn, $entityFqn); |
88
|
|
|
} |
89
|
70 |
|
} |
90
|
|
|
|
91
|
70 |
|
protected function alreadyUsingFieldWithThisShortName(PhpClass $entity, PhpTrait $field): bool |
92
|
|
|
{ |
93
|
70 |
|
$useStatements = $entity->getUseStatements(); |
94
|
|
|
|
95
|
70 |
|
return null !== $useStatements->get($field->getName()); |
96
|
|
|
} |
97
|
|
|
|
98
|
70 |
|
protected function fieldHasFakerProvider(\ts\Reflection\ReflectionClass $fieldReflection): bool |
99
|
|
|
{ |
100
|
70 |
|
return \class_exists( |
101
|
70 |
|
$this->namespaceHelper->getFakerProviderFqnFromFieldTraitReflection($fieldReflection) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \ts\Reflection\ReflectionClass $fieldInterfaceReflection |
107
|
|
|
*/ |
108
|
70 |
|
protected function checkInterfaceLooksLikeField(\ts\Reflection\ReflectionClass $fieldInterfaceReflection): void |
109
|
|
|
{ |
110
|
|
|
$lookFor = [ |
111
|
70 |
|
'PROP_', |
112
|
|
|
'DEFAULT_', |
113
|
|
|
]; |
114
|
70 |
|
$found = []; |
115
|
70 |
|
$consts = $fieldInterfaceReflection->getConstants(); |
116
|
70 |
|
foreach (\array_keys($consts) as $name) { |
117
|
70 |
|
foreach ($lookFor as $key => $prefix) { |
118
|
70 |
|
if (\ts\stringStartsWith($name, $prefix)) { |
119
|
70 |
|
$found[$key] = $prefix; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
70 |
|
if ($found !== $lookFor) { |
124
|
|
|
throw new \InvalidArgumentException( |
125
|
|
|
'Field ' . $fieldInterfaceReflection->getName() |
126
|
|
|
. ' does not look like a field interface, failed to find the following const prefixes: ' |
127
|
|
|
. "\n" . print_r($lookFor, true) |
128
|
|
|
); |
129
|
|
|
} |
130
|
70 |
|
} |
131
|
|
|
} |
132
|
|
|
|