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
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 1 addition & 9 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@

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

/**
* The booted state.
*/
Expand Down Expand Up @@ -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);
}

Expand Down
41 changes: 23 additions & 18 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

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

Expand All @@ -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.
*/
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand Down
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
Expand Up @@ -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 [];
}

Expand Down
6 changes: 3 additions & 3 deletions src/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading