Skip to content

Commit cb8ace2

Browse files
committed
feat: command for creating blocks
1 parent a0a2d45 commit cb8ace2

File tree

6 files changed

+174
-10
lines changed

6 files changed

+174
-10
lines changed

src/Commands/PageBuilderPluginCommand.php

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,150 @@
22

33
namespace RedberryProducts\PageBuilderPlugin\Commands;
44

5+
use Filament\Facades\Filament;
6+
use Filament\Panel;
57
use Illuminate\Console\Command;
8+
use Illuminate\Filesystem\Filesystem;
9+
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Str;
11+
12+
use function Laravel\Prompts\confirm;
13+
use function Laravel\Prompts\select;
14+
use function Laravel\Prompts\text;
615

716
class PageBuilderPluginCommand extends Command
817
{
9-
// TODO: Implement scaffolding block class
10-
public $signature = 'page-builder-plugin';
18+
public $signature = 'page-builder-plugin:make-block {name?} {--T|type=} {--panel=}';
1119

12-
public $description = 'My command';
20+
public $description = 'create a new block';
1321

1422
public function handle(): int
1523
{
16-
$this->comment('All done');
24+
$block = (string) str(
25+
$this->argument('name') ??
26+
text(
27+
label: 'What is the block name?',
28+
placeholder: 'Header',
29+
required: true,
30+
),
31+
)
32+
->trim('/')
33+
->trim('\\')
34+
->trim(' ')
35+
->replace('/', '\\');
36+
37+
$panel = $this->option('panel');
38+
39+
if ($panel) {
40+
$panel = Filament::getPanel($panel, isStrict: false);
41+
}
42+
43+
if (! $panel) {
44+
$panels = Filament::getPanels();
45+
46+
/** @var Panel $panel */
47+
$panel = (count($panels) > 1) ? $panels[select(
48+
label: 'Which panel would you like to create this in?',
49+
options: array_map(
50+
fn (Panel $panel): string => $panel->getId(),
51+
$panels,
52+
),
53+
default: Filament::getDefaultPanel()->getId()
54+
)] : Arr::first($panels);
55+
}
56+
57+
$blocksNamespace = array_map(
58+
fn (string $namespace): string => str_replace('Resources', 'Blocks', $namespace),
59+
$panel->getResourceNamespaces(),
60+
);
61+
62+
$blocksNamespace = (count($blocksNamespace) > 1) ?
63+
select(
64+
label: 'Which namespace would you like to create this in?',
65+
options: $blocksNamespace
66+
) : (Arr::first($blocksNamespace));
67+
68+
$blockClass = $blocksNamespace . '\\' . $block;
69+
70+
if (class_exists($blockClass)) {
71+
if (! confirm(
72+
label: 'This block already exists. Do you want to overwrite it?',
73+
default: false,
74+
)) {
75+
return self::FAILURE;
76+
}
77+
}
78+
79+
$blockType = $this->option('type') ?? select(
80+
label: 'What type of block is this?',
81+
options: ['iframe', 'view'],
82+
default: 'iframe',
83+
);
84+
85+
if ($blockType === 'iframe') {
86+
$this->createFileFromStub(
87+
'block',
88+
$this->appClassToPath($blockClass),
89+
[
90+
"{{ class }}" => str($blockClass)->afterLast('\\')->replace('\\', ''),
91+
"{{ namespace }}" => str($blockClass)->beforeLast('\\'),
92+
]
93+
);
94+
}
95+
96+
if ($blockType === 'view') {
97+
$viewName = str($block)->replace("\\", ".")->kebab()->replace(".-", ".");
98+
$this->createFileFromStub(
99+
'block.view',
100+
$this->appClassToPath($blockClass),
101+
[
102+
"{{ class }}" => str($block)->afterLast('\\')->replace('\\', ''),
103+
"{{ namespace }}" => str($blockClass)->beforeLast('\\'),
104+
'{{ viewName }}' => $viewName,
105+
]
106+
);
107+
108+
$this->createFileFromStub(
109+
'block.blade',
110+
resource_path(
111+
$viewName
112+
->replace('.', '/')
113+
->prepend("views/blocks/")
114+
->append('.blade.php')
115+
),
116+
);
117+
}
17118

18119
return self::SUCCESS;
19120
}
121+
122+
public function createFileFromStub(string $stub, string $path, array $replacements = []): void
123+
{
124+
/** @var Filesystem */
125+
$filesystem = app(Filesystem::class);
126+
127+
if (! file_exists($stubPath = base_path("stubs/page-builder-plugin/{$stub}.stub"))) {
128+
$stubPath = __DIR__ . "/../../stubs/{$stub}.stub";
129+
}
130+
131+
$contents = strtr(file_get_contents($stubPath), $replacements);
132+
133+
$filesystem->ensureDirectoryExists(
134+
pathinfo($path, PATHINFO_DIRNAME)
135+
);
136+
137+
ray($path, $contents);
138+
139+
$filesystem->put($path, $contents);
140+
}
141+
142+
private function appClassToPath(string $class): string
143+
{
144+
$appNamespace = app()->getNamespace();
145+
$relativePath = Str::replaceFirst($appNamespace, '', $class);
146+
147+
return app_path(
148+
str_replace('\\', '/', $relativePath) . '.php'
149+
);
150+
}
20151
}

src/Components/Forms/PageBuilderPreview.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getPageBuilderData(): array
6767
$id = $data['block_id'];
6868

6969
if ($blockType) {
70-
$formatted = $blockType::formatForSinglePreview($data['data']);
70+
$formatted = $blockType::formatForSinglePreview($data['data'] ?? []);
7171

7272
return [
7373
'id' => $id,

src/Contracts/BaseBlock.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ public static function getCategory(): string
3232
return '';
3333
}
3434

35-
public static function getThumbnail(): string
36-
{
37-
return '';
38-
}
39-
4035
public static function formatForListing(array $data): array
4136
{
4237
return self::formatForSinglePreview($data);

stubs/block.blade.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
@json($block)
3+
</div>

stubs/block.stub

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use RedberryProducts\PageBuilderPlugin\Contracts\BaseBlock;
6+
7+
class {{ class }} extends BaseBlock
8+
{
9+
public static function blockSchema(): array
10+
{
11+
return [
12+
13+
];
14+
}
15+
}

stubs/block.view.stub

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use RedberryProducts\PageBuilderPlugin\Contracts\BaseBlock;
6+
7+
class {{ class }} extends BaseBlock
8+
{
9+
public static function blockSchema(): array
10+
{
11+
return [
12+
// schema
13+
];
14+
}
15+
16+
public static function getView(): ?string
17+
{
18+
return 'blocks.{{ viewName }}';
19+
}
20+
}

0 commit comments

Comments
 (0)