Skip to content

Commit 945960c

Browse files
authored
Merge pull request #2052 from solomon-ochepa/patch-61
Enhance Config Merging Logic to Prevent Duplicate Keys in Modules
2 parents c4dd018 + b63b31c commit 945960c

12 files changed

+180
-72
lines changed

src/Commands/stubs/scaffold/provider.stub

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class $CLASS$ extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generate_module_when_provider_is_enable_and_route_provider_is_disable__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generate_module_when_provider_is_enable_and_route_provider_is_enable__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_api_module_with_resources__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_module_namespace_using_studly_case__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class ModuleNameServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_module_resources__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_web_module_with_resources__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ModuleMakeCommandTest__test_it_generates_web_module_with_resources_when_adding_more_than_one_option__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ProviderMakeCommandTest__test_it_can_change_the_default_namespace__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

tests/Commands/Make/__snapshots__/ProviderMakeCommandTest__test_it_can_change_the_default_namespace_specific__1.txt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@ class BlogServiceProvider extends ServiceProvider
7878
*/
7979
protected function registerConfig(): void
8080
{
81-
$relativeConfigPath = config('modules.paths.generator.config.path');
82-
$configPath = module_path($this->name, $relativeConfigPath);
81+
$configPath = module_path($this->name, config('modules.paths.generator.config.path'));
8382

8483
if (is_dir($configPath)) {
8584
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($configPath));
8685

8786
foreach ($iterator as $file) {
8887
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;
88+
$config = str_replace($configPath.DIRECTORY_SEPARATOR, '', $file->getPathname());
89+
$config_key = str_replace([DIRECTORY_SEPARATOR, '.php'], ['.', ''], $config);
90+
$segments = explode('.', $this->nameLower.'.'.$config_key);
9291

93-
$this->publishes([$file->getPathname() => config_path($relativePath)], 'config');
92+
// Remove duplicated adjacent segments
93+
$normalized = [];
94+
foreach ($segments as $segment) {
95+
if (end($normalized) !== $segment) {
96+
$normalized[] = $segment;
97+
}
98+
}
99+
100+
$key = ($config === 'config.php') ? $this->nameLower : implode('.', $normalized);
101+
102+
$this->publishes([$file->getPathname() => config_path($config)], 'config');
94103
$this->mergeConfigFrom($file->getPathname(), $key);
95104
}
96105
}

0 commit comments

Comments
 (0)