Create Disk & Folder for Files module using a seeder
Created 6 years ago by araminho

I would like to create Disk & Folder for Files module using a seeder. And, if possible, upload some files.

I am using "S3" adapter.

araminho  —  6 years ago Best Answer

In case if anyone needs this, I am copying the working code here. It's a migration.

NOTE. If you are not using S3 adapter, see William's answer for simple local disk.

public function up()
    {
        $disks = app(DiskRepositoryInterface::class);
        $configuration = app(ConfigurationRepositoryInterface::class);

        $config = config('filesystems.disks.s3');
        $disk = $disks->findBySlug('jumpramp_cms');
        if ($disk) {
            // $disk->forceDelete();
            $disk->name  = 'JumpRamp CMS';
            $disk->description = 'JumpRamp CMS disk in S3';
            $disk->adapter = 'anomaly.extension.s3_adapter';
            $disk->save();
        }
        else {
            $disk = $disks->create([
                'en'      => [
                    'name'        => 'JumpRamp CMS',
                    'description' => 'JumpRamp CMS disk in S3',
                ],
                'slug'    => 'jumpramp_cms',
                'adapter' => 'anomaly.extension.s3_adapter',
            ]);
        }

        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::access_key',
            'value' => $config['key'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::secret_key',
            'value' => $config['secret'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::region',
            'value' => $config['region'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::bucket',
            'value' => $config['bucket'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::prefix',
            'value' => $config['prefix'],
        ]);

    }

And here is the content of 'config/filesystems.php'. The config variables are coming from .env file, which I will not copy here for obvious reasons 😄


        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('S3_ACCESS_KEY'),
            'secret' => env('S3_SECRET_KEY'),
            'region' => env('S3_REGION'),
            'bucket' => env('S3_BUCKET'),
            'prefix' => env('S3_PREFIX', true),
        ],

    ],
william  —  6 years ago

Hello, sorry for a late reply.

You could do something like this:

<?php

use Illuminate\Database\Seeder;
use Anomaly\FilesModule\Disk\Contract\DiskRepositoryInterface;
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface;

class FolderSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(DiskRepositoryInterface $disks, FolderRepositoryInterface $folders)
    {

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

        $folders->create(
            [
                'en'            => [
                    'name'        => 'Images',
                    'description' => 'A folder for images.',
                ],
                'slug'          => 'images',
                'disk'          => $disk,
                'allowed_types' => [
                    'jpeg',
                    'jpg',
                ],
            ]
        );
    }
}
araminho  —  6 years ago

Hi William. Thank you for your answer. It was helpful for creating a disk with local storage adapter. But how can I create a disk using S3 adapter? It has some more parameters that I'll need to pass somehow - "Access Key", "Secret Key", "Region" and "Bucket". How should I indicate those parameters?

ryanthompson  —  6 years ago

You will need to checkout the extension's config/configuration.php file to determine what's required for "configuration" and then populate configuration for said extension instance. Here's an example of how the Dashboard module seeds a widget using the XML feed widget extension:

https://github.com/anomalylabs/dashboard-module/blob/2.2/src/Widget/WidgetSeeder.php

Pay particular attention to the population of the configuration for the instance of the extension being used: https://github.com/anomalylabs/dashboard-module/blob/2.2/src/Widget/WidgetSeeder.php#L79

araminho  —  6 years ago Best Answer

In case if anyone needs this, I am copying the working code here. It's a migration.

NOTE. If you are not using S3 adapter, see William's answer for simple local disk.

public function up()
    {
        $disks = app(DiskRepositoryInterface::class);
        $configuration = app(ConfigurationRepositoryInterface::class);

        $config = config('filesystems.disks.s3');
        $disk = $disks->findBySlug('jumpramp_cms');
        if ($disk) {
            // $disk->forceDelete();
            $disk->name  = 'JumpRamp CMS';
            $disk->description = 'JumpRamp CMS disk in S3';
            $disk->adapter = 'anomaly.extension.s3_adapter';
            $disk->save();
        }
        else {
            $disk = $disks->create([
                'en'      => [
                    'name'        => 'JumpRamp CMS',
                    'description' => 'JumpRamp CMS disk in S3',
                ],
                'slug'    => 'jumpramp_cms',
                'adapter' => 'anomaly.extension.s3_adapter',
            ]);
        }

        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::access_key',
            'value' => $config['key'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::secret_key',
            'value' => $config['secret'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::region',
            'value' => $config['region'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::bucket',
            'value' => $config['bucket'],
        ]);
        $configuration->create([
            'scope' => $disk->getSlug(),
            'key'   => 'anomaly.extension.s3_adapter::prefix',
            'value' => $config['prefix'],
        ]);

    }

And here is the content of 'config/filesystems.php'. The config variables are coming from .env file, which I will not copy here for obvious reasons 😄


        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('S3_ACCESS_KEY'),
            'secret' => env('S3_SECRET_KEY'),
            'region' => env('S3_REGION'),
            'bucket' => env('S3_BUCKET'),
            'prefix' => env('S3_PREFIX', true),
        ],

    ],