-
-
Notifications
You must be signed in to change notification settings - Fork 2
Feat/global blocks #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
c4f186a
02c4ad8
f72f8c0
c9f09c8
0e52bc1
f69d4d5
a96af73
67e4a2e
356f951
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,4 +11,5 @@ phpunit.xml | |
| phpstan.neon | ||
| testbench.yaml | ||
| vendor | ||
| .phpunit.cache | ||
| .phpunit.cache | ||
| CLAUDE.md | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ class CreatePageBuilderPluginBlockCommand extends Command | |
| { | ||
| use CreatesClassFile; | ||
|
|
||
| public $signature = 'page-builder-plugin:make-block {name?} {--T|type=} {--panel=}'; | ||
| public $signature = 'page-builder-plugin:make-block {name?} {--T|type=} {--panel=} {--global : Create a global block in Blocks/Globals directory}'; | ||
|
|
||
| public $description = 'create a new block'; | ||
|
|
||
|
|
@@ -37,8 +37,16 @@ public function handle(): int | |
|
|
||
| $this->panel = $this->getPanelToCreateIn(); | ||
|
|
||
| $isGlobal = $this->option('global'); | ||
|
|
||
| $blocksNamespace = $this->getClassNameSpaces('Blocks'); | ||
|
|
||
| if ($isGlobal) { | ||
| $this->createGlobalsCategoryIfNotExists(); | ||
| $isFirstGlobalBlock = $this->isFirstGlobalBlock(); | ||
| $blocksNamespace .= '\\Globals'; | ||
| } | ||
|
|
||
| $blockClass = $blocksNamespace . '\\' . $block; | ||
|
|
||
| try { | ||
|
|
@@ -60,40 +68,157 @@ public function handle(): int | |
| ); | ||
|
|
||
| if ($blockType === 'iframe') { | ||
| $stubName = $isGlobal ? 'block.global' : 'block'; | ||
| $replacements = [ | ||
| '{{ class }}' => str($blockClass)->afterLast('\\')->replace('\\', ''), | ||
| '{{ namespace }}' => str($blockClass)->beforeLast('\\'), | ||
| ]; | ||
|
|
||
| if ($isGlobal) { | ||
| $replacements['{{ globalsNamespace }}'] = $this->getClassNameSpaces('BlockCategories'); | ||
| } | ||
|
|
||
| $this->createFileFromStub( | ||
| 'block', | ||
| $stubName, | ||
| $this->appClassToPath($blockClass), | ||
| [ | ||
| '{{ class }}' => str($blockClass)->afterLast('\\')->replace('\\', ''), | ||
| '{{ namespace }}' => str($blockClass)->beforeLast('\\'), | ||
| ] | ||
| $replacements | ||
| ); | ||
| } | ||
|
|
||
| if ($blockType === 'view') { | ||
| $viewName = str($block)->replace('\\', '.')->kebab()->replace('.-', '.'); | ||
| if ($isGlobal) { | ||
| $viewName = 'globals.' . $viewName; | ||
| } | ||
|
|
||
| $stubName = $isGlobal ? 'block.global.view' : 'block.view'; | ||
| $replacements = [ | ||
| '{{ class }}' => str($block)->afterLast('\\')->replace('\\', ''), | ||
| '{{ namespace }}' => str($blockClass)->beforeLast('\\'), | ||
| '{{ viewName }}' => $viewName, | ||
| '{{ panelId }}' => $this->panel->getId(), | ||
| ]; | ||
|
|
||
| if ($isGlobal) { | ||
| $replacements['{{ globalsNamespace }}'] = $this->getClassNameSpaces('BlockCategories'); | ||
| } | ||
|
|
||
| $this->createFileFromStub( | ||
| 'block.view', | ||
| $stubName, | ||
| $this->appClassToPath($blockClass), | ||
| [ | ||
| '{{ class }}' => str($block)->afterLast('\\')->replace('\\', ''), | ||
| '{{ namespace }}' => str($blockClass)->beforeLast('\\'), | ||
| '{{ viewName }}' => $viewName, | ||
| '{{ panelId }}' => $this->panel->getId(), | ||
| ] | ||
| $replacements | ||
| ); | ||
|
|
||
| $viewPath = str($viewName) | ||
| ->replace('.', '/') | ||
| ->prepend("views/{$this->panel->getId()}/blocks/") | ||
| ->append('.blade.php'); | ||
|
|
||
| $this->createFileFromStub( | ||
| 'block.blade', | ||
| resource_path( | ||
| $viewName | ||
| ->replace('.', '/') | ||
| ->prepend("views/{$this->panel->getId()}/blocks/") | ||
| ->append('.blade.php') | ||
| ), | ||
| resource_path($viewPath), | ||
| ); | ||
| } | ||
|
|
||
| if ($isGlobal && isset($isFirstGlobalBlock) && $isFirstGlobalBlock) { | ||
| $this->createGlobalBlocksResource(); | ||
| $this->publishGlobalBlockMigration(); | ||
| } | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| protected function createGlobalsCategoryIfNotExists(): void | ||
| { | ||
| $categoryNamespace = $this->getClassNameSpaces('BlockCategories'); | ||
| $globalsClass = $categoryNamespace . '\\Globals'; | ||
|
|
||
| if (class_exists($globalsClass)) { | ||
| return; | ||
| } | ||
|
|
||
| $this->createFileFromStub( | ||
| 'category-block', | ||
| $this->appClassToPath($globalsClass), | ||
| [ | ||
| '{{ class }}' => 'Globals', | ||
| '{{ namespace }}' => $categoryNamespace, | ||
| ] | ||
| ); | ||
|
|
||
| $this->info("Created Globals category at: {$globalsClass}"); | ||
| } | ||
|
|
||
| protected function isFirstGlobalBlock(): bool | ||
| { | ||
| $globalBlocksPath = app_path("Filament/{$this->panel->getId()}/Blocks/Globals"); | ||
|
|
||
| if (! is_dir($globalBlocksPath)) { | ||
| return true; | ||
| } | ||
|
|
||
| $existingBlocks = glob($globalBlocksPath . '/*.php'); | ||
|
|
||
| return empty($existingBlocks); | ||
| } | ||
|
|
||
| protected function createGlobalBlocksResource(): void | ||
| { | ||
| $resourceNamespace = $this->getClassNameSpaces('Resources'); | ||
| $resourceClass = 'GlobalBlocksResource'; | ||
| $resourceFullClass = $resourceNamespace . '\\' . $resourceClass; | ||
|
|
||
| $this->createFileFromStub( | ||
| 'global-blocks-resource', | ||
| $this->appClassToPath($resourceFullClass), | ||
| [ | ||
| '{{ class }}' => $resourceClass, | ||
| '{{ namespace }}' => $resourceNamespace, | ||
| '{{ resourceNamespace }}' => $resourceNamespace, | ||
| ] | ||
| ); | ||
|
|
||
| $pagesNamespace = $resourceNamespace . '\\' . $resourceClass . '\\Pages'; | ||
|
|
||
| $this->createFileFromStub( | ||
| 'global-blocks-list-page', | ||
| $this->appClassToPath($pagesNamespace . '\\ListGlobalBlocks'), | ||
| [ | ||
| '{{ class }}' => 'ListGlobalBlocks', | ||
| '{{ namespace }}' => $pagesNamespace, | ||
| '{{ resourceClass }}' => $resourceClass, | ||
| '{{ resourceNamespace }}' => $resourceNamespace, | ||
| ] | ||
| ); | ||
|
|
||
| $this->createFileFromStub( | ||
| 'global-blocks-edit-page', | ||
| $this->appClassToPath($pagesNamespace . '\\EditGlobalBlock'), | ||
| [ | ||
| '{{ class }}' => 'EditGlobalBlock', | ||
| '{{ namespace }}' => $pagesNamespace, | ||
| '{{ resourceClass }}' => $resourceClass, | ||
| '{{ resourceNamespace }}' => $resourceNamespace, | ||
| ] | ||
| ); | ||
|
|
||
| $this->info("Created Global Blocks resource at: {$resourceFullClass}"); | ||
| $this->info('The resource has been automatically registered and will appear in your Filament panel navigation.'); | ||
| } | ||
|
||
|
|
||
| protected function publishGlobalBlockMigration(): void | ||
| { | ||
| $timestamp = now()->format('Y_m_d_His'); | ||
| $migrationName = 'create_global_block_configs_table'; | ||
| $migrationFile = database_path("migrations/{$timestamp}_{$migrationName}.php"); | ||
|
|
||
| $this->createFileFromStub( | ||
| 'create_global_block_configs_table.php', | ||
| $migrationFile, | ||
| [] | ||
| ); | ||
|
|
||
| $this->info("Created migration: {$migrationFile}"); | ||
| $this->warn("Don't forget to run 'php artisan migrate' to create the global_block_configs table."); | ||
| } | ||
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do not think u will need this if u will remove those two function regarding exporting migrations and block resource