Skip to content

Commit

Permalink
Ran Phpstan + Pint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Meijer committed Nov 19, 2024
1 parent 91fbb83 commit 3f97e1f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Statamic Page Builder
# Statamic Page Builder Kit

> This addon adds a page builder fieldset with some components.
Expand All @@ -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
Expand Down
28 changes: 1 addition & 27 deletions config/statamic-page-builder-kit.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
<?php

use Justbetter\StatamicPageBuilderKit\Fieldsets\Components\ComponentTextFieldset;
use Justbetter\StatamicPageBuilderKit\Fieldsets\Components\ComponentUspsFieldset;
use Justbetter\StatamicPageBuilderKit\Fieldsets\Components\ComponentImageTextFieldset;
use Justbetter\StatamicPageBuilderKit\Fieldsets\Components\ComponentHeroBannerFieldset;

return [
'groups' => [
'general' => [
'title' => __('General'),
'sets' => []
],
'banners' => [
'title' => __('Banners'),
'sets' => []
],
],

'sets' => [
'general' => [
ComponentTextFieldset::class,
ComponentUspsFieldset::class,
ComponentImageTextFieldset::class,
],
'banners' => [
ComponentHeroBannerFieldset::class
]
],
'additional_sets' => [],
];
];
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon

parameters:
paths:
- src
- tests
level: 5
45 changes: 23 additions & 22 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'
);

Expand All @@ -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;
Expand All @@ -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');
}

Expand All @@ -67,20 +67,20 @@ public function bootCollections(): self
'label' => 'Entry',
'url' => '{permalink}',
'refresh' => true,
]
],
],
'structure' => [
'root' => true
'root' => true,
],
];

$file = YAML::file($pagesCollection->path());
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;
Expand All @@ -90,35 +90,36 @@ 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;
}

protected function fieldsetIsComponent(Fieldset $fieldset): bool
{
$handle = $this->getFieldsetHandle($fieldset);

return Str::startsWith($handle, 'component_');
}

Expand All @@ -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();

Expand Down

0 comments on commit 3f97e1f

Please sign in to comment.