@@ -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}
0 commit comments