Skip to content
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

Make fields method optional #298

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
🧑‍💻 Add DI support to the fields() method
🔥 Remove unnecessary `Field` contract
🎨 Tidy up code
Log1x committed Dec 31, 2024
commit 3ee3687888a2b81c65ee5a95005d1ece57a70ccb
10 changes: 1 addition & 9 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
@@ -11,13 +11,6 @@

class AcfComposer
{
/**
* The application instance.
*
* @var \Roots\Acorn\Application
*/
public $app;

/**
* The booted state.
*/
@@ -71,9 +64,8 @@ class AcfComposer
/**
* Create a new Composer instance.
*/
public function __construct(Application $app)
public function __construct(protected Application $app)
{
$this->app = $app;
$this->manifest = Manifest::make($this);
}

2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ public function addPartial(string $partial): self
}

if (! is_a($partial, FieldsBuilder::class)) {
return $this;
throw new InvalidArgumentException('The partial must return an instance of Builder.');
}

return $this->addFields($partial);
49 changes: 23 additions & 26 deletions src/Composer.php
Original file line number Diff line number Diff line change
@@ -6,12 +6,11 @@
use Illuminate\Support\Str;
use Log1x\AcfComposer\Concerns\InteractsWithPartial;
use Log1x\AcfComposer\Contracts\Composer as ComposerContract;
use Log1x\AcfComposer\Contracts\Field as FieldContract;
use Log1x\AcfComposer\Exceptions\InvalidFieldsException;
use Roots\Acorn\Application;
use StoutLogic\AcfBuilder\FieldsBuilder;

abstract class Composer implements ComposerContract, FieldContract
abstract class Composer implements ComposerContract
{
use InteractsWithPartial;

@@ -68,37 +67,33 @@ public static function make(AcfComposer $composer): self
*/
public function handle(): self
{
$this->beforeRegister();
$this->call('beforeRegister');

$this->compose();

$this->afterRegister();
$this->call('afterRegister');

return $this;
}

/**
* Actions to run before registering the Composer.
* Call a method using the application container.
*/
public function beforeRegister(): void
protected function call(string $hook): mixed
{
//
}
if (! method_exists($this, $hook)) {
return;
}

/**
* Actions to run after registering the Composer.
*/
public function afterRegister(): void
{
//
return $this->app->call([$this, $hook]);
}

/**
* Register the field group with Advanced Custom Fields.
*/
protected function register(?callable $callback = null): void
{
if (empty($this->fields)) {
if (blank($this->fields)) {
return;
}

@@ -124,7 +119,9 @@ public function getFields(bool $cache = true): array
return $this->composer->manifest()->get($this);
}

$fields = is_a($fields = $this->fields(), FieldsBuilder::class)
$fields = $this->resolveFields();

$fields = is_a($fields, FieldsBuilder::class)
? $fields->build()
: $fields;

@@ -139,6 +136,14 @@ public function getFields(bool $cache = true): array
return $fields;
}

/**
* Resolve the fields from the Composer with the container.
*/
public function resolveFields(): mixed
{
return $this->call('fields') ?? [];
}

/**
* Build the field group with the default field type settings.
*/
@@ -147,7 +152,7 @@ public function build(array $fields = []): array
return collect($fields)->map(function ($value, $key) {
if (
! in_array($key, $this->keys) ||
(Str::is($key, 'type') && ! $this->defaults->has($value))
($key === 'type' && ! $this->defaults->has($value))
) {
return $value;
}
@@ -158,7 +163,7 @@ public function build(array $fields = []): array
return $this->build($field);
}

if (Str::is($key, 'type') && $this->defaults->has($value)) {
if ($key === 'type' && $this->defaults->has($value)) {
$field = array_merge($this->defaults->get($field['type'], []), $field);
}
}
@@ -167,12 +172,4 @@ public function build(array $fields = []): array
}, $value);
})->all();
}

/**
* The field group.
*/
public function fields()
{
return [];
}
}
13 changes: 0 additions & 13 deletions src/Contracts/Field.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Field.php
Original file line number Diff line number Diff line change
@@ -40,9 +40,9 @@ public function getFields(bool $cache = true): array
return $this->composer->manifest()->get($this);
}

$fields = $this->fields();
$fields = $this->resolveFields();

if (empty($fields)) {
if (blank($fields)) {
return [];
}

6 changes: 3 additions & 3 deletions src/Partial.php
Original file line number Diff line number Diff line change
@@ -7,13 +7,13 @@ abstract class Partial extends Composer
/**
* Compose and register the defined field groups with ACF.
*
* @return mixed
* @return \StoutLogic\AcfBuilder\FieldsBuilder|void
*/
public function compose()
{
$fields = $this->fields();
$fields = $this->resolveFields();

if (empty($fields)) {
if (blank($fields)) {
return;
}