From 2bc34adc72a1ac3cb064ead0cd763a51bca762ca Mon Sep 17 00:00:00 2001 From: pkosciak Date: Fri, 27 Dec 2024 09:16:23 +0100 Subject: [PATCH 1/2] Make fields method optional --- src/Builder.php | 2 +- src/Composer.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Builder.php b/src/Builder.php index b432e4c..b0e21c1 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -99,7 +99,7 @@ public function addPartial(string $partial): self } if (! is_a($partial, FieldsBuilder::class)) { - throw new InvalidArgumentException('The partial must return an instance of Builder.'); + return $this; } return $this->addFields($partial); diff --git a/src/Composer.php b/src/Composer.php index c1ae495..843b5f3 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -167,4 +167,12 @@ public function build(array $fields = []): array }, $value); })->all(); } + + /** + * The field group. + */ + public function fields() + { + return []; + } } From 3ee3687888a2b81c65ee5a95005d1ece57a70ccb Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 31 Dec 2024 09:54:25 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Add=20D?= =?UTF-8?q?I=20support=20to=20the=20`fields()`=20method=20=F0=9F=94=A5=20R?= =?UTF-8?q?emove=20unnecessary=20`Field`=20contract=20=F0=9F=8E=A8=20Tidy?= =?UTF-8?q?=20up=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AcfComposer.php | 10 +-------- src/Builder.php | 2 +- src/Composer.php | 49 +++++++++++++++++++---------------------- src/Contracts/Field.php | 13 ----------- src/Field.php | 4 ++-- src/Partial.php | 6 ++--- 6 files changed, 30 insertions(+), 54 deletions(-) delete mode 100644 src/Contracts/Field.php diff --git a/src/AcfComposer.php b/src/AcfComposer.php index a319622..43c42f7 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -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); } diff --git a/src/Builder.php b/src/Builder.php index b0e21c1..b432e4c 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -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); diff --git a/src/Composer.php b/src/Composer.php index 843b5f3..871835d 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -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,29 +67,25 @@ 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]); } /** @@ -98,7 +93,7 @@ public function afterRegister(): void */ 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 []; - } } diff --git a/src/Contracts/Field.php b/src/Contracts/Field.php deleted file mode 100644 index 7045ad3..0000000 --- a/src/Contracts/Field.php +++ /dev/null @@ -1,13 +0,0 @@ -composer->manifest()->get($this); } - $fields = $this->fields(); + $fields = $this->resolveFields(); - if (empty($fields)) { + if (blank($fields)) { return []; } diff --git a/src/Partial.php b/src/Partial.php index 9932325..8904b68 100644 --- a/src/Partial.php +++ b/src/Partial.php @@ -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; }