From 3f97e1f938e794c938edb78941eeff5c6047cb6d Mon Sep 17 00:00:00 2001 From: Kevin Meijer Date: Tue, 19 Nov 2024 10:49:30 +0100 Subject: [PATCH] Ran Phpstan + Pint --- README.md | 4 +-- config/statamic-page-builder-kit.php | 28 +---------------- phpstan.neon | 9 ++++++ src/ServiceProvider.php | 45 ++++++++++++++-------------- 4 files changed, 35 insertions(+), 51 deletions(-) create mode 100644 phpstan.neon diff --git a/README.md b/README.md index 33489fd..b832a43 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Statamic Page Builder +# Statamic Page Builder Kit > This addon adds a page builder fieldset with some components. @@ -13,7 +13,7 @@ The page builder contains a: ## Install ``` bash -composer require justbetter/statamic-page-builder +composer require justbetter/statamic-page-builder-kit ``` ## How to Use diff --git a/config/statamic-page-builder-kit.php b/config/statamic-page-builder-kit.php index 0957195..ca5d8ed 100644 --- a/config/statamic-page-builder-kit.php +++ b/config/statamic-page-builder-kit.php @@ -1,31 +1,5 @@ [ - 'general' => [ - 'title' => __('General'), - 'sets' => [] - ], - 'banners' => [ - 'title' => __('Banners'), - 'sets' => [] - ], - ], - 'sets' => [ - 'general' => [ - ComponentTextFieldset::class, - ComponentUspsFieldset::class, - ComponentImageTextFieldset::class, - ], - 'banners' => [ - ComponentHeroBannerFieldset::class - ] - ], - 'additional_sets' => [], -]; \ No newline at end of file +]; diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..33a27ef --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +includes: + - ./vendor/larastan/larastan/extension.neon + - ./vendor/phpstan/phpstan-mockery/extension.neon + +parameters: + paths: + - src + - tests + level: 5 \ No newline at end of file diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 1cde424..894263e 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,14 +2,14 @@ namespace Justbetter\StatamicPageBuilderKit; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; use Statamic\Facades\Blueprint; use Statamic\Facades\Collection as CollectionFacade; -use Illuminate\Support\Collection; use Statamic\Facades\Fieldset as FieldsetFacade; -use Statamic\Fields\Fieldset; use Statamic\Facades\Site; use Statamic\Facades\YAML; +use Statamic\Fields\Fieldset; use Statamic\Providers\AddonServiceProvider; use Statamic\Support\Str; @@ -27,7 +27,7 @@ public function bootAddon(): void public function bootConfig(): self { $this->mergeConfigFrom( - __DIR__ . '/../config/statamic-page-builder-kit.php', + __DIR__.'/../config/statamic-page-builder-kit.php', 'justbetter.statamic-page-builder-kit' ); @@ -36,9 +36,9 @@ public function bootConfig(): self public function bootViews(): self { - $this->loadViewsFrom(__DIR__ . '/../resources/views', 'statamic-page-builder-kit'); + $this->loadViewsFrom(__DIR__.'/../resources/views', 'statamic-page-builder-kit'); $this->publishes([ - __DIR__ . '/../resources/views' => resource_path('views/vendor/justbetter/statamic-page-builder-kit'), + __DIR__.'/../resources/views' => resource_path('views/vendor/justbetter/statamic-page-builder-kit'), ], 'statamic-page-builder-kit'); return $this; @@ -48,8 +48,8 @@ public function bootCollections(): self { $pagesCollection = CollectionFacade::findByHandle('pages'); - if (!$pagesCollection || !File::exists($pagesCollection->path())) { - if (!$pagesCollection) { + if (! $pagesCollection || ! File::exists($pagesCollection->path())) { + if (! $pagesCollection) { $pagesCollection = CollectionFacade::make('pages'); } @@ -67,10 +67,10 @@ public function bootCollections(): self 'label' => 'Entry', 'url' => '{permalink}', 'refresh' => true, - ] + ], ], 'structure' => [ - 'root' => true + 'root' => true, ], ]; @@ -78,9 +78,9 @@ public function bootCollections(): self File::put($pagesCollection->path(), $file->dump($pagesData)); } - if (!Blueprint::find('collections/pages/page')) { + if (! Blueprint::find('collections/pages/page')) { $pageBlueprint = Blueprint::make('collections/pages/page'); - File::copyDirectory(__DIR__ . '/../resources/blueprints/collections/pages/', File::dirname($pageBlueprint->path())); + File::copyDirectory(__DIR__.'/../resources/blueprints/collections/pages/', File::dirname($pageBlueprint->path())); } return $this; @@ -90,28 +90,28 @@ public function bootPageBuilder(): self { $pageBuilderComponents = FieldsetFacade::all(); $pageBuilderComponents = $pageBuilderComponents - ->filter(fn($fieldset) => $this->fieldsetIsComponent($fieldset)) + ->filter(fn ($fieldset) => $this->fieldsetIsComponent($fieldset)) ->mapToGroups(fn ($fieldset) => [ - $this->getFieldsetGroup($fieldset) => [$this->getFieldsetName($fieldset) => $fieldset] + $this->getFieldsetGroup($fieldset) => [$this->getFieldsetName($fieldset) => $fieldset], ]) ->toBase(); $groups = $pageBuilderComponents ->map(fn ($fieldsets, $group) => [ 'display' => __(Str::headline($group)), - 'sets' => $this->getGroupFieldsets($fieldsets) + 'sets' => $this->getGroupFieldsets($fieldsets), ])->toArray(); $pageBuilderFieldset = FieldsetFacade::find('statamic-page-builder-kit::page_builder'); - $pageBuilderContent = $pageBuilderFieldset->contents(); + $pageBuilderContent = $pageBuilderFieldset?->contents(); - if (!empty($pageBuilderContent['fields']) && !empty($pageBuilderContent['fields'][0]['field']['sets'])) { + if (! empty($pageBuilderContent['fields']) && ! empty($pageBuilderContent['fields'][0]['field']['sets'])) { $pageBuilderContent['fields'][0]['field']['sets'] = $groups; } $pageBuilderFieldset - ->setContents($pageBuilderContent) - ->saveQuietly(); + ?->setContents($pageBuilderContent ?? []) + ?->saveQuietly(); return $this; } @@ -119,6 +119,7 @@ public function bootPageBuilder(): self protected function fieldsetIsComponent(Fieldset $fieldset): bool { $handle = $this->getFieldsetHandle($fieldset); + return Str::startsWith($handle, 'component_'); } @@ -128,14 +129,14 @@ protected function getGroupFieldsets(Collection $fieldsets): array $fieldset = collect($fieldset)->first(); return [ - $this->getFieldsetGroup($fieldset) . '_' . $this->getFieldsetName($fieldset) => [ + $this->getFieldsetGroup($fieldset).'_'.$this->getFieldsetName($fieldset) => [ 'display' => __(Str::headline($this->getFieldsetName($fieldset))), 'fields' => [ [ - 'import' => $fieldset->handle() - ] + 'import' => $fieldset->handle(), + ], ], - ] + ], ]; })->toBase();