itiden / statamic-backup

https://itiden.github.io/statamic-backup/
MIT License
4 stars 0 forks source link

Merge user-defined Stache stores config with default config #32

Closed CedsTrash closed 5 months ago

CedsTrash commented 5 months ago

Hi,

I get this error message when running the User pipe: Itiden\Backup\Support\Zipper::addDirectory(): Argument #1 ($path) must be of type string, null given, called in /statamic/vendor/itiden/statamic-backup/src/Pipes/Users.php on line 33

class Users extends BackupPipe
{

    // Removed code for simplicity

    public function backup(Zipper $zip, Closure $next)
    {
        $zip->addDirectory(config('statamic.stache.stores.users.directory'), static::getKey());

        return $next($zip);
    }
}

I found out where this is coming from: config('statamic.stache.stores.users.directory') get the user-defined stache stores but doesn't handle the fact that only some custom stores might have been set.

This is my config file:

'stores' => [
    'taxonomies' => [
        'directory' => config_path('statamic_content/taxonomies'),
    ],
    'collections' => [
        'directory' => config_path('statamic_content/collections'),
    ],
    'navigation' => [
        'directory' => config_path('statamic_content/navigation'),
    ],
    'asset-containers' => [
        'directory' => config_path('statamic_content/assets'),
    ],
],

As you can see, I didn't override the users store so config('statamic.stache.stores.users.directory') returns null.

If you look at how Statamic handles it when registering the stores, you can see that it merges the default config with the user-defined config:

private function registerStores($stache)
{
    // Merge the stores from our config file with the stores in the user's published
    // config file. If we ever need to add more stores, they won't need to add them.
    $config = require __DIR__.'/../../config/stache.php';
    $published = config('statamic.stache.stores');

    $nativeStores = collect($config['stores'])
        ->reject(fn ($config, $key) => $key === 'users' && config('statamic.users.repository') !== 'file')
        ->map(function ($config, $key) use ($published) {
            return array_merge($config, $published[$key] ?? []);
        });

    // Merge in any user defined stores that aren't native.
    $stores = $nativeStores->merge(collect($published)->diffKeys($nativeStores));
    Log::info($stores);

    $stores = $stores->map(function ($config) {
        return app($config['class'])->directory($config['directory'] ?? null);
    });

    $stache->registerStores($stores->all());
}

I think this is how this package should handle it too.