Skip to content

Commit

Permalink
Add support:zip-blueprint command (#3740)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurperton committed Sep 1, 2021
1 parent 584907b commit 3924cff
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Console/Commands/SupportZipBlueprint.php
@@ -0,0 +1,91 @@
<?php

namespace Statamic\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Fieldset;
use ZipArchive;

class SupportZipBlueprint extends Command
{
use RunsInPlease;

protected $signature = 'statamic:support:zip-blueprint { blueprint : The blueprint. (e.g. user or collections/blog/post) }';
protected $description = 'Creates a zip file containing a blueprint and all the fieldsets it uses.';

public function handle()
{
if (! $blueprint = $this->getBlueprint()) {
return 1;
}

if (! $filename = $this->createZip($blueprint)) {
return 1;
}

$this->info('Zip created successfully.');
$this->comment("Your zip file awaits: {$filename}");
}

protected function createZip($blueprint)
{
$filename = $blueprint->handle().'-blueprint.zip';

$zip = new ZipArchive();

if (true !== $zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
$this->error("Unable to create zip file \"$filename\"");

return false;
}

$zip->addFile($path = $blueprint->path(), $this->relativePath($path));

$this->getFieldsets($blueprint)->each(function ($fieldset) use ($zip) {
$zip->addFile($path = $fieldset->path(), $this->relativePath($path));
});

$zip->close();

return $filename;
}

protected function getBlueprint()
{
$handle = $this->argument('blueprint');

if (! $blueprint = Blueprint::find($handle)) {
$this->error("Blueprint \"$handle\" not found");

return null;
}

return $blueprint;
}

protected function getFieldsets($blueprint)
{
return $this->getFieldsetHandles($blueprint)->map(function ($handle) {
return Fieldset::find($handle);
});
}

protected function getFieldsetHandles($blueprint)
{
return $blueprint->sections()->map->fields()->flatMap->items()->map(function ($field) {
if (isset($field['import'])) {
return $field['import'];
} elseif (is_string($field['field'])) {
return Str::before($field['field'], '.');
}
})->filter()->unique()->values();
}

protected function relativePath($path)
{
return Str::after($path, resource_path());
}
}
9 changes: 9 additions & 0 deletions src/Fields/Fieldset.php
Expand Up @@ -6,6 +6,7 @@
use Statamic\Events\FieldsetDeleted;
use Statamic\Events\FieldsetSaved;
use Statamic\Facades;
use Statamic\Facades\Path;
use Statamic\Support\Str;

class Fieldset
Expand All @@ -25,6 +26,14 @@ public function handle(): ?string
return $this->handle;
}

public function path()
{
return Path::tidy(vsprintf('%s/%s.yaml', [
Facades\Fieldset::directory(),
str_replace('.', '/', $this->handle()),
]));
}

public function setContents(array $contents)
{
$fields = array_get($contents, 'fields', []);
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/FieldsetRepository.php
Expand Up @@ -19,6 +19,11 @@ public function setDirectory($directory)
return $this;
}

public function directory()
{
return $this->directory;
}

public function find(string $handle): ?Fieldset
{
if ($cached = array_get($this->fieldsets, $handle)) {
Expand Down
1 change: 1 addition & 0 deletions src/Providers/ConsoleServiceProvider.php
Expand Up @@ -35,6 +35,7 @@ class ConsoleServiceProvider extends ServiceProvider
Commands\StaticWarm::class,
// Commands\MakeUserMigration::class,
Commands\SupportDetails::class,
Commands\SupportZipBlueprint::class,
Commands\AuthMigration::class,
Commands\Multisite::class,
Commands\SiteClear::class,
Expand Down

0 comments on commit 3924cff

Please sign in to comment.