From fc06a037841fbd9ab3bf7aecc635394a270a21fb Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 12 Nov 2024 15:37:14 +0000 Subject: [PATCH] Extract field update logic into a class. --- src/Support/FieldUpdater.php | 138 +++++++++++++++++++++++++++ src/Transformers/BardTransformer.php | 70 +------------- src/WordPress/Gutenberg.php | 90 ++--------------- 3 files changed, 149 insertions(+), 149 deletions(-) create mode 100644 src/Support/FieldUpdater.php diff --git a/src/Support/FieldUpdater.php b/src/Support/FieldUpdater.php new file mode 100644 index 0000000..79c4185 --- /dev/null +++ b/src/Support/FieldUpdater.php @@ -0,0 +1,138 @@ +field = $field; + + return $this; + } + + public function blueprint(Blueprint $blueprint): self + { + $this->blueprint = $blueprint; + + return $this; + } + + public function updateFieldConfig(array $config): void + { + if ($prefix = $this->field->prefix()) { + $this->updatePrefixedField($prefix, $config); + return; + } + + if ($importedField = $this->getImportedField()) { + $this->updateImportedField($importedField, $config); + return; + } + + $this->blueprint->ensureFieldHasConfig( + handle: $this->field->handle(), + config: $config + ); + + $this->blueprint->save(); + } + + private function getImportedField(): ?array + { + return $this->blueprint->fields()->items() + ->where('handle', $this->field->handle()) + ->filter(fn (array $field) => isset($field['field']) && is_string($field['field'])) + ->first(); + } + + /** + * This method handles updating imported fields from fieldsets. + * + * - + * handle: foo + * field: fieldset.foo + */ + private function updateImportedField(array $importedField, array $config): void + { + /** @var \Statamic\Fields\Fieldset $fieldset */ + $fieldHandle = Str::after($importedField['field'], '.'); + $fieldset = Fieldset::find(Str::before($importedField['field'], '.')); + + $fieldset->setContents([ + ...$fieldset->contents(), + 'fields' => collect($fieldset->contents()['fields']) + ->map(function (array $field) use ($config, $fieldHandle) { + if ($field['handle'] === $fieldHandle) { + return [ + 'handle' => $field['handle'], + 'field' => $config, + ]; + } + + return $field; + }) + ->all(), + ]); + + $fieldset->save(); + + $this->clearBlinkCaches(); + } + + /** + * This method handles updating imported fields from fieldsets, which use a prefix. + * + * - + * import: fieldset + * prefix: foo_ + */ + private function updatePrefixedField(string $prefix, array $config): void + { + /** @var \Statamic\Fields\Fieldset $fieldset */ + $fieldset = $this->blueprint->fields()->items() + ->filter(fn (array $field) => isset($field['import'])) + ->map(fn (array $field) => Fieldset::find($field['import'])) + ->filter(function ($fieldset) use ($prefix) { + return collect($fieldset->fields()->items()) + ->where('handle', Str::after($this->field->handle(), $prefix)) + ->isNotEmpty(); + }) + ->first(); + + $fieldset->setContents([ + ...$fieldset->contents(), + 'fields' => collect($fieldset->contents()['fields']) + ->map(function (array $field) use ($config, $prefix) { + if ($field['handle'] === Str::after($this->field->handle(), $prefix)) { + return [ + 'handle' => $field['handle'], + 'field' => $config, + ]; + } + + return $field; + }) + ->all(), + ]); + + $fieldset->save(); + + $this->clearBlinkCaches(); + } + + private function clearBlinkCaches(): void + { + Blink::store('blueprints.found')->flush(); + Blink::store('blueprints.from-file')->flush(); + } +} diff --git a/src/Transformers/BardTransformer.php b/src/Transformers/BardTransformer.php index e32e21d..5ef790d 100644 --- a/src/Transformers/BardTransformer.php +++ b/src/Transformers/BardTransformer.php @@ -6,6 +6,7 @@ use Statamic\Facades\Fieldset; use Statamic\Fields\Field; use Statamic\Fieldtypes\Bard\Augmentor as BardAugmentor; +use Facades\Statamic\Importer\Support\FieldUpdater; use Statamic\Importer\WordPress\Gutenberg; use Statamic\Support\Str; @@ -76,72 +77,9 @@ private function enableBardButtons(): void 'superscript', ]; - $importedField = $this->blueprint->fields()->items() - ->where('handle', $this->field->handle()) - ->filter(fn (array $field) => isset($field['field']) && is_string($field['field'])) - ->first(); - - if ($importedField) { - /** @var \Statamic\Fields\Fieldset $fieldset */ - $fieldHandle = Str::after($importedField['field'], '.'); - $fieldset = Fieldset::find(Str::before($importedField['field'], '.')); - - $fieldset->setContents([ - ...$fieldset->contents(), - 'fields' => collect($fieldset->contents()['fields']) - ->map(function (array $field) use ($buttons, $fieldHandle) { - if ($field['handle'] === $fieldHandle) { - return [ - 'handle' => $field['handle'], - 'field' => array_merge($field['field'], ['buttons' => $buttons]), - ]; - } - - return $field; - }) - ->all(), - ])->save(); - - return; - } - - if ($prefix = $this->field->prefix()) { - /** @var \Statamic\Fields\Fieldset $fieldset */ - $fieldset = $this->blueprint->fields()->items() - ->filter(fn (array $field) => isset($field['import'])) - ->map(fn (array $field) => Fieldset::find($field['import'])) - ->filter(function ($fieldset) use ($prefix) { - return collect($fieldset->fields()->items()) - ->where('handle', Str::after($this->field->handle(), $prefix)) - ->isNotEmpty(); - }) - ->first(); - - $fieldset->setContents([ - ...$fieldset->contents(), - 'fields' => collect($fieldset->contents()['fields']) - ->map(function (array $field) use ($buttons, $prefix) { - if ($field['handle'] === Str::after($this->field->handle(), $prefix)) { - return [ - 'handle' => $field['handle'], - 'field' => array_merge($field['field'], ['buttons' => $buttons]), - ]; - } - - return $field; - }) - ->all(), - ])->save(); - - return; - } - - $this->blueprint->ensureFieldHasConfig( - handle: $this->field->handle(), - config: array_merge($this->field->config(), ['buttons' => $buttons]) - ); - - $this->blueprint->save(); + FieldUpdater::field($this->field) + ->blueprint($this->blueprint) + ->updateFieldConfig(array_merge($this->field->config(), ['buttons' => $buttons])); } private function isGutenbergValue(string $value): bool diff --git a/src/WordPress/Gutenberg.php b/src/WordPress/Gutenberg.php index ea4bd71..322791a 100644 --- a/src/WordPress/Gutenberg.php +++ b/src/WordPress/Gutenberg.php @@ -2,7 +2,9 @@ namespace Statamic\Importer\WordPress; +use Facades\Statamic\Importer\Support\FieldUpdater; use Statamic\Facades\AssetContainer; +use Statamic\Facades\Blink; use Statamic\Facades\Blueprint as BlueprintFacade; use Statamic\Facades\Fieldset; use Statamic\Fields\Blueprint; @@ -399,7 +401,7 @@ protected static function renderHtmlToProsemirror(Field $field, string $html) protected static function ensureBardSet(Blueprint $blueprint, Field $field, string $handle, array $config): void { - $blueprint = BlueprintFacade::find("{$blueprint->namespace()}.{$blueprint->handle()}"); + $blueprint = BlueprintFacade::find($blueprint->fullyQualifiedHandle()); $field = $blueprint->field($field->handle()); $setExists = collect($field->get('sets', []))->contains( @@ -410,86 +412,9 @@ protected static function ensureBardSet(Blueprint $blueprint, Field $field, stri return; } - $importedField = $blueprint->fields()->items() - ->where('handle', $field->handle()) - ->filter(fn (array $field) => isset($field['field']) && is_string($field['field'])) - ->first(); - - if ($importedField) { - /** @var \Statamic\Fields\Fieldset $fieldset */ - $fieldHandle = Str::after($importedField['field'], '.'); - $fieldset = Fieldset::find(Str::before($importedField['field'], '.')); - - $fieldset->setContents([ - ...$fieldset->contents(), - 'fields' => collect($fieldset->contents()['fields']) - ->map(function (array $fieldsetField) use ($field, $handle, $config, $fieldHandle) { - if ($fieldsetField['handle'] === $fieldHandle) { - return [ - 'handle' => $fieldsetField['handle'], - 'field' => array_merge($fieldsetField['field'], [ - ...$field->config(), - 'sets' => array_merge($field->get('sets', []), [ - 'main' => array_merge($field->get('sets.main', []), [ - 'sets' => array_merge($field->get('sets.main.sets', []), [ - $handle => $config, - ]), - ]), - ]), - ]), - ]; - } - - return $field; - }) - ->all(), - ])->save(); - - return; - } - - if ($prefix = $field->prefix()) { - /** @var \Statamic\Fields\Fieldset $fieldset */ - $fieldset = $blueprint->fields()->items() - ->filter(fn (array $field) => isset($field['import'])) - ->map(fn (array $field) => Fieldset::find($field['import'])) - ->filter(function ($fieldset) use ($field, $prefix) { - return collect($fieldset->fields()->items()) - ->where('handle', Str::after($field->handle(), $prefix)) - ->isNotEmpty(); - }) - ->first(); - - $fieldset->setContents([ - ...$fieldset->contents(), - 'fields' => collect($fieldset->contents()['fields']) - ->map(function (array $fieldsetField) use ($field, $handle, $config, $prefix) { - if ($fieldsetField['handle'] === Str::after($field->handle(), $prefix)) { - return [ - 'handle' => $fieldsetField['handle'], - 'field' => array_merge($fieldsetField['field'], [ - ...$field->config(), - 'sets' => array_merge($field->get('sets', []), [ - 'main' => array_merge($field->get('sets.main', []), [ - 'sets' => array_merge($field->get('sets.main.sets', []), [ - $handle => $config, - ]), - ]), - ]), - ]), - ]; - } - - return $fieldsetField; - }) - ->all(), - ])->save(); - - return; - } - - $blueprint - ->ensureFieldHasConfig($field->handle(), [ + FieldUpdater::field($field) + ->blueprint($blueprint) + ->updateFieldConfig([ ...$field->config(), 'sets' => array_merge($field->get('sets', []), [ 'main' => array_merge($field->get('sets.main', []), [ @@ -498,7 +423,6 @@ protected static function ensureBardSet(Blueprint $blueprint, Field $field, stri ]), ]), ]), - ]) - ->save(); + ]); } }