@@ -36,7 +36,15 @@ trait GeneratorTrait
36
36
protected $ directory ;
37
37
38
38
/**
39
- * View template name
39
+ * (Optional) View template path
40
+ *
41
+ * We use special namespaced paths like:
42
+ * `CodeIgniter\Commands\Generators\Views\cell.tpl.php`.
43
+ */
44
+ protected ?string $ templatePath = null ;
45
+
46
+ /**
47
+ * View template name for fallback
40
48
*
41
49
* @var string
42
50
*/
@@ -118,6 +126,8 @@ protected function generateClass(array $params)
118
126
119
127
/**
120
128
* Generate a view file from an existing template.
129
+ *
130
+ * @param string $view namespaced view name that is generated
121
131
*/
122
132
protected function generateView (string $ view , array $ params )
123
133
{
@@ -135,6 +145,8 @@ protected function generateView(string $view, array $params)
135
145
136
146
/**
137
147
* Handles writing the file to disk, and all of the safety checks around that.
148
+ *
149
+ * @param string $target file path
138
150
*/
139
151
private function generateFile (string $ target , string $ content ): void
140
152
{
@@ -143,7 +155,13 @@ private function generateFile(string $target, string $content): void
143
155
CLI ::write (lang ('CLI.generator.usingCINamespace ' ), 'yellow ' );
144
156
CLI ::newLine ();
145
157
146
- if (CLI ::prompt ('Are you sure you want to continue? ' , ['y ' , 'n ' ], 'required ' ) === 'n ' ) {
158
+ if (
159
+ CLI ::prompt (
160
+ 'Are you sure you want to continue? ' ,
161
+ ['y ' , 'n ' ],
162
+ 'required '
163
+ ) === 'n '
164
+ ) {
147
165
CLI ::newLine ();
148
166
CLI ::write (lang ('CLI.generator.cancelOperation ' ), 'yellow ' );
149
167
CLI ::newLine ();
@@ -160,7 +178,11 @@ private function generateFile(string $target, string $content): void
160
178
// Overwriting files unknowingly is a serious annoyance, So we'll check if
161
179
// we are duplicating things, If 'force' option is not supplied, we bail.
162
180
if (! $ this ->getOption ('force ' ) && $ isFile ) {
163
- CLI ::error (lang ('CLI.generator.fileExist ' , [clean_path ($ target )]), 'light_gray ' , 'red ' );
181
+ CLI ::error (
182
+ lang ('CLI.generator.fileExist ' , [clean_path ($ target )]),
183
+ 'light_gray ' ,
184
+ 'red '
185
+ );
164
186
CLI ::newLine ();
165
187
166
188
return ;
@@ -179,26 +201,40 @@ private function generateFile(string $target, string $content): void
179
201
// contents from the template, and then we'll do the necessary replacements.
180
202
if (! write_file ($ target , $ content )) {
181
203
// @codeCoverageIgnoreStart
182
- CLI ::error (lang ('CLI.generator.fileError ' , [clean_path ($ target )]), 'light_gray ' , 'red ' );
204
+ CLI ::error (
205
+ lang ('CLI.generator.fileError ' , [clean_path ($ target )]),
206
+ 'light_gray ' ,
207
+ 'red '
208
+ );
183
209
CLI ::newLine ();
184
210
185
211
return ;
186
212
// @codeCoverageIgnoreEnd
187
213
}
188
214
189
215
if ($ this ->getOption ('force ' ) && $ isFile ) {
190
- CLI ::write (lang ('CLI.generator.fileOverwrite ' , [clean_path ($ target )]), 'yellow ' );
216
+ CLI ::write (
217
+ lang ('CLI.generator.fileOverwrite ' , [clean_path ($ target )]),
218
+ 'yellow '
219
+ );
191
220
CLI ::newLine ();
192
221
193
222
return ;
194
223
}
195
224
196
- CLI ::write (lang ('CLI.generator.fileCreate ' , [clean_path ($ target )]), 'green ' );
225
+ CLI ::write (
226
+ lang ('CLI.generator.fileCreate ' , [clean_path ($ target )]),
227
+ 'green '
228
+ );
197
229
CLI ::newLine ();
198
230
}
199
231
200
232
/**
201
233
* Prepare options and do the necessary replacements.
234
+ *
235
+ * @param string $class namespaced classname or namespaced view.
236
+ *
237
+ * @return string generated file content
202
238
*/
203
239
protected function prepare (string $ class ): string
204
240
{
@@ -244,15 +280,34 @@ protected function qualifyClassName(): string
244
280
$ class = $ matches [1 ] . ucfirst ($ matches [2 ]);
245
281
}
246
282
247
- if ($ this ->enabledSuffixing && $ this ->getOption ('suffix ' ) && preg_match ($ pattern , $ class ) !== 1 ) {
283
+ if (
284
+ $ this ->enabledSuffixing && $ this ->getOption ('suffix ' )
285
+ && preg_match ($ pattern , $ class ) !== 1
286
+ ) {
248
287
$ class .= ucfirst ($ component );
249
288
}
250
289
251
290
// Trims input, normalize separators, and ensure that all paths are in Pascalcase.
252
- $ class = ltrim (implode ('\\' , array_map ('pascalize ' , explode ('\\' , str_replace ('/ ' , '\\' , trim ($ class ))))), '\\/ ' );
291
+ $ class = ltrim (
292
+ implode (
293
+ '\\' ,
294
+ array_map (
295
+ 'pascalize ' ,
296
+ explode ('\\' , str_replace ('/ ' , '\\' , trim ($ class )))
297
+ )
298
+ ),
299
+ '\\/ '
300
+ );
253
301
254
302
// Gets the namespace from input. Don't forget the ending backslash!
255
- $ namespace = trim (str_replace ('/ ' , '\\' , $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE ), '\\' ) . '\\' ;
303
+ $ namespace = trim (
304
+ str_replace (
305
+ '/ ' ,
306
+ '\\' ,
307
+ $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE
308
+ ),
309
+ '\\'
310
+ ) . '\\' ;
256
311
257
312
if (strncmp ($ class , $ namespace , strlen ($ namespace )) === 0 ) {
258
313
return $ class ; // @codeCoverageIgnore
@@ -268,21 +323,41 @@ protected function qualifyClassName(): string
268
323
protected function renderTemplate (array $ data = []): string
269
324
{
270
325
try {
271
- return view (config (Generators::class)->views [$ this ->name ], $ data , ['debug ' => false ]);
326
+ $ template = $ this ->templatePath ?? config (Generators::class)->views [$ this ->name ];
327
+
328
+ return view ($ template , $ data , ['debug ' => false ]);
272
329
} catch (Throwable $ e ) {
273
330
log_message ('error ' , (string ) $ e );
274
331
275
- return view ("CodeIgniter \\Commands \\Generators \\Views \\{$ this ->template }" , $ data , ['debug ' => false ]);
332
+ return view (
333
+ "CodeIgniter \\Commands \\Generators \\Views \\{$ this ->template }" ,
334
+ $ data ,
335
+ ['debug ' => false ]
336
+ );
276
337
}
277
338
}
278
339
279
340
/**
280
341
* Performs pseudo-variables contained within view file.
342
+ *
343
+ * @param string $class namespaced classname or namespaced view.
344
+ *
345
+ * @return string generated file content
281
346
*/
282
- protected function parseTemplate (string $ class , array $ search = [], array $ replace = [], array $ data = []): string
283
- {
347
+ protected function parseTemplate (
348
+ string $ class ,
349
+ array $ search = [],
350
+ array $ replace = [],
351
+ array $ data = []
352
+ ): string {
284
353
// Retrieves the namespace part from the fully qualified class name.
285
- $ namespace = trim (implode ('\\' , array_slice (explode ('\\' , $ class ), 0 , -1 )), '\\' );
354
+ $ namespace = trim (
355
+ implode (
356
+ '\\' ,
357
+ array_slice (explode ('\\' , $ class ), 0 , -1 )
358
+ ),
359
+ '\\'
360
+ );
286
361
$ search [] = '<@php ' ;
287
362
$ search [] = '{namespace} ' ;
288
363
$ search [] = '{class} ' ;
@@ -302,7 +377,14 @@ protected function buildContent(string $class): string
302
377
{
303
378
$ template = $ this ->prepare ($ class );
304
379
305
- if ($ this ->sortImports && preg_match ('/(?P<imports>(?:^use [^;]+;$\n?)+)/m ' , $ template , $ match )) {
380
+ if (
381
+ $ this ->sortImports
382
+ && preg_match (
383
+ '/(?P<imports>(?:^use [^;]+;$\n?)+)/m ' ,
384
+ $ template ,
385
+ $ match
386
+ )
387
+ ) {
306
388
$ imports = explode ("\n" , trim ($ match ['imports ' ]));
307
389
sort ($ imports );
308
390
@@ -314,25 +396,50 @@ protected function buildContent(string $class): string
314
396
315
397
/**
316
398
* Builds the file path from the class name.
399
+ *
400
+ * @param string $class namespaced classname or namespaced view.
317
401
*/
318
402
protected function buildPath (string $ class ): string
319
403
{
320
- $ namespace = trim (str_replace ('/ ' , '\\' , $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE ), '\\' );
404
+ $ namespace = trim (
405
+ str_replace (
406
+ '/ ' ,
407
+ '\\' ,
408
+ $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE
409
+ ),
410
+ '\\'
411
+ );
321
412
322
413
// Check if the namespace is actually defined and we are not just typing gibberish.
323
414
$ base = Services::autoloader ()->getNamespace ($ namespace );
324
415
325
416
if (! $ base = reset ($ base )) {
326
- CLI ::error (lang ('CLI.namespaceNotDefined ' , [$ namespace ]), 'light_gray ' , 'red ' );
417
+ CLI ::error (
418
+ lang ('CLI.namespaceNotDefined ' , [$ namespace ]),
419
+ 'light_gray ' ,
420
+ 'red '
421
+ );
327
422
CLI ::newLine ();
328
423
329
424
return '' ;
330
425
}
331
426
332
427
$ base = realpath ($ base ) ?: $ base ;
333
- $ file = $ base . DIRECTORY_SEPARATOR . str_replace ('\\' , DIRECTORY_SEPARATOR , trim (str_replace ($ namespace . '\\' , '' , $ class ), '\\' )) . '.php ' ;
334
-
335
- return implode (DIRECTORY_SEPARATOR , array_slice (explode (DIRECTORY_SEPARATOR , $ file ), 0 , -1 )) . DIRECTORY_SEPARATOR . $ this ->basename ($ file );
428
+ $ file = $ base . DIRECTORY_SEPARATOR
429
+ . str_replace (
430
+ '\\' ,
431
+ DIRECTORY_SEPARATOR ,
432
+ trim (str_replace ($ namespace . '\\' , '' , $ class ), '\\' )
433
+ ) . '.php ' ;
434
+
435
+ return implode (
436
+ DIRECTORY_SEPARATOR ,
437
+ array_slice (
438
+ explode (DIRECTORY_SEPARATOR , $ file ),
439
+ 0 ,
440
+ -1
441
+ )
442
+ ) . DIRECTORY_SEPARATOR . $ this ->basename ($ file );
336
443
}
337
444
338
445
/**
0 commit comments