Seeding Extension Configurations


Introduction

Many extensions have configurable settings that aren't clear on how to seed at first glance. We will be looking at anomaly.extension.local_storage_adapter in this example.

The issue

When you are setting up a disk using the local storage adapter in the files module, you will see a toggle for Store files privately? at the bottom of the page. At first glance it would be assumed that this is just a normal setting stored in the general files stream.

When you seed a disk you need to specify an array for each language for name and description, but you'll notice that there isn't a way to seed that you want this disk to be private through the create array.

$disk = $this->disks->create(
            [
                    'en'      => [
                            'name'        => 'Private Local Disk',
                            'description' => 'A local (private) storage disk.',
                    ],
                    'slug'    => $this->diskSlug,
                    'adapter' => 'anomaly.extension.local_storage_adapter',
            ]
    );

Seeding the configuration

If you look at Load Disk you will see that the private flag is actually stored in the configuration stream.

In order to set this you are going to need to use the ConfigurationRepository to find the configuration key and scope, set the value, and save it.

The ConfigurationRepository has the function findByKeyAndScopeOrNew('key','slug'), returning an exsisting model, or a new instance if it didn't exsist.

The key in this case can be found in the LoadDisk class, the slug would be the slug of your disk.

You can alternatively find keys and type of keys by looking at the configuration.php of the extension in question.

Once you have the configuration, it is as simple as setting the value, and saving. (Note: we are setting the value as 1, as the key is of type boolean

$this->configuration->findByKeyAndScopeOrNew('anomaly.extension.local_storage_adapter::private',$this->diskSlug)->setValue(1)->save();`

All together this would like like this

class DiskSeeder extends Seeder
{

    /**
     * The $disks repository.
     *
     * @var DiskRepository
     */
    protected $disks;

        /**
     * The slug for our disk
     * 
     * @var string
     */
    protected $diskSlug = 'private_disk';

    /**
     * @var ConfigurationRepository
     */
    protected $configuration;

    /**
     * DiskSeeder constructor.
     *
     * @param DiskRepositoryInterface $disks
     * @param ConfigurationRepositoryInterface $folders
     */
    public function __construct(DiskRepositoryInterface $disks, ConfigurationRepositoryInterface $configuration)
    {
        $this->disks = $disks;
        $this->configuration = $configuration;
    }

    /**
     * Seed the disk.
     */
    public function run()
    {
        $disk = $this->disks->findBySlug($this->diskSlug);

        //Check for the Disk
        if (!$disk) {
            $disk = $this->disks
                ->create(
                    [
                        'en'      => [
                            'name'        => 'Private Local Disk',
                            'description' => 'A local (private) storage disk.',
                        ],
                        'slug'    => $this->diskSlug,
                        'adapter' => 'anomaly.extension.local_storage_adapter',
                    ]
                );
        }

        //Mark it as private
        $this->configuration->findByKeyAndScopeOrNew('anomaly.extension.local_storage_adapter::private',$this->diskSlug)->setValue(1)->save();
    }
}