|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FindAndReplaceHelper; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper; |
|
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
13
|
|
|
|
|
14
|
|
|
class GenerateRelationCodeForEntity |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var NamespaceHelper |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $namespaceHelper; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var PathHelper |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $pathHelper; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var FindAndReplaceHelper |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $findAndReplaceHelper; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $projectRootNamespace; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $entityFqn; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $pathToProjectRoot; |
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $srcSubFolderName; |
|
44
|
|
|
/** |
|
45
|
|
|
* @var CodeHelper |
|
46
|
|
|
*/ |
|
47
|
|
|
private $codeHelper; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $destinationDirectory; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
private $singularNamespace; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $pluralNamespace; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $singularNamespacedName; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
private $pluralNamespacedName; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
private $dirsToRename; |
|
72
|
|
|
/** |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
private $filesCreated; |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
24 |
|
public function __construct( |
|
79
|
|
|
string $entityFqn, |
|
80
|
|
|
string $pathToProjectRoot, |
|
81
|
|
|
string $projectRootNamespace, |
|
82
|
|
|
string $srcSubFolderName, |
|
83
|
|
|
CodeHelper $codeHelper, |
|
84
|
|
|
NamespaceHelper $namespaceHelper, |
|
85
|
|
|
PathHelper $pathHelper, |
|
86
|
|
|
FindAndReplaceHelper $findAndReplaceHelper |
|
87
|
|
|
) { |
|
88
|
24 |
|
$this->entityFqn = $entityFqn; |
|
89
|
24 |
|
$this->pathToProjectRoot = $pathToProjectRoot; |
|
90
|
24 |
|
$this->projectRootNamespace = $projectRootNamespace; |
|
91
|
24 |
|
$this->srcSubFolderName = $srcSubFolderName; |
|
92
|
24 |
|
$this->codeHelper = $codeHelper; |
|
93
|
24 |
|
$this->namespaceHelper = $namespaceHelper; |
|
94
|
24 |
|
$this->pathHelper = $pathHelper; |
|
95
|
24 |
|
$this->findAndReplaceHelper = $findAndReplaceHelper; |
|
96
|
24 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Calculate and set the destination full path to the destination directory for the generated relations files |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $className |
|
102
|
|
|
* @param array $subDirsNoEntities |
|
103
|
|
|
* |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
24 |
|
private function setDestinationDirectory( |
|
107
|
|
|
string $className, |
|
108
|
|
|
array $subDirsNoEntities |
|
109
|
|
|
): void { |
|
110
|
|
|
|
|
111
|
24 |
|
$subDirsNoEntities = \array_slice($subDirsNoEntities, 2); |
|
112
|
|
|
|
|
113
|
24 |
|
$this->destinationDirectory = $this->codeHelper->resolvePath( |
|
114
|
24 |
|
$this->pathToProjectRoot |
|
115
|
24 |
|
.'/'.$this->srcSubFolderName |
|
116
|
24 |
|
.AbstractGenerator::ENTITY_RELATIONS_FOLDER_NAME |
|
117
|
24 |
|
.\implode( |
|
118
|
24 |
|
'/', |
|
119
|
24 |
|
$subDirsNoEntities |
|
120
|
|
|
) |
|
121
|
24 |
|
.'/'.$className |
|
122
|
|
|
); |
|
123
|
24 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Copy all the relations code templates into the destination directory read for processing |
|
127
|
|
|
* |
|
128
|
|
|
* @throws DoctrineStaticMetaException |
|
129
|
|
|
*/ |
|
130
|
24 |
|
private function copyRelationsTemplateToDestinationDirectory(): void |
|
131
|
|
|
{ |
|
132
|
24 |
|
$this->pathHelper->copyTemplateDirectoryAndGetPath( |
|
133
|
24 |
|
$this->pathToProjectRoot, |
|
134
|
24 |
|
AbstractGenerator::RELATIONS_TEMPLATE_PATH, |
|
135
|
24 |
|
$this->destinationDirectory |
|
136
|
|
|
); |
|
137
|
24 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Perform the find and replace operations on the specified file |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $path |
|
143
|
|
|
*/ |
|
144
|
24 |
|
private function performFindAndReplaceInFile( |
|
145
|
|
|
string $path |
|
146
|
|
|
): void { |
|
147
|
24 |
|
$this->findAndReplaceHelper->findReplace( |
|
148
|
24 |
|
'use '.RelationsGenerator::FIND_ENTITIES_NAMESPACE.'\\'.RelationsGenerator::FIND_ENTITY_NAME, |
|
149
|
24 |
|
"use {$this->entityFqn}", |
|
150
|
24 |
|
$path |
|
151
|
|
|
); |
|
152
|
24 |
|
$this->findAndReplaceHelper->findReplaceRegex( |
|
153
|
24 |
|
'%use(.+?)Relations\\\TemplateEntity(.+?);%', |
|
154
|
24 |
|
'use ${1}Relations\\'.$this->singularNamespace.'${2};', |
|
155
|
24 |
|
$path |
|
156
|
|
|
); |
|
157
|
24 |
|
$this->findAndReplaceHelper->findReplaceRegex( |
|
158
|
24 |
|
'%use(.+?)Relations\\\TemplateEntity(.+?);%', |
|
159
|
24 |
|
'use ${1}Relations\\'.$this->pluralNamespace.'${2};', |
|
160
|
24 |
|
$path |
|
161
|
|
|
); |
|
162
|
24 |
|
$this->findAndReplaceHelper->replaceName($this->singularNamespacedName, $path); |
|
163
|
24 |
|
$this->findAndReplaceHelper->replacePluralName($this->pluralNamespacedName, $path); |
|
164
|
24 |
|
$this->findAndReplaceHelper->replaceProjectNamespace($this->projectRootNamespace, $path); |
|
165
|
24 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Loop through the created files and rename the paths |
|
169
|
|
|
* |
|
170
|
|
|
* @throws DoctrineStaticMetaException |
|
171
|
|
|
*/ |
|
172
|
24 |
|
private function renamePathsForCreatedFiles(): void |
|
173
|
|
|
{ |
|
174
|
24 |
|
foreach ($this->filesCreated as $key => $realPath) { |
|
175
|
24 |
|
$this->filesCreated[$key] = $this->pathHelper->renamePathBasenameSingularOrPlural( |
|
176
|
24 |
|
$realPath, |
|
177
|
24 |
|
$this->singularNamespacedName, |
|
178
|
24 |
|
$this->pluralNamespacedName |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
24 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Initialise the values for all requires variable |
|
185
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
186
|
|
|
* |
|
187
|
|
|
* @throws DoctrineStaticMetaException |
|
188
|
|
|
*/ |
|
189
|
24 |
|
private function initialiseVariables(): void |
|
190
|
|
|
{ |
|
191
|
24 |
|
list($className, , $subDirs) = $this->namespaceHelper->parseFullyQualifiedName( |
|
192
|
24 |
|
$this->entityFqn, |
|
193
|
24 |
|
$this->srcSubFolderName, |
|
194
|
24 |
|
$this->projectRootNamespace |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
24 |
|
$this->singularNamespacedName = $this->namespaceHelper->getSingularNamespacedName( |
|
198
|
24 |
|
$this->entityFqn, |
|
199
|
24 |
|
$subDirs |
|
200
|
|
|
); |
|
201
|
24 |
|
$this->pluralNamespacedName = $this->namespaceHelper->getPluralNamespacedName( |
|
202
|
24 |
|
$this->entityFqn, |
|
203
|
24 |
|
$subDirs |
|
204
|
|
|
); |
|
205
|
24 |
|
$this->setDestinationDirectory( |
|
206
|
24 |
|
$className, |
|
207
|
24 |
|
$subDirs |
|
208
|
|
|
); |
|
209
|
24 |
|
$plural = \ucfirst(MappingHelper::getPluralForFqn($this->entityFqn)); |
|
210
|
24 |
|
$singular = \ucfirst(MappingHelper::getSingularForFqn($this->entityFqn)); |
|
211
|
24 |
|
$nsNoEntities = \implode('\\', \array_slice($subDirs, 2)); |
|
212
|
24 |
|
$this->singularNamespace = \ltrim($nsNoEntities.'\\'.$singular, '\\'); |
|
213
|
24 |
|
$this->pluralNamespace = \ltrim($nsNoEntities.'\\'.$plural, '\\'); |
|
214
|
24 |
|
$this->dirsToRename = []; |
|
215
|
24 |
|
$this->filesCreated = []; |
|
216
|
24 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Loop through the iterator paths and process each item |
|
220
|
|
|
* |
|
221
|
|
|
* @param \Generator $relativePathRelationsGenerator |
|
222
|
|
|
*/ |
|
223
|
24 |
|
private function processPaths(\Generator $relativePathRelationsGenerator): void |
|
224
|
|
|
{ |
|
225
|
24 |
|
foreach ($relativePathRelationsGenerator as $path => $fileInfo) { |
|
226
|
24 |
|
$fullPath = $this->destinationDirectory."/$path"; |
|
227
|
24 |
|
$path = \realpath($fullPath); |
|
228
|
24 |
|
if (false === $path) { |
|
229
|
|
|
throw new \RuntimeException("path $fullPath does not exist"); |
|
230
|
|
|
} |
|
231
|
24 |
|
if ($fileInfo->isDir()) { |
|
232
|
24 |
|
$this->dirsToRename[] = $path; |
|
233
|
24 |
|
continue; |
|
234
|
|
|
} |
|
235
|
24 |
|
$this->performFindAndReplaceInFile($path); |
|
236
|
24 |
|
$this->filesCreated[] = $path; |
|
237
|
|
|
} |
|
238
|
24 |
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Loop through all the directories to rename and then rename them |
|
242
|
|
|
* |
|
243
|
|
|
* @throws DoctrineStaticMetaException |
|
244
|
|
|
*/ |
|
245
|
24 |
|
private function renameDirectories(): void |
|
246
|
|
|
{ |
|
247
|
|
|
//update directory names and update file created paths accordingly |
|
248
|
24 |
|
foreach ($this->dirsToRename as $dirPath) { |
|
249
|
24 |
|
$updateDirPath = $this->pathHelper->renamePathBasenameSingularOrPlural( |
|
250
|
24 |
|
$dirPath, |
|
251
|
24 |
|
$this->singularNamespacedName, |
|
252
|
24 |
|
$this->pluralNamespacedName |
|
253
|
|
|
); |
|
254
|
24 |
|
foreach ($this->filesCreated as $k => $filePath) { |
|
255
|
24 |
|
$this->filesCreated[$k] = \str_replace($dirPath, $updateDirPath, $filePath); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
24 |
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Update the namespace in all the created files |
|
262
|
|
|
* |
|
263
|
|
|
* @throws DoctrineStaticMetaException |
|
264
|
|
|
*/ |
|
265
|
24 |
|
private function updateNamespace() |
|
266
|
|
|
{ |
|
267
|
|
|
//now path is totally sorted, update namespace based on path |
|
268
|
24 |
|
foreach ($this->filesCreated as $filePath) { |
|
269
|
24 |
|
$this->findAndReplaceHelper->setNamespaceFromPath( |
|
270
|
24 |
|
$filePath, |
|
271
|
24 |
|
$this->srcSubFolderName, |
|
272
|
24 |
|
$this->projectRootNamespace |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
24 |
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param \Generator $relativePathRelationsGenerator |
|
279
|
|
|
* |
|
280
|
|
|
* @throws DoctrineStaticMetaException |
|
281
|
|
|
*/ |
|
282
|
24 |
|
public function __invoke(\Generator $relativePathRelationsGenerator) |
|
283
|
|
|
{ |
|
284
|
|
|
try { |
|
285
|
24 |
|
$this->initialiseVariables(); |
|
286
|
24 |
|
$this->copyRelationsTemplateToDestinationDirectory(); |
|
287
|
24 |
|
$this->processPaths($relativePathRelationsGenerator); |
|
288
|
24 |
|
$this->renamePathsForCreatedFiles(); |
|
289
|
24 |
|
$this->renameDirectories(); |
|
290
|
24 |
|
$this->updateNamespace(); |
|
291
|
|
|
} catch (\Exception $e) { |
|
292
|
|
|
throw new DoctrineStaticMetaException( |
|
293
|
|
|
'Exception generating relation for entity '.$this->entityFqn.': '.$e->getMessage(), |
|
294
|
|
|
$e->getCode(), |
|
295
|
|
|
$e |
|
296
|
|
|
); |
|
297
|
|
|
} |
|
298
|
24 |
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|