Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] merge published config with package config #1873

Merged
merged 2 commits into from
Jan 27, 2025
Merged

Conversation

pxpm
Copy link
Contributor

@pxpm pxpm commented Jan 23, 2025

There is currently a bug on the way the configs are merged. If a "nested array key" is missing on the published config file, array_merge() wouldn't add that key and the application will fail to properly boot the config.

I got the following error after updating from v8 to v9 - Undefined array key "compression_method".

I dug a bit on it, and indeed my published backup.php config file didn't had that key, but that key is present on the package config file:

'compression_method' => ZipArchive::CM_DEFAULT,
so I would expect the "default package value" to be used.

This PR changes array_merge to array_replace_recursive, so that we use the package config as the base, and replace any values from the published config file.

I've added tests for both scenarios (without keys present in the published config file, and with keys present in the config file).

@freekmurze any chance you can take a look at this ?

Cheers

@freekmurze freekmurze merged commit 074e35f into spatie:main Jan 27, 2025
1 check passed
@freekmurze
Copy link
Member

Thanks!

@cannahan
Copy link

After this update, disabling notifications in the backup.php configuration file using the following line is no longer possible: “\Spatie\Backup\Notifications\Notifications\BackupWasSuccessfulNotification::class => [],” because the empty array is not recognized as an actual value by the array_replace_recursive() function, but as an additional array.

@mho22
Copy link
Contributor

mho22 commented Feb 1, 2025

@pxpm This is why I didn't use array_replace_recursive in my previous pull request. You can't overwrite array with data with an empty array using array_replace_recursive like @cannahan mentionned :

array_replace_recursive() replaces the values of array with the same values from all the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.

The inconvenience of my PR is that you still have to add some nested array keys even if you don't need them but overall most of them are no more needed.

Edit : I just found your #1880 PR. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants