@@ -59,6 +59,12 @@ trait GeneratorTrait
59
59
*/
60
60
protected $ classNameLang = '' ;
61
61
62
+ /**
63
+ * Namespace to use for class.
64
+ * Leave null to use the default namespace.
65
+ */
66
+ protected ?string $ namespace = null ;
67
+
62
68
/**
63
69
* Whether to require class name.
64
70
*
@@ -108,7 +114,7 @@ protected function execute(array $params): void
108
114
/**
109
115
* Generates a class file from an existing template.
110
116
*/
111
- protected function generateClass (array $ params )
117
+ protected function generateClass (array $ params ): void
112
118
{
113
119
$ this ->params = $ params ;
114
120
@@ -119,7 +125,7 @@ protected function generateClass(array $params)
119
125
$ target = $ this ->buildPath ($ class );
120
126
121
127
// Check if path is empty.
122
- if (empty ( $ target) ) {
128
+ if ($ target === '' ) {
123
129
return ;
124
130
}
125
131
@@ -131,14 +137,14 @@ protected function generateClass(array $params)
131
137
*
132
138
* @param string $view namespaced view name that is generated
133
139
*/
134
- protected function generateView (string $ view , array $ params )
140
+ protected function generateView (string $ view , array $ params ): void
135
141
{
136
142
$ this ->params = $ params ;
137
143
138
144
$ target = $ this ->buildPath ($ view );
139
145
140
146
// Check if path is empty.
141
- if (empty ( $ target) ) {
147
+ if ($ target === '' ) {
142
148
return ;
143
149
}
144
150
@@ -263,8 +269,10 @@ protected function qualifyClassName(): string
263
269
264
270
if ($ class === null && $ this ->hasClassName ) {
265
271
// @codeCoverageIgnoreStart
266
- $ nameLang = $ this ->classNameLang ?: 'CLI.generator.className.default ' ;
267
- $ class = CLI ::prompt (lang ($ nameLang ), null , 'required ' );
272
+ $ nameLang = $ this ->classNameLang !== ''
273
+ ? $ this ->classNameLang
274
+ : 'CLI.generator.className.default ' ;
275
+ $ class = CLI ::prompt (lang ($ nameLang ), null , 'required ' );
268
276
CLI ::newLine ();
269
277
// @codeCoverageIgnoreEnd
270
278
}
@@ -302,20 +310,15 @@ protected function qualifyClassName(): string
302
310
);
303
311
304
312
// Gets the namespace from input. Don't forget the ending backslash!
305
- $ namespace = trim (
306
- str_replace (
307
- '/ ' ,
308
- '\\' ,
309
- $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE
310
- ),
311
- '\\'
312
- ) . '\\' ;
313
+ $ namespace = $ this ->getNamespace () . '\\' ;
313
314
314
315
if (strncmp ($ class , $ namespace , strlen ($ namespace )) === 0 ) {
315
316
return $ class ; // @codeCoverageIgnore
316
317
}
317
318
318
- return $ namespace . $ this ->directory . '\\' . str_replace ('/ ' , '\\' , $ class );
319
+ $ directoryString = ($ this ->directory !== null ) ? $ this ->directory . '\\' : '' ;
320
+
321
+ return $ namespace . $ directoryString . str_replace ('/ ' , '\\' , $ class );
319
322
}
320
323
321
324
/**
@@ -403,14 +406,7 @@ protected function buildContent(string $class): string
403
406
*/
404
407
protected function buildPath (string $ class ): string
405
408
{
406
- $ namespace = trim (
407
- str_replace (
408
- '/ ' ,
409
- '\\' ,
410
- $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE
411
- ),
412
- '\\'
413
- );
409
+ $ namespace = $ this ->getNamespace ();
414
410
415
411
// Check if the namespace is actually defined and we are not just typing gibberish.
416
412
$ base = Services::autoloader ()->getNamespace ($ namespace );
@@ -426,7 +422,9 @@ protected function buildPath(string $class): string
426
422
return '' ;
427
423
}
428
424
429
- $ base = realpath ($ base ) ?: $ base ;
425
+ $ realpath = realpath ($ base );
426
+ $ base = ($ realpath !== false ) ? $ realpath : $ base ;
427
+
430
428
$ file = $ base . DIRECTORY_SEPARATOR
431
429
. str_replace (
432
430
'\\' ,
@@ -444,6 +442,23 @@ protected function buildPath(string $class): string
444
442
) . DIRECTORY_SEPARATOR . $ this ->basename ($ file );
445
443
}
446
444
445
+ /**
446
+ * Gets the namespace from the command-line option,
447
+ * or the default namespace if the option is not set.
448
+ * Can be overridden by directly setting $this->namespace.
449
+ */
450
+ protected function getNamespace (): string
451
+ {
452
+ return $ this ->namespace ?? trim (
453
+ str_replace (
454
+ '/ ' ,
455
+ '\\' ,
456
+ $ this ->getOption ('namespace ' ) ?? APP_NAMESPACE
457
+ ),
458
+ '\\'
459
+ );
460
+ }
461
+
447
462
/**
448
463
* Allows child generators to modify the internal `$hasClassName` flag.
449
464
*
@@ -483,10 +498,8 @@ protected function setEnabledSuffixing(bool $enabledSuffixing)
483
498
/**
484
499
* Gets a single command-line option. Returns TRUE if the option exists,
485
500
* but doesn't have a value, and is simply acting as a flag.
486
- *
487
- * @return mixed
488
501
*/
489
- protected function getOption (string $ name )
502
+ protected function getOption (string $ name ): string | bool | null
490
503
{
491
504
if (! array_key_exists ($ name , $ this ->params )) {
492
505
return CLI ::getOption ($ name );
0 commit comments