@@ -26,7 +26,7 @@ class Generator
26
26
private GeneratorTwigHelper $ twigHelper ;
27
27
private array $ pendingOperations = [];
28
28
private ?TemplateComponentGenerator $ templateComponentGenerator ;
29
- private array $ generatedPhpFiles = [];
29
+ private array $ generatedFiles = [];
30
30
31
31
public function __construct (
32
32
private FileManager $ fileManager ,
@@ -70,13 +70,13 @@ public function generateClass(string $className, string $templateName, array $va
70
70
71
71
$ this ->addOperation ($ targetPath , $ templateName , $ variables );
72
72
73
- $ this ->generatedPhpFiles [] = $ targetPath ;
74
-
75
73
return $ targetPath ;
76
74
}
77
75
78
76
/**
79
77
* Generate a normal file from a template.
78
+ *
79
+ * @return void
80
80
*/
81
81
public function generateFile (string $ targetPath , string $ templateName , array $ variables = [])
82
82
{
@@ -87,6 +87,9 @@ public function generateFile(string $targetPath, string $templateName, array $va
87
87
$ this ->addOperation ($ targetPath , $ templateName , $ variables );
88
88
}
89
89
90
+ /**
91
+ * @return void
92
+ */
90
93
public function dumpFile (string $ targetPath , string $ contents )
91
94
{
92
95
$ this ->pendingOperations [$ targetPath ] = [
@@ -163,29 +166,6 @@ public function getRootDirectory(): string
163
166
return $ this ->fileManager ->getRootDirectory ();
164
167
}
165
168
166
- private function addOperation (string $ targetPath , string $ templateName , array $ variables ): void
167
- {
168
- if ($ this ->fileManager ->fileExists ($ targetPath )) {
169
- throw new RuntimeCommandException (sprintf ('The file "%s" can \'t be generated because it already exists. ' , $ this ->fileManager ->relativizePath ($ targetPath )));
170
- }
171
-
172
- $ variables ['relative_path ' ] = $ this ->fileManager ->relativizePath ($ targetPath );
173
-
174
- $ templatePath = $ templateName ;
175
- if (!file_exists ($ templatePath )) {
176
- $ templatePath = __DIR__ .'/Resources/skeleton/ ' .$ templateName ;
177
-
178
- if (!file_exists ($ templatePath )) {
179
- throw new \Exception (sprintf ('Cannot find template "%s" ' , $ templateName ));
180
- }
181
- }
182
-
183
- $ this ->pendingOperations [$ targetPath ] = [
184
- 'template ' => $ templatePath ,
185
- 'variables ' => $ variables ,
186
- ];
187
- }
188
-
189
169
public function hasPendingOperations (): bool
190
170
{
191
171
return !empty ($ this ->pendingOperations );
@@ -199,6 +179,8 @@ public function hasPendingOperations(): bool
199
179
public function writeChanges ()
200
180
{
201
181
foreach ($ this ->pendingOperations as $ targetPath => $ templateData ) {
182
+ $ this ->generatedFiles [] = $ targetPath ;
183
+
202
184
if (isset ($ templateData ['contents ' ])) {
203
185
$ this ->fileManager ->dumpFile ($ targetPath , $ templateData ['contents ' ]);
204
186
@@ -207,7 +189,7 @@ public function writeChanges()
207
189
208
190
$ this ->fileManager ->dumpFile (
209
191
$ targetPath ,
210
- $ this ->getFileContentsForPendingOperation ($ targetPath, $ templateData )
192
+ $ this ->getFileContentsForPendingOperation ($ targetPath )
211
193
);
212
194
}
213
195
@@ -245,9 +227,12 @@ public function generateTemplate(string $targetPath, string $templateName, array
245
227
);
246
228
}
247
229
248
- public function getGeneratedPhpFiles (): array
230
+ /**
231
+ * Get the full path of each file created by the Generator.
232
+ */
233
+ public function getGeneratedFiles (): array
249
234
{
250
- return $ this ->generatedPhpFiles ;
235
+ return $ this ->generatedFiles ;
251
236
}
252
237
253
238
/**
@@ -259,4 +244,27 @@ public static function getControllerBaseClass(): ClassNameDetails
259
244
260
245
return new ClassNameDetails (AbstractController::class, '\\' );
261
246
}
247
+
248
+ private function addOperation (string $ targetPath , string $ templateName , array $ variables ): void
249
+ {
250
+ if ($ this ->fileManager ->fileExists ($ targetPath )) {
251
+ throw new RuntimeCommandException (sprintf ('The file "%s" can \'t be generated because it already exists. ' , $ this ->fileManager ->relativizePath ($ targetPath )));
252
+ }
253
+
254
+ $ variables ['relative_path ' ] = $ this ->fileManager ->relativizePath ($ targetPath );
255
+
256
+ $ templatePath = $ templateName ;
257
+ if (!file_exists ($ templatePath )) {
258
+ $ templatePath = __DIR__ .'/Resources/skeleton/ ' .$ templateName ;
259
+
260
+ if (!file_exists ($ templatePath )) {
261
+ throw new \Exception (sprintf ('Cannot find template "%s" ' , $ templateName ));
262
+ }
263
+ }
264
+
265
+ $ this ->pendingOperations [$ targetPath ] = [
266
+ 'template ' => $ templatePath ,
267
+ 'variables ' => $ variables ,
268
+ ];
269
+ }
262
270
}
0 commit comments