1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\Pipeline; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceNameProcess; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process\ReplaceProjectRootNamespaceProcess; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractCreator implements CreatorInterface |
18
|
|
|
{ |
19
|
|
|
protected const TEMPLATE_ENTITY_NAME = 'TemplateEntity'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The absolute path to the template file residing in the root codeTemplates directory |
23
|
|
|
*/ |
24
|
|
|
protected const TEMPLATE_PATH = 'override me'; |
25
|
|
|
/** |
26
|
|
|
* The basename of the Template object FQN |
27
|
|
|
*/ |
28
|
|
|
protected const FIND_NAME = 'override me'; |
29
|
|
|
/** |
30
|
|
|
* The path to the root code templates folder |
31
|
|
|
*/ |
32
|
|
|
public const ROOT_TEMPLATE_PATH = __DIR__ . '/../../../codeTemplates/'; |
33
|
|
|
|
34
|
|
|
public const SRC_DIR = 'src'; |
35
|
|
|
|
36
|
|
|
public const TEST_DIR = 'tests'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $newObjectFqn; |
42
|
|
|
/** |
43
|
|
|
* @var File |
44
|
|
|
*/ |
45
|
|
|
protected $templateFile; |
46
|
|
|
/** |
47
|
|
|
* @var File |
48
|
|
|
*/ |
49
|
|
|
protected $targetFile; |
50
|
|
|
/** |
51
|
|
|
* @var FileFactory |
52
|
|
|
*/ |
53
|
|
|
protected $fileFactory; |
54
|
|
|
/** |
55
|
|
|
* @var FindReplaceFactory |
56
|
|
|
*/ |
57
|
|
|
protected $findReplaceFactory; |
58
|
|
|
/** |
59
|
|
|
* @var NamespaceHelper |
60
|
|
|
*/ |
61
|
|
|
protected $namespaceHelper; |
62
|
|
|
/** |
63
|
|
|
* @var File\Writer |
64
|
|
|
*/ |
65
|
|
|
protected $fileWriter; |
66
|
|
|
/** |
67
|
|
|
* @var Pipeline |
68
|
|
|
*/ |
69
|
|
|
protected $pipeline; |
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected $projectRootNamespace; |
74
|
|
|
/** |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $projectRootDirectory; |
78
|
|
|
|
79
|
19 |
|
public function __construct( |
80
|
|
|
FileFactory $fileFactory, |
81
|
|
|
NamespaceHelper $namespaceHelper, |
82
|
|
|
File\Writer $fileWriter, |
83
|
|
|
Config $config, |
84
|
|
|
FindReplaceFactory $findReplaceFactory |
85
|
|
|
) { |
86
|
19 |
|
$this->fileFactory = $fileFactory; |
87
|
19 |
|
$this->namespaceHelper = $namespaceHelper; |
88
|
19 |
|
$this->fileWriter = $fileWriter; |
89
|
19 |
|
$this->setProjectRootNamespace($this->namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
90
|
19 |
|
$this->setProjectRootDirectory($config::getProjectRootDirectory()); |
91
|
19 |
|
$this->findReplaceFactory = $findReplaceFactory; |
92
|
19 |
|
} |
93
|
|
|
|
94
|
19 |
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
95
|
|
|
{ |
96
|
19 |
|
$this->projectRootNamespace = $projectRootNamespace; |
97
|
19 |
|
$this->fileFactory->setProjectRootNamespace($projectRootNamespace); |
98
|
|
|
|
99
|
19 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
19 |
|
public function setProjectRootDirectory(string $projectRootDirectory): self |
103
|
|
|
{ |
104
|
19 |
|
$this->projectRootDirectory = $projectRootDirectory; |
105
|
19 |
|
$this->fileFactory->setProjectRootDirectory($projectRootDirectory); |
106
|
|
|
|
107
|
19 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
18 |
|
public function createTargetFileObject(string $newObjectFqn = null): self |
111
|
|
|
{ |
112
|
18 |
|
if (null === $newObjectFqn && null === $this->newObjectFqn) { |
113
|
|
|
throw new \RuntimeException( |
114
|
|
|
'No new objectFqn either set previously or passed in' |
115
|
|
|
); |
116
|
|
|
} |
117
|
18 |
|
if (null !== $newObjectFqn) { |
118
|
12 |
|
$this->newObjectFqn = $newObjectFqn; |
119
|
|
|
} |
120
|
18 |
|
$this->templateFile = $this->fileFactory->createFromExistingPath(static::TEMPLATE_PATH); |
121
|
18 |
|
$this->targetFile = $this->fileFactory->createFromFqn($this->newObjectFqn); |
122
|
18 |
|
$this->updateRootDirOnTargetFile(); |
123
|
18 |
|
$this->setTargetContentsWithTemplateContents(); |
124
|
18 |
|
$this->configurePipeline(); |
125
|
18 |
|
$this->pipeline->run($this->targetFile); |
126
|
|
|
|
127
|
18 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Where the template file is in tests, we need to fix that in the target file |
132
|
|
|
*/ |
133
|
18 |
|
private function updateRootDirOnTargetFile(): void |
134
|
|
|
{ |
135
|
18 |
|
$realTemplateTestsPath = realpath(self::ROOT_TEMPLATE_PATH . self::TEST_DIR); |
136
|
18 |
|
if (0 === \strpos($this->templateFile->getPath(), $realTemplateTestsPath)) { |
137
|
|
|
$targetSrcPath = rtrim($this->projectRootDirectory, '/') . '/' . self::SRC_DIR; |
138
|
|
|
$targetTestsPath = rtrim($this->projectRootDirectory, '/') . '/' . self::TEST_DIR; |
139
|
|
|
$updatedPath = str_replace( |
140
|
|
|
$targetSrcPath, |
141
|
|
|
$targetTestsPath, |
142
|
|
|
$this->targetFile->getPath() |
143
|
|
|
); |
144
|
|
|
$this->targetFile->setPath($updatedPath); |
145
|
|
|
} |
146
|
18 |
|
} |
147
|
|
|
|
148
|
18 |
|
protected function setTargetContentsWithTemplateContents() |
149
|
|
|
{ |
150
|
18 |
|
$this->targetFile->setContents( |
151
|
18 |
|
$this->templateFile->loadContents() |
152
|
18 |
|
->getContents() |
153
|
|
|
); |
154
|
18 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* In this method we register all the process steps that we want to rnu against the file |
158
|
|
|
* |
159
|
|
|
* By default this registers the ReplaceNameProcess which is almost certainly required. Other processes can be |
160
|
|
|
* registered as required |
161
|
|
|
*/ |
162
|
18 |
|
protected function configurePipeline(): void |
163
|
|
|
{ |
164
|
18 |
|
$this->pipeline = new Pipeline($this->findReplaceFactory); |
165
|
18 |
|
$this->registerReplaceName(); |
166
|
18 |
|
$this->registerReplaceProjectRootNamespace(); |
167
|
18 |
|
} |
168
|
|
|
|
169
|
18 |
|
protected function registerReplaceName(): void |
170
|
|
|
{ |
171
|
18 |
|
$replaceName = new ReplaceNameProcess(); |
172
|
18 |
|
$replaceName->setArgs(static::FIND_NAME, $this->namespaceHelper->basename($this->newObjectFqn)); |
173
|
18 |
|
$this->pipeline->register($replaceName); |
174
|
18 |
|
} |
175
|
|
|
|
176
|
18 |
|
protected function registerReplaceProjectRootNamespace() |
177
|
|
|
{ |
178
|
18 |
|
$replaceTemplateNamespace = new ReplaceProjectRootNamespaceProcess(); |
179
|
18 |
|
$replaceTemplateNamespace->setProjectRootNamespace($this->projectRootNamespace); |
180
|
18 |
|
$this->pipeline->register($replaceTemplateNamespace); |
181
|
18 |
|
} |
182
|
|
|
|
183
|
18 |
|
public function getTargetFile(): File |
184
|
|
|
{ |
185
|
18 |
|
return $this->targetFile; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Write the file only if it doesn't already exist |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
193
|
|
|
*/ |
194
|
|
|
public function writeIfNotExists(): string |
195
|
|
|
{ |
196
|
|
|
if ($this->targetFile->exists()) { |
197
|
|
|
return $this->targetFile->getPath(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->write(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Write the file and return the generated path |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
* @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException |
208
|
|
|
*/ |
209
|
|
|
public function write(): string |
210
|
|
|
{ |
211
|
|
|
return $this->fileWriter->write($this->targetFile); |
212
|
|
|
} |
213
|
|
|
|
214
|
8 |
|
protected function registerEntityReplaceName(string $entityFqn): void |
215
|
|
|
{ |
216
|
8 |
|
$replaceName = new ReplaceNameProcess(); |
217
|
8 |
|
$replaceName->setArgs(self::TEMPLATE_ENTITY_NAME, $this->namespaceHelper->basename($entityFqn)); |
218
|
8 |
|
$this->pipeline->register($replaceName); |
219
|
8 |
|
} |
220
|
|
|
} |
221
|
|
|
|