|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\AbstractCommand; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Config; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractGenerator |
|
14
|
|
|
{ |
|
15
|
|
|
public const TEMPLATE_PATH = __DIR__.'/../../../codeTemplates'; |
|
16
|
|
|
|
|
17
|
|
|
public const ENTITIES_FOLDER_NAME = 'Entities'; |
|
18
|
|
|
|
|
19
|
|
|
public const ENTITY_FOLDER_NAME = 'Entity'; |
|
20
|
|
|
|
|
21
|
|
|
public const ENTITY_RELATIONS_FOLDER_NAME = '/'.self::ENTITY_FOLDER_NAME.'/Relations/'; |
|
22
|
|
|
|
|
23
|
|
|
public const ENTITY_REPOSITORIES_FOLDER_NAME = '/'.self::ENTITY_FOLDER_NAME.'/Repositories/'; |
|
24
|
|
|
|
|
25
|
|
|
public const ENTITY_FIELDS_FOLDER_NAME = '/'.self::ENTITY_FOLDER_NAME.'/Fields/'; |
|
26
|
|
|
|
|
27
|
|
|
public const ENTITY_SAVERS_FOLDER_NAME = '/'.self::ENTITY_FOLDER_NAME.'/Savers/'; |
|
28
|
|
|
|
|
29
|
|
|
public const ENTITY_INTERFACES_FOLDER_NAME = '/'.self::ENTITY_FOLDER_NAME.'/Interfaces/'; |
|
30
|
|
|
|
|
31
|
|
|
public const ENTITY_TEMPLATE_PATH = self::TEMPLATE_PATH.'/src/'.self::ENTITIES_FOLDER_NAME |
|
32
|
|
|
.'/TemplateEntity.php'; |
|
33
|
|
|
|
|
34
|
|
|
public const ENTITY_INTERFACE_TEMPLATE_PATH = self::TEMPLATE_PATH.'/src/'.self::ENTITY_INTERFACES_FOLDER_NAME |
|
35
|
|
|
.'/TemplateEntityInterface.php'; |
|
36
|
|
|
|
|
37
|
|
|
public const ENTITY_TEST_TEMPLATE_PATH = self::TEMPLATE_PATH.'/tests/'.self::ENTITIES_FOLDER_NAME |
|
38
|
|
|
.'/TemplateEntityTest.php'; |
|
39
|
|
|
|
|
40
|
|
|
public const ABSTRACT_ENTITY_TEST_TEMPLATE_PATH = self::TEMPLATE_PATH.'/tests/'.self::ENTITIES_FOLDER_NAME |
|
41
|
|
|
.'/AbstractEntityTest.php'; |
|
42
|
|
|
|
|
43
|
|
|
public const PHPUNIT_BOOTSTRAP_TEMPLATE_PATH = self::TEMPLATE_PATH.'/tests/bootstrap.php'; |
|
44
|
|
|
|
|
45
|
|
|
public const RELATIONS_TEMPLATE_PATH = self::TEMPLATE_PATH.'/src/'.self::ENTITY_RELATIONS_FOLDER_NAME |
|
46
|
|
|
.'/TemplateEntity'; |
|
47
|
|
|
|
|
48
|
|
|
public const REPOSITORIES_TEMPLATE_PATH = self::TEMPLATE_PATH |
|
49
|
|
|
.'/src/'.self::ENTITY_REPOSITORIES_FOLDER_NAME |
|
50
|
|
|
.'/TemplateEntityRepository.php'; |
|
51
|
|
|
|
|
52
|
|
|
public const ABSTRACT_ENTITY_REPOSITORY_TEMPLATE_PATH = self::TEMPLATE_PATH |
|
53
|
|
|
.'/src/'.self::ENTITY_REPOSITORIES_FOLDER_NAME |
|
54
|
|
|
.'/AbstractEntityRepository.php'; |
|
55
|
|
|
|
|
56
|
|
|
public const FIELD_TRAIT_TEMPLATE_PATH = self::TEMPLATE_PATH.'/src/' |
|
57
|
|
|
.self::ENTITY_FIELDS_FOLDER_NAME |
|
58
|
|
|
.'/Traits/' |
|
59
|
|
|
.self::FIND_ENTITY_FIELD_NAME.'FieldTrait.php'; |
|
60
|
|
|
|
|
61
|
|
|
public const FIELD_INTERFACE_TEMPLATE_PATH = self::TEMPLATE_PATH.'/src/' |
|
62
|
|
|
.self::ENTITY_FIELDS_FOLDER_NAME |
|
63
|
|
|
.'/Interfaces/' |
|
64
|
|
|
.self::FIND_ENTITY_FIELD_NAME.'FieldInterface.php'; |
|
65
|
|
|
|
|
66
|
|
|
public const FIND_ENTITY_NAME = 'TemplateEntity'; |
|
67
|
|
|
|
|
68
|
|
|
public const FIND_ENTITY_NAME_PLURAL = 'TemplateEntities'; |
|
69
|
|
|
|
|
70
|
|
|
public const FIND_PROJECT_NAMESPACE = 'TemplateNamespace'; |
|
71
|
|
|
|
|
72
|
|
|
public const FIND_ENTITIES_NAMESPACE = self::FIND_PROJECT_NAMESPACE.'\\Entities'; |
|
73
|
|
|
|
|
74
|
|
|
public const FIND_ENTITY_NAMESPACE = self::FIND_PROJECT_NAMESPACE.'\\Entity'; |
|
75
|
|
|
|
|
76
|
|
|
public const ENTITY_RELATIONS_NAMESPACE = '\\Entity\\Relations'; |
|
77
|
|
|
|
|
78
|
|
|
public const FIND_ENTITY_RELATIONS_NAMESPACE = self::FIND_PROJECT_NAMESPACE.self::ENTITY_RELATIONS_NAMESPACE; |
|
79
|
|
|
|
|
80
|
|
|
public const ENTITY_REPOSITORIES_NAMESPACE = '\\Entity\\Repositories'; |
|
81
|
|
|
|
|
82
|
|
|
public const ENTITY_SAVERS_NAMESPACE = '\\Entity\\Savers'; |
|
83
|
|
|
|
|
84
|
|
|
public const FIND_ENTITY_REPOSITORIES_NAMESPACE = self::FIND_PROJECT_NAMESPACE.self::ENTITY_REPOSITORIES_NAMESPACE; |
|
85
|
|
|
|
|
86
|
|
|
public const ENTITY_INTERFACE_NAMESPACE = '\\Entity\\Interfaces'; |
|
87
|
|
|
|
|
88
|
|
|
public const FIND_ENTITY_INTERFACE_NAMESPACE = self::FIND_PROJECT_NAMESPACE.self::ENTITY_INTERFACE_NAMESPACE; |
|
89
|
|
|
|
|
90
|
|
|
public const FIND_ENTITY_FIELD_NAME = 'TemplateFieldName'; |
|
91
|
|
|
|
|
92
|
|
|
public const ENTITY_FIELD_NAMESPACE = '\\Entity\\Fields'; |
|
93
|
|
|
|
|
94
|
|
|
public const ENTITY_FIELD_TRAIT_NAMESPACE = self::ENTITY_FIELD_NAMESPACE.'\\Traits'; |
|
95
|
|
|
|
|
96
|
|
|
public const ENTITY_FIELD_INTERFACE_NAMESPACE = self::ENTITY_FIELD_NAMESPACE.'\\Interfaces'; |
|
97
|
|
|
|
|
98
|
|
|
public const FIND_FIELD_TRAIT_NAMESPACE = self::FIND_PROJECT_NAMESPACE.self::ENTITY_FIELD_TRAIT_NAMESPACE; |
|
99
|
|
|
|
|
100
|
|
|
public const FIND_FIELD_INTERFACE_NAMESPACE = self::FIND_PROJECT_NAMESPACE.self::ENTITY_FIELD_INTERFACE_NAMESPACE; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @var string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected $projectRootNamespace = ''; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected $pathToProjectRoot = ''; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected $srcSubFolderName = AbstractCommand::DEFAULT_SRC_SUBFOLDER; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var string |
|
119
|
|
|
*/ |
|
120
|
|
|
protected $testSubFolderName = AbstractCommand::DEFAULT_TEST_SUBFOLDER; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @var Filesystem |
|
124
|
|
|
*/ |
|
125
|
|
|
protected $fileSystem; |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @var FileCreationTransaction |
|
129
|
|
|
*/ |
|
130
|
|
|
protected $fileCreationTransaction; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @var NamespaceHelper |
|
134
|
|
|
*/ |
|
135
|
|
|
protected $namespaceHelper; |
|
136
|
|
|
/** |
|
137
|
|
|
* @var CodeHelper |
|
138
|
|
|
*/ |
|
139
|
|
|
protected $codeHelper; |
|
140
|
|
|
/** |
|
141
|
|
|
* @var PathHelper |
|
142
|
|
|
*/ |
|
143
|
|
|
protected $pathHelper; |
|
144
|
|
|
/** |
|
145
|
|
|
* @var FindAndReplaceHelper |
|
146
|
|
|
*/ |
|
147
|
|
|
protected $findAndReplaceHelper; |
|
148
|
|
|
|
|
149
|
60 |
|
public function __construct( |
|
150
|
|
|
Filesystem $filesystem, |
|
151
|
|
|
FileCreationTransaction $fileCreationTransaction, |
|
152
|
|
|
NamespaceHelper $namespaceHelper, |
|
153
|
|
|
Config $config, |
|
154
|
|
|
CodeHelper $codeHelper, |
|
155
|
|
|
PathHelper $pathHelper, |
|
156
|
|
|
FindAndReplaceHelper $findAndReplaceHelper |
|
157
|
|
|
) { |
|
158
|
60 |
|
$this->fileSystem = $filesystem; |
|
159
|
60 |
|
$this->fileCreationTransaction = $fileCreationTransaction; |
|
160
|
60 |
|
$this->namespaceHelper = $namespaceHelper; |
|
161
|
60 |
|
$this->setProjectRootNamespace($this->namespaceHelper->getProjectRootNamespaceFromComposerJson()); |
|
162
|
60 |
|
$this->setPathToProjectRoot($config::getProjectRootDirectory()); |
|
163
|
60 |
|
$this->codeHelper = $codeHelper; |
|
164
|
60 |
|
$this->pathHelper = $pathHelper; |
|
165
|
60 |
|
$this->findAndReplaceHelper = $findAndReplaceHelper; |
|
166
|
60 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $projectRootNamespace |
|
170
|
|
|
* |
|
171
|
|
|
* @return $this |
|
172
|
|
|
*/ |
|
173
|
60 |
|
public function setProjectRootNamespace(string $projectRootNamespace): AbstractGenerator |
|
174
|
|
|
{ |
|
175
|
60 |
|
$this->projectRootNamespace = rtrim($projectRootNamespace, '\\'); |
|
176
|
|
|
|
|
177
|
60 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $pathToProjectRoot |
|
182
|
|
|
* |
|
183
|
|
|
* @return $this |
|
184
|
|
|
* @throws \RuntimeException |
|
185
|
|
|
*/ |
|
186
|
60 |
|
public function setPathToProjectRoot(string $pathToProjectRoot): AbstractGenerator |
|
187
|
|
|
{ |
|
188
|
60 |
|
$realPath = \realpath($pathToProjectRoot); |
|
189
|
60 |
|
if (false === $realPath) { |
|
190
|
|
|
throw new \RuntimeException('Invalid path to project root '.$pathToProjectRoot); |
|
191
|
|
|
} |
|
192
|
60 |
|
$this->pathToProjectRoot = $realPath; |
|
193
|
|
|
|
|
194
|
60 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $srcSubFolderName |
|
199
|
|
|
* |
|
200
|
|
|
* @return $this |
|
201
|
|
|
*/ |
|
202
|
|
|
public function setSrcSubFolderName(string $srcSubFolderName): AbstractGenerator |
|
203
|
|
|
{ |
|
204
|
|
|
$this->srcSubFolderName = $srcSubFolderName; |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $testSubFolderName |
|
211
|
|
|
* |
|
212
|
|
|
* @return $this |
|
213
|
|
|
*/ |
|
214
|
4 |
|
public function setTestSubFolderName(string $testSubFolderName): AbstractGenerator |
|
215
|
|
|
{ |
|
216
|
4 |
|
$this->testSubFolderName = $testSubFolderName; |
|
217
|
|
|
|
|
218
|
4 |
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
58 |
|
protected function getFilesystem(): Filesystem |
|
223
|
|
|
{ |
|
224
|
58 |
|
return $this->fileSystem; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* From the fully qualified name, parse out: |
|
229
|
|
|
* - class name, |
|
230
|
|
|
* - namespace |
|
231
|
|
|
* - the namespace parts not including the project root namespace |
|
232
|
|
|
* |
|
233
|
|
|
* @param string $fqn |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $srcOrTestSubFolder |
|
236
|
|
|
* |
|
237
|
|
|
* @return array [$className,$namespace,$subDirectories] |
|
238
|
|
|
* @throws DoctrineStaticMetaException |
|
239
|
|
|
*/ |
|
240
|
59 |
|
protected function parseFullyQualifiedName(string $fqn, string $srcOrTestSubFolder = null): array |
|
241
|
|
|
{ |
|
242
|
59 |
|
if (null === $srcOrTestSubFolder) { |
|
243
|
21 |
|
$srcOrTestSubFolder = $this->srcSubFolderName; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
59 |
|
return $this->namespaceHelper->parseFullyQualifiedName($fqn, $srcOrTestSubFolder, $this->projectRootNamespace); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param array $subDirectories |
|
251
|
|
|
* |
|
252
|
|
|
* @return string |
|
253
|
|
|
* @throws DoctrineStaticMetaException |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function createSubDirectoriesAndGetPath(array $subDirectories): string |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->pathHelper->createSubDirectoriesAndGetPath($this->pathToProjectRoot, $subDirectories); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param string $templatePath |
|
262
|
|
|
* @param string $destPath |
|
263
|
|
|
* |
|
264
|
|
|
* @return string |
|
265
|
|
|
* @throws DoctrineStaticMetaException |
|
266
|
|
|
*/ |
|
267
|
|
|
protected function copyTemplateDirectoryAndGetPath( |
|
268
|
|
|
string $templatePath, |
|
269
|
|
|
string $destPath |
|
270
|
|
|
): string { |
|
271
|
|
|
return $this->pathHelper->copyTemplateDirectoryAndGetPath( |
|
272
|
|
|
$this->pathToProjectRoot, |
|
273
|
|
|
$templatePath, |
|
274
|
|
|
$destPath |
|
275
|
|
|
); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|