From f653763073a183757a1b79881b04b5174bb4a693 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 24 Feb 2024 13:57:01 -0600 Subject: [PATCH 1/7] Update Blueprint.php --- src/Fields/Blueprint.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index e1278480f96..bee75d6b135 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -42,6 +42,8 @@ class Blueprint implements Arrayable, ArrayAccess, Augmentable, QueryableValue protected $ensuredFields = []; protected $afterSaveCallbacks = []; protected $withEvents = true; + protected $lastEntryBlueprint = null; + private ?Columns $columns = null; public function setHandle(string $handle) @@ -305,7 +307,18 @@ public function setParent($parent) { $this->parent = $parent; - $this->resetFieldsCache(); + $handle = (function () { + if (property_exists($this, 'blueprint')) { + return $this->blueprint; + } + + return null; + })->call($parent); + + if ($handle == null || $handle != $this->lastEntryBlueprint) { + $this->resetFieldsCache(); + $this->lastEntryBlueprint = $handle; + } return $this; } From d2f212a8b536fe320c7a30e3bbd7e47ceefc010d Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 24 Feb 2024 14:29:59 -0600 Subject: [PATCH 2/7] Refactors/cleanup --- src/Fields/Blueprint.php | 34 ++++++++++++++---------- src/Support/Traits/InvadesProperties.php | 15 +++++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/Support/Traits/InvadesProperties.php diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index bee75d6b135..e71789f5d92 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -26,10 +26,11 @@ use Statamic\Facades\Path; use Statamic\Support\Arr; use Statamic\Support\Str; +use Statamic\Support\Traits\InvadesProperties; class Blueprint implements Arrayable, ArrayAccess, Augmentable, QueryableValue { - use ExistsAsFile, HasAugmentedData; + use ExistsAsFile, HasAugmentedData, InvadesProperties; protected $handle; protected $namespace; @@ -42,7 +43,7 @@ class Blueprint implements Arrayable, ArrayAccess, Augmentable, QueryableValue protected $ensuredFields = []; protected $afterSaveCallbacks = []; protected $withEvents = true; - protected $lastEntryBlueprint = null; + protected $lastBlueprintHandle = null; private ?Columns $columns = null; @@ -307,18 +308,7 @@ public function setParent($parent) { $this->parent = $parent; - $handle = (function () { - if (property_exists($this, 'blueprint')) { - return $this->blueprint; - } - - return null; - })->call($parent); - - if ($handle == null || $handle != $this->lastEntryBlueprint) { - $this->resetFieldsCache(); - $this->lastEntryBlueprint = $handle; - } + $this->resetFieldsCache(); return $this; } @@ -636,6 +626,22 @@ public function validateUniqueHandles() protected function resetFieldsCache() { + if ($this->parent) { + $blueprintHandle = $this->invade($this->parent, function () { + if (property_exists($this, 'blueprint')) { + return $this->blueprint; + } + + return null; + }); + + if ($blueprintHandle && $blueprintHandle === $this->lastBlueprintHandle) { + return $this; + } + + $this->lastBlueprintHandle = $blueprintHandle; + } + $this->fieldsCache = null; Blink::forget($this->contentsBlinkKey()); diff --git a/src/Support/Traits/InvadesProperties.php b/src/Support/Traits/InvadesProperties.php new file mode 100644 index 00000000000..f28a0979437 --- /dev/null +++ b/src/Support/Traits/InvadesProperties.php @@ -0,0 +1,15 @@ + $this->{$property})->call($object); + } + + return $property->call($object); + } +} From 9dc9b99e3d55fbadc165a8d084adcdb259df4957 Mon Sep 17 00:00:00 2001 From: John Koster Date: Sat, 24 Feb 2024 14:37:03 -0600 Subject: [PATCH 3/7] Code hardening --- src/Support/Traits/InvadesProperties.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Support/Traits/InvadesProperties.php b/src/Support/Traits/InvadesProperties.php index f28a0979437..8596867fcaf 100644 --- a/src/Support/Traits/InvadesProperties.php +++ b/src/Support/Traits/InvadesProperties.php @@ -6,7 +6,11 @@ trait InvadesProperties { protected function invade($object, $property) { - if (! is_callable($property)) { + if (! $property) { + return null; + } + + if (is_string($property) || ! is_callable($property)) { return (fn () => $this->{$property})->call($object); } From 04263f7a1da1593fbbba443b83b1e1e6725d6de5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 15 Apr 2024 15:28:17 -0400 Subject: [PATCH 4/7] viz --- src/Fields/Blueprint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index e957c445777..7fbe70f4407 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -43,7 +43,7 @@ class Blueprint implements Arrayable, ArrayAccess, Augmentable, QueryableValue protected $ensuredFields = []; protected $afterSaveCallbacks = []; protected $withEvents = true; - protected $lastBlueprintHandle = null; + private $lastBlueprintHandle = null; private ?Columns $columns = null; From be8897d84cf6d808e9fac16e0933bd39c0781475 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 15 Apr 2024 15:28:33 -0400 Subject: [PATCH 5/7] simplify. dont need to introduce a trait. --- src/Fields/Blueprint.php | 8 +------- src/Support/Traits/InvadesProperties.php | 19 ------------------- 2 files changed, 1 insertion(+), 26 deletions(-) delete mode 100644 src/Support/Traits/InvadesProperties.php diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index 7fbe70f4407..47a7cfbba3c 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -639,13 +639,7 @@ public function validateUniqueHandles() protected function resetFieldsCache() { if ($this->parent) { - $blueprintHandle = $this->invade($this->parent, function () { - if (property_exists($this, 'blueprint')) { - return $this->blueprint; - } - - return null; - }); + $blueprintHandle = (fn () => property_exists($this, 'blueprint') ? $this->blueprint : null)->call($this->parent); if ($blueprintHandle && $blueprintHandle === $this->lastBlueprintHandle) { return $this; diff --git a/src/Support/Traits/InvadesProperties.php b/src/Support/Traits/InvadesProperties.php deleted file mode 100644 index 8596867fcaf..00000000000 --- a/src/Support/Traits/InvadesProperties.php +++ /dev/null @@ -1,19 +0,0 @@ - $this->{$property})->call($object); - } - - return $property->call($object); - } -} From 4b950139fc342ef2ea56440b7d9ee401ab6a67be Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 15 Apr 2024 15:29:08 -0400 Subject: [PATCH 6/7] oops --- src/Fields/Blueprint.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index 47a7cfbba3c..9bf322a9cac 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -26,11 +26,10 @@ use Statamic\Facades\Path; use Statamic\Support\Arr; use Statamic\Support\Str; -use Statamic\Support\Traits\InvadesProperties; class Blueprint implements Arrayable, ArrayAccess, Augmentable, QueryableValue { - use ExistsAsFile, HasAugmentedData, InvadesProperties; + use ExistsAsFile, HasAugmentedData; protected $handle; protected $namespace; From 9eb3bc6f17ea1cf14049683263857d7e8f850fdb Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 15 Apr 2024 15:30:47 -0400 Subject: [PATCH 7/7] shorten --- src/Fields/Blueprint.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fields/Blueprint.php b/src/Fields/Blueprint.php index 9bf322a9cac..2e77a4ec8ee 100644 --- a/src/Fields/Blueprint.php +++ b/src/Fields/Blueprint.php @@ -638,13 +638,13 @@ public function validateUniqueHandles() protected function resetFieldsCache() { if ($this->parent) { - $blueprintHandle = (fn () => property_exists($this, 'blueprint') ? $this->blueprint : null)->call($this->parent); + $blueprint = (fn () => property_exists($this, 'blueprint') ? $this->blueprint : null)->call($this->parent); - if ($blueprintHandle && $blueprintHandle === $this->lastBlueprintHandle) { + if ($blueprint && $blueprint === $this->lastBlueprintHandle) { return $this; } - $this->lastBlueprintHandle = $blueprintHandle; + $this->lastBlueprintHandle = $blueprint; } $this->fieldsCache = null;