diff --git a/src/Console/BlockMakeCommand.php b/src/Console/BlockMakeCommand.php index 4f66a33..b8d1b36 100644 --- a/src/Console/BlockMakeCommand.php +++ b/src/Console/BlockMakeCommand.php @@ -2,6 +2,12 @@ namespace Log1x\AcfComposer\Console; +use Illuminate\Support\Str; + +use function Laravel\Prompts\multiselect; +use function Laravel\Prompts\select; +use function Laravel\Prompts\text; + class BlockMakeCommand extends MakeCommand { /** @@ -10,7 +16,7 @@ class BlockMakeCommand extends MakeCommand * @var string */ protected $signature = 'acf:block {name* : The name of the block} - {--construct : Generate block properties inside of `__construct`} + {--localize : Localize the block name and description} {--force : Overwrite any existing files}'; /** @@ -34,6 +40,112 @@ class BlockMakeCommand extends MakeCommand */ protected $view = 'block'; + /** + * The block supports array. + */ + protected array $supports = [ + 'align', + 'align_text', + 'align_content', + 'full_height', + 'anchor', + 'mode', + 'multiple', + 'jsx', + 'color' => ['background', 'text', 'gradient'], + ]; + + /** + * {@inheritdoc} + */ + public function buildClass($name) + { + $stub = parent::buildClass($name); + + $name = Str::of($name) + ->afterLast('\\') + ->kebab() + ->headline() + ->replace('-', ' '); + + $description = "A beautiful {$name} block."; + + $description = text( + label: 'Enter the block description', + placeholder: $description, + ) ?: $description; + + $categories = get_default_block_categories(); + + $category = select( + label: 'Select the block category', + options: collect($categories)->mapWithKeys(fn ($category) => [$category['slug'] => $category['title']]), + default: 'common', + ); + + $postTypes = multiselect( + label: 'Select the supported post types', + options: collect( + get_post_types(['public' => true]) + )->mapWithKeys(fn ($postType) => [$postType => Str::headline($postType)])->all(), + hint: 'Leave empty to support all post types.', + ); + + $postTypes = collect($postTypes) + ->map(fn ($postType) => sprintf("'%s'", $postType)) + ->join(', '); + + $supports = multiselect( + label: 'Select the supported block features', + options: $this->getSupports(), + default: config('acf.generators.supports', []), + scroll: 8, + ); + + $stub = str_replace( + ['DummySupports', 'DummyDescription', 'DummyCategory', 'DummyPostTypes'], + [$this->buildSupports($supports), $description, $category, $postTypes], + $stub + ); + + return $stub; + } + + /** + * Build the block supports array. + */ + protected function buildSupports(array $selected): string + { + return collect($this->supports)->map(function ($value, $key) use ($selected) { + if (is_int($key)) { + return sprintf("'%s' => %s,", $value, in_array($value, $selected) ? 'true' : 'false'); + } + + $options = collect($value) + ->map(fn ($option) => sprintf( + "%s'%s' => %s,", + Str::repeat(' ', 12), + $option, + in_array($option, $selected) ? 'true' : 'false' + )) + ->join("\n"); + + return sprintf("'%s' => [\n%s\n ],", $key, $options); + })->join("\n "); + } + + /** + * Retrieve the support options. + */ + protected function getSupports(): array + { + return collect($this->supports) + ->mapWithKeys(fn ($value, $key) => is_array($value) + ? collect($value)->mapWithKeys(fn ($option) => [$option => Str::of($option)->finish(" {$key}")->headline()->toString()])->all() + : [$value => Str::headline($value)] + )->all(); + } + /** * Get the stub file for the generator. * @@ -41,8 +153,8 @@ class BlockMakeCommand extends MakeCommand */ protected function getStub() { - if ($this->option('construct')) { - return $this->resolveStub('block.construct'); + if ($this->option('localize')) { + return $this->resolveStub('block.localized'); } return $this->resolveStub('block'); diff --git a/src/Console/stubs/block.stub b/src/Console/stubs/block.stub index 4c4002c..76e3001 100644 --- a/src/Console/stubs/block.stub +++ b/src/Console/stubs/block.stub @@ -19,14 +19,14 @@ class DummyClass extends Block * * @var string */ - public $description = 'A simple DummyTitle block.'; + public $description = 'DummyDescription'; /** * The block category. * * @var string */ - public $category = 'formatting'; + public $category = 'DummyCategory'; /** * The block icon. @@ -47,7 +47,7 @@ class DummyClass extends Block * * @var array */ - public $post_types = []; + public $post_types = [DummyPostTypes]; /** * The parent block type allow list. @@ -97,19 +97,7 @@ class DummyClass extends Block * @var array */ public $supports = [ - 'align' => true, - 'align_text' => false, - 'align_content' => false, - 'full_height' => false, - 'anchor' => false, - 'mode' => false, - 'multiple' => true, - 'jsx' => true, - 'color' => [ - 'background' => true, - 'text' => true, - 'gradient' => true, - ], + DummySupports ]; /**