generated from filamentphp/plugin-skeleton
-
-
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c4f186a
wip: global blocks
mishatch 02c4ad8
feat: add global blocks
mishatch f72f8c0
Fix styling
mishatch c9f09c8
refactor: move global blocks resource to package with configurable re…
mishatch 0e52bc1
Fix styling
mishatch f69d4d5
refactor: move global blocks config to plugin-level registration
mishatch a96af73
Fix styling
mishatch 67e4a2e
test: add tests for global blocks feature
mishatch 356f951
Fix styling
mishatch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
database/migrations/create_global_block_configs_table.php.stub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up() | ||
| { | ||
| Schema::create('global_block_configs', function (Blueprint $table) { | ||
| $table->id(); | ||
| $table->string('name'); | ||
| $table->string('class_name')->unique(); | ||
| $table->json('configuration')->nullable(); | ||
| $table->timestamps(); | ||
|
|
||
| $table->index('class_name'); | ||
| }); | ||
| } | ||
|
|
||
| public function down() | ||
| { | ||
| Schema::dropIfExists('global_block_configs'); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,96 @@ 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->info('To manage global blocks in Filament, add the GlobalBlocksPlugin to your panel:'); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
Comment on lines
+151
to
+162
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
some apps have multiple panel providers, move this definitions to plugin registration step, so in case someone has multiple panels and want global blocks in only one of them they can configure it without lot of work