1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\DataTransferObjects\DtoCreator; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\AbstractTestFakerDataProviderUpdater; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
10
|
|
|
use gossi\codegen\model\PhpClass; |
11
|
|
|
use gossi\codegen\model\PhpInterface; |
12
|
|
|
use gossi\codegen\model\PhpTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class EntityEmbeddableSetter |
16
|
|
|
* |
17
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable |
18
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
19
|
|
|
*/ |
20
|
|
|
class EntityEmbeddableSetter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var CodeHelper |
24
|
|
|
*/ |
25
|
|
|
protected $codeHelper; |
26
|
|
|
/** |
27
|
|
|
* @var NamespaceHelper |
28
|
|
|
*/ |
29
|
|
|
protected $namespaceHelper; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $projectRootNamespace = ''; |
34
|
|
|
/** |
35
|
|
|
* @var AbstractTestFakerDataProviderUpdater |
36
|
|
|
*/ |
37
|
|
|
private $abstractTestFakerDataProviderUpdater; |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $pathToProjectRoot; |
42
|
|
|
/** |
43
|
|
|
* @var DtoCreator |
44
|
|
|
*/ |
45
|
|
|
private $dtoCreator; |
46
|
|
|
|
47
|
|
|
public function __construct( |
48
|
|
|
CodeHelper $codeHelper, |
49
|
|
|
NamespaceHelper $namespaceHelper, |
50
|
|
|
AbstractTestFakerDataProviderUpdater $abstractTestFakerDataProviderUpdater, |
51
|
|
|
Config $config, |
52
|
|
|
DtoCreator $dtoCreator |
53
|
|
|
) { |
54
|
|
|
$this->codeHelper = $codeHelper; |
55
|
|
|
$this->namespaceHelper = $namespaceHelper; |
56
|
|
|
$this->abstractTestFakerDataProviderUpdater = $abstractTestFakerDataProviderUpdater; |
57
|
|
|
$this->pathToProjectRoot = $config::getProjectRootDirectory(); |
58
|
|
|
$this->setProjectRootNamespace($this->namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
59
|
|
|
$this->dtoCreator = $dtoCreator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $projectRootNamespace |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
68
|
|
|
{ |
69
|
|
|
$this->projectRootNamespace = rtrim($projectRootNamespace, '\\'); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $pathToProjectRoot |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
*/ |
80
|
|
|
public function setPathToProjectRoot(string $pathToProjectRoot): self |
81
|
|
|
{ |
82
|
|
|
$realPath = \realpath($pathToProjectRoot); |
83
|
|
|
if (false === $realPath) { |
84
|
|
|
throw new \RuntimeException('Invalid path to project root ' . $pathToProjectRoot); |
85
|
|
|
} |
86
|
|
|
$this->pathToProjectRoot = $realPath; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setEntityHasEmbeddable(string $entityFqn, string $embeddableTraitFqn): void |
92
|
|
|
{ |
93
|
|
|
$entityReflection = new \ts\Reflection\ReflectionClass($entityFqn); |
94
|
|
|
$entity = PhpClass::fromFile($entityReflection->getFileName()); |
95
|
|
|
$entityInterfaceFqn = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn); |
96
|
|
|
$entityInterfaceReflection = new \ts\Reflection\ReflectionClass($entityInterfaceFqn); |
97
|
|
|
$entityInterface = PhpInterface::fromFile($entityInterfaceReflection->getFileName()); |
98
|
|
|
$embeddableReflection = new \ts\Reflection\ReflectionClass($embeddableTraitFqn); |
99
|
|
|
$trait = PhpTrait::fromFile($embeddableReflection->getFileName()); |
100
|
|
|
$interfaceFqn = \str_replace( |
101
|
|
|
'\Traits\\', |
102
|
|
|
'\Interfaces\\', |
103
|
|
|
$embeddableTraitFqn |
104
|
|
|
); |
105
|
|
|
$interfaceFqn = $this->namespaceHelper->swapSuffix($interfaceFqn, 'Trait', 'Interface'); |
106
|
|
|
$interfaceReflection = new \ts\Reflection\ReflectionClass($interfaceFqn); |
107
|
|
|
$interface = PhpInterface::fromFile($interfaceReflection->getFileName()); |
108
|
|
|
$entity->addTrait($trait); |
109
|
|
|
$this->codeHelper->generate($entity, $entityReflection->getFileName()); |
110
|
|
|
$entityInterface->addInterface($interface); |
111
|
|
|
$this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName()); |
112
|
|
|
$this->abstractTestFakerDataProviderUpdater->updateFakerProviderArrayWithEmbeddableFakerData( |
113
|
|
|
$this->pathToProjectRoot, |
114
|
|
|
$embeddableTraitFqn, |
115
|
|
|
$entityFqn |
116
|
|
|
); |
117
|
|
|
$this->dtoCreator->setNewObjectFqnFromEntityFqn($entityFqn) |
118
|
|
|
->setProjectRootDirectory($this->pathToProjectRoot) |
119
|
|
|
->setProjectRootNamespace($this->projectRootNamespace) |
120
|
|
|
->createTargetFileObject() |
121
|
|
|
->write(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|