Skip to content

Commit c4f186a

Browse files
committed
wip: global blocks
1 parent 5715ce9 commit c4f186a

File tree

4 files changed

+118
-20
lines changed

4 files changed

+118
-20
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ phpunit.xml
1111
phpstan.neon
1212
testbench.yaml
1313
vendor
14-
.phpunit.cache
14+
.phpunit.cache
15+
CLAUDE.md

src/Commands/CreatePageBuilderPluginBlockCommand.php

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreatePageBuilderPluginBlockCommand extends Command
1414
{
1515
use CreatesClassFile;
1616

17-
public $signature = 'page-builder-plugin:make-block {name?} {--T|type=} {--panel=}';
17+
public $signature = 'page-builder-plugin:make-block {name?} {--T|type=} {--panel=} {--global : Create a global block in Blocks/Globals directory}';
1818

1919
public $description = 'create a new block';
2020

@@ -37,7 +37,14 @@ public function handle(): int
3737

3838
$this->panel = $this->getPanelToCreateIn();
3939

40+
$isGlobal = $this->option('global');
41+
4042
$blocksNamespace = $this->getClassNameSpaces('Blocks');
43+
44+
if ($isGlobal) {
45+
$this->createGlobalsCategoryIfNotExists();
46+
$blocksNamespace .= '\\Globals';
47+
}
4148

4249
$blockClass = $blocksNamespace . '\\' . $block;
4350

@@ -60,40 +67,83 @@ public function handle(): int
6067
);
6168

6269
if ($blockType === 'iframe') {
70+
$stubName = $isGlobal ? 'block.global' : 'block';
71+
$replacements = [
72+
'{{ class }}' => str($blockClass)->afterLast('\\')->replace('\\', ''),
73+
'{{ namespace }}' => str($blockClass)->beforeLast('\\'),
74+
];
75+
76+
if ($isGlobal) {
77+
$replacements['{{ globalsNamespace }}'] = $this->getClassNameSpaces('BlockCategories');
78+
}
79+
6380
$this->createFileFromStub(
64-
'block',
81+
$stubName,
6582
$this->appClassToPath($blockClass),
66-
[
67-
'{{ class }}' => str($blockClass)->afterLast('\\')->replace('\\', ''),
68-
'{{ namespace }}' => str($blockClass)->beforeLast('\\'),
69-
]
83+
$replacements
7084
);
7185
}
7286

7387
if ($blockType === 'view') {
7488
$viewName = str($block)->replace('\\', '.')->kebab()->replace('.-', '.');
89+
if ($isGlobal) {
90+
$viewName = 'globals.' . $viewName;
91+
}
92+
93+
$stubName = $isGlobal ? 'block.global.view' : 'block.view';
94+
$replacements = [
95+
'{{ class }}' => str($block)->afterLast('\\')->replace('\\', ''),
96+
'{{ namespace }}' => str($blockClass)->beforeLast('\\'),
97+
'{{ viewName }}' => $viewName,
98+
'{{ panelId }}' => $this->panel->getId(),
99+
];
100+
101+
if ($isGlobal) {
102+
$replacements['{{ globalsNamespace }}'] = $this->getClassNameSpaces('BlockCategories');
103+
}
104+
75105
$this->createFileFromStub(
76-
'block.view',
106+
$stubName,
77107
$this->appClassToPath($blockClass),
78-
[
79-
'{{ class }}' => str($block)->afterLast('\\')->replace('\\', ''),
80-
'{{ namespace }}' => str($blockClass)->beforeLast('\\'),
81-
'{{ viewName }}' => $viewName,
82-
'{{ panelId }}' => $this->panel->getId(),
83-
]
108+
$replacements
84109
);
85110

111+
$viewPath = str($viewName)
112+
->replace('.', '/')
113+
->prepend("views/{$this->panel->getId()}/blocks/")
114+
->append('.blade.php');
115+
86116
$this->createFileFromStub(
87117
'block.blade',
88-
resource_path(
89-
$viewName
90-
->replace('.', '/')
91-
->prepend("views/{$this->panel->getId()}/blocks/")
92-
->append('.blade.php')
93-
),
118+
resource_path($viewPath),
94119
);
95120
}
96121

97122
return self::SUCCESS;
98123
}
124+
125+
protected function createGlobalsCategoryIfNotExists(): void
126+
{
127+
$categoryNamespace = $this->getClassNameSpaces('BlockCategories');
128+
$globalsClass = $categoryNamespace . '\\Globals';
129+
130+
try {
131+
if (class_exists($globalsClass)) {
132+
return; // Category already exists
133+
}
134+
} catch (\Throwable $th) {
135+
// Class doesn't exist, proceed with creation
136+
}
137+
138+
$this->createFileFromStub(
139+
'category-block',
140+
$this->appClassToPath($globalsClass),
141+
[
142+
'{{ class }}' => 'Globals',
143+
'{{ namespace }}' => $categoryNamespace,
144+
]
145+
);
146+
147+
$this->info("Created Globals category at: {$globalsClass}");
148+
}
99149
}

stubs/block.global.stub

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

stubs/block.global.view.stub

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

0 commit comments

Comments
 (0)