diff --git a/src/AcfComposer.php b/src/AcfComposer.php index a3196224..43c42f7b 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/Composer.php b/src/Composer.php index c1ae495f..871835d6 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); } } diff --git a/src/Contracts/Field.php b/src/Contracts/Field.php deleted file mode 100644 index 7045ad34..00000000 --- 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 99323251..8904b68a 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; }