Skip to content

Commit

Permalink
Some tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Apr 11, 2024
1 parent 24320ee commit 53a53d7
Show file tree
Hide file tree
Showing 22 changed files with 591 additions and 320 deletions.
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
58 changes: 27 additions & 31 deletions src/Commands/ImportAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ImportAssets extends Command
* @var string
*/
protected $signature = 'statamic:eloquent:import-assets
{--force : Force the operation to run, with all questions yes}
{--force : Force the import to run, with all prompts answered "yes"}
{--only-asset-containers : Only import asset containers}
{--only-assets : Only import assets}';

Expand All @@ -37,14 +37,14 @@ class ImportAssets extends Command
*
* @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 @@ -54,7 +54,7 @@ public function handle()
return 0;
}

private function useDefaultRepositories()
private function useDefaultRepositories(): void
{
Facade::clearResolvedInstance(AssetContainerRepositoryContract::class);
Facade::clearResolvedInstance(AssetRepositoryContract::class);
Expand All @@ -63,50 +63,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('only-assets')) {
return;
}

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

$containers = AssetContainerFacade::all();

$this->withProgressBar($containers, function ($container) {
$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('only-asset-containers')) {
return;
}

if (! $this->option('only-assets') && ! $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?'));
}
}
49 changes: 20 additions & 29 deletions src/Commands/ImportBlueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ 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 +46,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 +62,7 @@ private function useDefaultRepositories()
);
}

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

Expand Down Expand Up @@ -104,25 +104,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 +130,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
49 changes: 25 additions & 24 deletions src/Commands/ImportCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ImportCollections extends Command
* @var string
*/
protected $signature = 'statamic:eloquent:import-collections
{--force : Force the operation to run, with all questions yes}
{--force : Force the import to run, with all prompts answered "yes"}
{--only-collections : Only import collections}
{--only-collection-trees : Only import collection trees}';

Expand All @@ -35,28 +35,27 @@ class ImportCollections extends Command
*
* @var string
*/
protected $description = 'Imports file based collections into the database.';
protected $description = 'Imports file-based collections & collection trees into the database.';

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

$this->updateEntryOrder();

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

return 0;
}

private function usingDefaultRepositories(Closure $callback)
private function usingDefaultRepositories(Closure $callback): void
{
$originalRepo = get_class(app()->make(CollectionRepositoryContract::class));
$originalTreeRepo = get_class(app()->make(CollectionTreeRepositoryContract::class));
Expand All @@ -79,33 +78,21 @@ private function usingDefaultRepositories(Closure $callback)
Facade::clearResolvedInstance(CollectionTreeRepositoryContract::class);
}

private function importCollections()
private function importCollections(): void
{
if ($this->option('only-collections') || $this->option('only-collection-trees')) {
$importCollections = $this->option('only-collections');
$importCollectionTrees = $this->option('only-collection-trees');
} elseif (! $this->option('force')) {
$importCollections = $this->confirm('Do you want to import collections?');
$importCollectionTrees = $this->confirm('Do you want to import collections trees?');
} else {
$importCollections = true;
$importCollectionTrees = true;
}

$collections = CollectionFacade::all();

$this->withProgressBar($collections, function ($collection) use ($importCollections, $importCollectionTrees) {
if ($importCollections) {
$this->withProgressBar(CollectionFacade::all(), function ($collection) {
if ($this->shouldImportCollections()) {
$lastModified = $collection->fileLastModified();

EloquentCollection::makeModelFromContract($collection)
->fill(['created_at' => $lastModified, 'updated_at' => $lastModified])
->save();
}

if ($importCollectionTrees && ($structure = $collection->structure())) {
if ($this->shouldImportCollectionTrees() && $structure = $collection->structure()) {
$structure->trees()->each(function ($tree) {
$lastModified = $tree->fileLastModified();

app('statamic.eloquent.collections.tree')::makeModelFromContract($tree)
->fill(['created_at' => $lastModified, 'updated_at' => $lastModified])
->save();
Expand All @@ -114,10 +101,24 @@ private function importCollections()
});
}

private function updateEntryOrder()
private function updateEntryOrder(): void
{
$this->withProgressBar(CollectionFacade::all(), function ($collections) {
$collections->updateEntryOrder();
});
}

private function shouldImportCollections(): bool
{
return $this->option('only-collections')
|| ! $this->option('only-collection-trees')
&& ($this->option('force') || $this->confirm('Do you want to import collections?'));
}

private function shouldImportCollectionTrees(): bool
{
return $this->option('only-collection-trees')
|| ! $this->option('only-collections')
&& ($this->option('force') || $this->confirm('Do you want to import collections trees?'));
}
}
Loading

0 comments on commit 53a53d7

Please sign in to comment.