Skip to content

Commit e56a298

Browse files
authored
Merge pull request #2040 from alissn/AddDynamicReplacementStub
Add Support for Closures in Stub Variable Replacement
2 parents 0f2324f + 5b8c42c commit e56a298

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

config/config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@
3939
'package' => 'package.json',
4040
],
4141
'replacements' => [
42+
/**
43+
* Define custom replacements for each section.
44+
* You can specify a closure for dynamic values.
45+
*
46+
* Example:
47+
*
48+
* 'composer' => [
49+
* 'CUSTOM_KEY' => fn (\Nwidart\Modules\Generators\ModuleGenerator $generator) => $generator->getModule()->getLowerName() . '-module',
50+
* 'CUSTOM_KEY2' => fn () => 'custom text',
51+
* 'LOWER_NAME',
52+
* 'STUDLY_NAME',
53+
* // ...
54+
* ],
55+
*
56+
* Note: Keys should be in UPPERCASE.
57+
*/
4258
'routes/web' => ['LOWER_NAME', 'STUDLY_NAME', 'PLURAL_LOWER_NAME', 'KEBAB_NAME', 'MODULE_NAMESPACE', 'CONTROLLER_NAMESPACE'],
4359
'routes/api' => ['LOWER_NAME', 'STUDLY_NAME', 'PLURAL_LOWER_NAME', 'KEBAB_NAME', 'MODULE_NAMESPACE', 'CONTROLLER_NAMESPACE'],
4460
'vite' => ['LOWER_NAME', 'STUDLY_NAME', 'KEBAB_NAME'],

src/Generators/ModuleGenerator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,19 @@ protected function getReplacement($stub): array
486486
}
487487
}
488488

489-
foreach ($keys as $key) {
490-
if (method_exists($this, $method = 'get'.ucfirst(Str::studly(strtolower($key))).'Replacement')) {
489+
foreach ($keys as $key => $value) {
490+
if ($value instanceof \Closure) {
491+
$replaces[strtoupper($key)] = $value($this);
492+
} elseif (method_exists($this, $method = 'get'.ucfirst(Str::studly(strtolower($value))).'Replacement')) {
491493
$replace = $this->$method();
492494

493495
if ($stub === 'routes/web' || $stub === 'routes/api') {
494496
$replace = str_replace('\\\\', '\\', $replace);
495497
}
496498

497-
$replaces[$key] = $replace;
499+
$replaces[$value] = $replace;
498500
} else {
499-
$replaces[$key] = null;
501+
$replaces[$value] = null;
500502
}
501503
}
502504

0 commit comments

Comments
 (0)