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

[5.x] Reduce the number of times the fieldsCache is reset #9585

Merged
merged 9 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 20 additions & 1 deletion src/Fields/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +43,8 @@ class Blueprint implements Arrayable, ArrayAccess, Augmentable, QueryableValue
protected $ensuredFields = [];
protected $afterSaveCallbacks = [];
protected $withEvents = true;
protected $lastBlueprintHandle = null;

private ?Columns $columns = null;

public function setHandle(string $handle)
Expand Down Expand Up @@ -623,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());
Expand Down
19 changes: 19 additions & 0 deletions src/Support/Traits/InvadesProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Statamic\Support\Traits;

trait InvadesProperties
{
protected function invade($object, $property)
{
if (! $property) {
return null;
}

if (is_string($property) || ! is_callable($property)) {
return (fn () => $this->{$property})->call($object);
}

return $property->call($object);
}
}
Loading