Skip to content

Commit 757175e

Browse files
committed
add support for multiple config files and nested config files
1 parent 57734a0 commit 757175e

12 files changed

+228
-24
lines changed

src/Commands/stubs/scaffold/provider.stub

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace $NAMESPACE$;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class $CLASS$ extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class $CLASS$ extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, '$PATH_CONFIG$/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, '$PATH_CONFIG$/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generate_module_when_provider_is_enable_and_route_provider_is_disable__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generate_module_when_provider_is_enable_and_route_provider_is_enable__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_api_module_with_resources__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_module_namespace_using_studly_case__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\ModuleName\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class ModuleNameServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class ModuleNameServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_module_resources__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_web_module_with_resources__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_web_module_with_resources_when_adding_more_than_one_option__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\Providers;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ProviderMakeCommandTest__test_it_can_change_the_default_namespace__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\SuperProviders;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

tests/Commands/Make/__snapshots__/ProviderMakeCommandTest__test_it_can_change_the_default_namespace_specific__1.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Modules\Blog\SuperProviders;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\ServiceProvider;
77
use Nwidart\Modules\Traits\PathNamespace;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
810

911
class BlogServiceProvider extends ServiceProvider
1012
{
@@ -76,8 +78,23 @@ class BlogServiceProvider extends ServiceProvider
7678
*/
7779
protected function registerConfig(): void
7880
{
79-
$this->publishes([module_path($this->name, 'config/config.php') => config_path($this->nameLower.'.php')], 'config');
80-
$this->mergeConfigFrom(module_path($this->name, 'config/config.php'), $this->nameLower);
81+
$relativeConfigPath = config('modules.paths.generator.config.path');
82+
$configPath = module_path($this->name, $relativeConfigPath);
83+
84+
if (is_dir($configPath)) {
85+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
86+
87+
foreach ($iterator as $file) {
88+
if ($file->isFile() && $file->getExtension() === 'php') {
89+
$relativePath = str_replace($configPath . DIRECTORY_SEPARATOR, '', $file->getPathname());
90+
$configKey = $this->nameLower . '.' . str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $relativePath);
91+
$key = ($relativePath === 'config.php') ? $this->nameLower : $configKey;
92+
93+
$this->publishes([$file->getPathname() => config_path($relativePath)], $configPath);
94+
$this->mergeConfigFrom($file->getPathname(), $key);
95+
}
96+
}
97+
}
8198
}
8299

83100
/**

0 commit comments

Comments
 (0)