Skip to content

Commit

Permalink
Importer Improvements (#269)
Browse files Browse the repository at this point in the history
Co-authored-by: duncanmcclean <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored May 2, 2024
1 parent c341573 commit b3b2d76
Show file tree
Hide file tree
Showing 27 changed files with 1,646 additions and 222 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"require-dev": {
"doctrine/dbal": "^3.3",
"laravel/pint": "^1.0",
"orchestra/testbench": "^8.0 || ^9.0",
"orchestra/testbench": "^8.0 || ^9.0.2",
"phpunit/phpunit": "^9.4 || ^10.0 || ^11.0"
},
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions src/Assets/AssetContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Statamic\Assets\AssetContainer as FileEntry;
use Statamic\Contracts\Assets\AssetContainer as AssetContainerContract;
use Statamic\Events\AssetContainerDeleted;
use Statamic\Events\AssetContainerSaved;
use Statamic\Support\Str;
Expand Down Expand Up @@ -76,6 +77,34 @@ public function toModel()
]);
}

public static function makeModelFromContract(AssetContainerContract $source)
{
$model = app('statamic.eloquent.assets.container_model')::firstOrNew(['handle' => $source->handle()])->fill([
'title' => $source->title(),
'disk' => $source->diskHandle() ?? config('filesystems.default'),
'settings' => [
'allow_uploads' => $source->allowUploads(),
'allow_downloading' => $source->allowDownloading(),
'allow_moving' => $source->allowMoving(),
'allow_renaming' => $source->allowRenaming(),
'create_folders' => $source->createFolders(),
'search_index' => $source->searchIndex(),
'source_preset' => $source->sourcePreset,
'warm_presets' => $source->warmPresets,
],
]);

// Set initial timestamps.
if (empty($model->created_at) && isset($meta['last_modified'])) {
$model->created_at = $source->fileLastModified();
$model->updated_at = $source->fileLastModified();
}

$model->save();

return $model;
}

public function model($model = null)
{
if (func_num_args() === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ExportAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ExportAssets extends Command
*
* @var string
*/
protected $signature = 'statamic:eloquent:export-assets {--force : Force the operation to run, with all questions yes}';
protected $signature = 'statamic:eloquent:export-assets {--force : Force the export to run, with all prompts answered "yes"}';

/**
* The console command description.
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ExportCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExportCollections extends Command
*
* @var string
*/
protected $signature = 'statamic:eloquent:export-collections {--force : Force the operation to run, with all questions yes}';
protected $signature = 'statamic:eloquent:export-collections {--force : Force the export to run, with all prompts answered "yes"}';

/**
* The console command description.
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ExportNavs.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ExportNavs extends Command
*
* @var string
*/
protected $signature = 'statamic:eloquent:export-navs {--force : Force the operation to run, with all questions yes}';
protected $signature = 'statamic:eloquent:export-navs {--force : Force the export to run, with all prompts answered "yes"}';

/**
* The console command description.
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ExportTaxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExportTaxonomies extends Command
*
* @var string
*/
protected $signature = 'statamic:eloquent:export-taxonomies {--force : Force the operation to run, with all questions yes}';
protected $signature = 'statamic:eloquent:export-taxonomies {--force : Force the export to run, with all prompts answered "yes"}';

/**
* The console command description.
Expand Down
59 changes: 31 additions & 28 deletions src/Commands/ImportAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Statamic\Assets\AssetContainerContents;
use Statamic\Assets\AssetRepository;
use Statamic\Console\RunsInPlease;
use Statamic\Contracts\Assets\Asset;
use Statamic\Contracts\Assets\AssetContainer as AssetContainerContract;
use Statamic\Contracts\Assets\AssetContainerRepository as AssetContainerRepositoryContract;
use Statamic\Contracts\Assets\AssetRepository as AssetRepositoryContract;
Expand All @@ -27,21 +26,22 @@ class ImportAssets extends Command
*
* @var string
*/
protected $signature = 'statamic:eloquent:import-assets {--force : Force the operation to run, with all questions yes}';
protected $signature = 'statamic:eloquent:import-assets
{--force : Force the import to run, with all prompts answered "yes"}
{--only-asset-containers : Only import asset containers}
{--only-assets : Only import assets}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports file based asset containers into the database.';
protected $description = 'Imports file-based asset containers & asset metadata into the database.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
public function handle(): int
{
$this->useDefaultRepositories();

Expand All @@ -51,7 +51,7 @@ public function handle()
return 0;
}

private function useDefaultRepositories()
private function useDefaultRepositories(): void
{
Facade::clearResolvedInstance(AssetContainerRepositoryContract::class);
Facade::clearResolvedInstance(AssetRepositoryContract::class);
Expand All @@ -60,43 +60,46 @@ private function useDefaultRepositories()
Statamic::repository(AssetRepositoryContract::class, AssetRepository::class);

app()->bind(AssetContainerContract::class, AssetContainer::class);
app()->bind(AssetContract::class, Asset::class);

app()->bind(AssetContainerContents::class, function ($app) {
return new AssetContainerContents();
});
app()->bind(AssetContainerContents::class, fn ($app) => new AssetContainerContents());
}

private function importAssetContainers()
private function importAssetContainers(): void
{
if (! $this->option('force') && ! $this->confirm('Do you want to import asset containers?')) {
if (! $this->shouldImportAssetContainers()) {
return;
}

$containers = AssetContainerFacade::all();

$this->withProgressBar($containers, function ($container) {
$lastModified = $container->fileLastModified();
$container->toModel()->fill(['created_at' => $lastModified, 'updated_at' => $lastModified])->save();
$this->withProgressBar(AssetContainerFacade::all(), function ($container) {
AssetContainer::makeModelFromContract($container);
});

$this->line('');
$this->info('Asset containers imported');
$this->components->info('Assets containers imported sucessfully');
}

private function importAssets()
private function importAssets(): void
{
if (! $this->option('force') && ! $this->confirm('Do you want to import assets?')) {
if (! $this->shouldImportAssets()) {
return;
}

$assets = AssetFacade::all();

$this->withProgressBar($assets, function ($asset) {
$this->withProgressBar(AssetFacade::all(), function ($asset) {
EloquentAsset::makeModelFromContract($asset);
});

$this->newLine();
$this->info('Assets imported');
$this->components->info('Assets imported sucessfully');
}

private function shouldImportAssetContainers(): bool
{
return $this->option('only-asset-containers')
|| ! $this->option('only-assets')
&& ($this->option('force') || $this->confirm('Do you want to import asset containers?'));
}

private function shouldImportAssets(): bool
{
return $this->option('only-assets')
|| ! $this->option('only-asset-containers')
&& ($this->option('force') || $this->confirm('Do you want to import assets?'));
}
}
51 changes: 20 additions & 31 deletions src/Commands/ImportBlueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ class ImportBlueprints extends Command
*
* @var string
*/
protected $description = 'Imports file based blueprints and fieldsets into the database.';
protected $description = 'Imports file-based blueprints & fieldsets into the database.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
public function handle(): int
{
$this->useDefaultRepositories();

Expand All @@ -46,7 +44,7 @@ public function handle()
return 0;
}

private function useDefaultRepositories()
private function useDefaultRepositories(): void
{
Facade::clearResolvedInstance(\Statamic\Fields\BlueprintRepository::class);
Facade::clearResolvedInstance(\Statamic\Fields\FieldsetRepository::class);
Expand All @@ -62,7 +60,7 @@ private function useDefaultRepositories()
);
}

private function importBlueprints()
private function importBlueprints(): void
{
$directory = resource_path('blueprints');

Expand Down Expand Up @@ -104,25 +102,21 @@ private function importBlueprints()
->setHandle($handle)
->setNamespace($namespace ?? null)
->setContents($contents);

$lastModified = Carbon::createFromTimestamp(File::lastModified($path));

$model = app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
'handle' => $blueprint->handle(),
app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
'handle' => $blueprint->handle(),
'namespace' => $blueprint->namespace() ?? null,
])->fill([
'data' => $blueprint->contents(),
'created_at' => $lastModified,
'updated_at' => $lastModified,
]);

$model->save();
])
->fill(['data' => $blueprint->contents(), 'created_at' => $lastModified, 'updated_at' => $lastModified])
->save();
});

$this->newLine();
$this->info('Blueprints imported');
$this->components->info('Blueprints imported successfully.');
}

private function importFieldsets()
private function importFieldsets(): void
{
$directory = resource_path('fieldsets');

Expand All @@ -134,26 +128,21 @@ private function importFieldsets()
$handle = Str::before($basename, '.yaml');
$handle = str_replace('/', '.', $handle);

$fieldset = Fieldset::make($handle)
->setContents(YAML::file($path)->parse());
$fieldset = Fieldset::make($handle)->setContents(YAML::file($path)->parse());

$lastModified = Carbon::createFromTimestamp(File::lastModified($path));

$model = app('statamic.eloquent.blueprints.fieldset_model')::firstOrNew([
app('statamic.eloquent.blueprints.fieldset_model')::firstOrNew([
'handle' => $fieldset->handle(),
])->fill([
'data' => $fieldset->contents(),
'created_at' => $lastModified,
'updated_at' => $lastModified,
]);

$model->save();
])
->fill(['data' => $fieldset->contents(), 'created_at' => $lastModified, 'updated_at' => $lastModified])
->save();
});

$this->newLine();
$this->info('Fieldsets imported');
$this->components->info('Fieldsets imported successfully.');
}

private function getNamespaceAndHandle($blueprint)
private function getNamespaceAndHandle(string $blueprint): array
{
$blueprint = str_replace('/', '.', $blueprint);
$parts = explode('.', $blueprint);
Expand Down
Loading

0 comments on commit b3b2d76

Please sign in to comment.