Skip to content

Commit 71ac71c

Browse files
authored
Merge pull request #2053 from solomon-ochepa/feature/module-config-override
Allow Module Configs to Override Root Application Configs
2 parents 4c09752 + ca06cde commit 71ac71c

12 files changed

+156
-25
lines changed

src/Commands/stubs/scaffold/provider.stub

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class $CLASS$ extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generate_module_when_provider_is_enable_and_route_provider_is_disable__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generate_module_when_provider_is_enable_and_route_provider_is_enable__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_api_module_with_resources__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_module_namespace_using_studly_case__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class ModuleNameServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_module_resources__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_web_module_with_resources__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_web_module_with_resources_when_adding_more_than_one_option__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ProviderMakeCommandTest__test_it_can_change_the_default_namespace__1.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,24 @@ class BlogServiceProvider extends ServiceProvider
9999

100100
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101101

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
102+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
103+
$this->merge_config_from($file->getPathname(), $key);
104104
}
105105
}
106106
}
107107
}
108108

109+
/**
110+
* Merge config from the given path recursively.
111+
*/
112+
protected function merge_config_from(string $path, string $key): void
113+
{
114+
$existing = config($key, []);
115+
$module_config = require $path;
116+
117+
config([$key => array_replace_recursive($existing, $module_config)]);
118+
}
119+
109120
/**
110121
* Register views.
111122
*/

tests/Commands/Make/__snapshots__/ProviderMakeCommandTest__test_it_can_change_the_default_namespace_specific__1.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class BlogServiceProvider extends ServiceProvider
8888
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
8989
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
9090
$segments = explode('.', $this->nameLower.'.'.$config_key);
91-
9291
// Remove duplicated adjacent segments
9392
$normalized = [];
9493
foreach ($segments as $segment) {
@@ -99,13 +98,24 @@ class BlogServiceProvider extends ServiceProvider
9998

10099
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101100

102-
$this->publishes([$file->getPathname() => config_path($config)], 'config');
103-
$this->mergeConfigFrom($file->getPathname(), $key);
101+
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
102+
$this->merge_config_from($file->getPathname(), $key);
104103
}
105104
}
106105
}
107106
}
108107

108+
/**
109+
* Merge config from the given path recursively.
110+
*/
111+
protected function merge_config_from(string $path, string $key): void
112+
{
113+
$existing = config($key, []);
114+
$module_config = require $path;
115+
116+
config([$key => array_replace_recursive($existing, $module_config)]);
117+
}
118+
109119
/**
110120
* Register views.
111121
*/

0 commit comments

Comments
 (0)