Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions src/Entries/AugmentedEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ public function keys()
return $this->cachedKeys;
}

return $this->cachedKeys = $this->data->keys()
->merge($this->data->supplements()->keys())
->merge($this->commonKeys())
->merge($this->blueprintFields()->keys())
->unique()->sort()->values()->all();
$keys = array_merge(
collect($this->data->keys())->all(),
array_keys(collect($this->data->supplements())->all()),
$this->commonKeys(),
array_keys($this->blueprintFields()->all()),
);

$keys = array_unique($keys, SORT_REGULAR);
asort($keys, SORT_REGULAR);

return $this->cachedKeys = array_values($keys);
}

private function commonKeys()
Expand Down
12 changes: 12 additions & 0 deletions src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public function __clone()

public function id($id = null)
{
if (func_num_args() === 0) {
return $this->id;
}

return $this->fluentlyGetOrSet('id')->args(func_get_args());
}

Expand All @@ -113,6 +117,10 @@ public function getBulkAugmentationReferenceKey(): ?string

public function locale($locale = null)
{
if (func_num_args() === 0) {
return $this->locale ?? Site::default()->handle();
}

return $this
->fluentlyGetOrSet('locale')
->setter(function ($locale) {
Expand Down Expand Up @@ -187,6 +195,10 @@ public function blueprint($blueprint = null)
{
$key = 'entry-'.($this->id() ?? 'obj-'.spl_object_id($this)).'-blueprint';

if (func_num_args() === 0 && ($cached = Blink::get($key)) !== null) {
return $cached;
}

return $this
->fluentlyGetOrSet('blueprint')
->getter(function ($blueprint) use ($key) {
Expand Down
3 changes: 1 addition & 2 deletions src/Extensions/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Support\Arr;
use Illuminate\Translation\Translator as BaseTranslator;
use Statamic\Statamic;
use Statamic\Support\Str;

class Translator extends BaseTranslator
{
Expand All @@ -21,7 +20,7 @@ public function __construct(Filesystem $files, Loader $loader, $locale)

public function parseKey($key)
{
if (Statamic::isCpRoute() && Str::startsWith($key, 'validation.')) {
if (is_string($key) && str_starts_with($key, 'validation.') && Statamic::isCpRoute()) {
$key = 'statamic::'.$key;
}

Expand Down
31 changes: 30 additions & 1 deletion src/Fields/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\View\Antlers\Parser;
use Statamic\Facades\Compare;
use Statamic\Fieldtypes;
use Statamic\Support\Str;
use Statamic\View\Antlers\Language\Parser\DocumentTransformer;
use Statamic\View\Cascade;
Expand All @@ -26,6 +27,21 @@ class Value implements ArrayAccess, IteratorAggregate, JsonSerializable
protected $fieldtype;
protected $augmentable;
protected $shallow = false;
private $augmentedFor;
private $augmented;

/**
* Fieldtypes whose augmented value only depends on the raw value and the
* field's config, so it can be kept rather than worked out on every read.
*/
private const REMEMBERS_AUGMENTED_VALUE = [
Fieldtypes\ButtonGroup::class => true,
Fieldtypes\Radio::class => true,
Fieldtypes\Select::class => true,
Fieldtypes\Text::class => true,
Fieldtypes\Textarea::class => true,
Fieldtypes\Toggle::class => true,
];

public function __construct($value, $handle = null, $fieldtype = null, $augmentable = null, $shallow = false)
{
Expand Down Expand Up @@ -84,9 +100,22 @@ public function value()

if ($raw === null) {
$raw = $this->fieldtype->field()?->defaultValue() ?? null;

return $this->getAugmentedValue($raw);
}

if ($this->augmentedFor === $this->fieldtype) {
return $this->augmented;
}

return $this->getAugmentedValue($raw);
$value = $this->getAugmentedValue($raw);

if (isset(self::REMEMBERS_AUGMENTED_VALUE[get_class($this->fieldtype)])) {
$this->augmentedFor = $this->fieldtype;
$this->augmented = $value;
}

return $value;
}

private function getAugmentedValue($raw)
Expand Down
29 changes: 28 additions & 1 deletion src/Sites/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Statamic\View\Antlers\Language\Runtime\RuntimeParser;
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
use Statamic\View\Cascade;
use WeakMap;

class Site implements Augmentable, QueryableValue
{
Expand All @@ -24,6 +25,12 @@ class Site implements Augmentable, QueryableValue
protected $rawConfig;
protected $isDefault;

/**
* The absolute URLs of sites with relative URLs, kept per request and keyed
* on everything that goes into them.
*/
private static ?WeakMap $absoluteUrls = null;

public function __construct($handle, $config, $isDefault = false)
{
$this->handle = $handle;
Expand Down Expand Up @@ -79,7 +86,27 @@ public function attribute($key, $default = null)

public function absoluteUrl()
{
return URL::makeAbsolute($this->url());
$configUrl = $this->config['url'] ?? null;

// Only a relative site URL depends on the request.
if (! is_string($configUrl) || $configUrl === '' || preg_match('#^(//|[a-z][a-z0-9+.\-]*:)#i', $configUrl)) {
return URL::makeAbsolute($this->url());
}

$request = app('request');
$key = $configUrl."\0".(URL::isEnforcingTrailingSlashes() ? '1' : '0')."\0".url()->to('/')."\0".$request->getScriptName();

self::$absoluteUrls ??= new WeakMap;
$memo = self::$absoluteUrls[$request] ?? [];

if (isset($memo[$key])) {
return $memo[$key];
}

$memo[$key] = $absolute = URL::makeAbsolute($this->url());
self::$absoluteUrls[$request] = $memo;

return $absolute;
}

public function relativePath($url)
Expand Down
25 changes: 14 additions & 11 deletions src/Structures/AugmentedPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ public function keys()
return $this->cachedKeys;
}

$keys = collect($this->hasEntry
? parent::keys()
: ['title', 'url', 'uri', 'permalink', 'id']);

$keys = $keys
->merge($this->page->data()->keys())
->merge($this->page->supplements()->keys())
->merge($this->blueprintFields()->keys())
->merge(['entry_id']);
$keys = array_merge(
collect($this->hasEntry ? parent::keys() : ['title', 'url', 'uri', 'permalink', 'id'])->all(),
array_keys($this->page->data()->all()),
array_keys(collect($this->page->supplements())->all()),
array_keys($this->blueprintFields()->all()),
['entry_id'],
);

if (Statamic::isApiRoute()) {
$keys = $this->apiKeys($keys)->all();
}

$keys = Statamic::isApiRoute() ? $this->apiKeys($keys) : $keys;
$keys = array_unique($keys, SORT_REGULAR);
asort($keys, SORT_REGULAR);

return $this->cachedKeys = $keys->unique()->sort()->values()->all();
return $this->cachedKeys = array_values($keys);
}

private function apiKeys($keys)
Expand Down
9 changes: 8 additions & 1 deletion src/Support/FluentGetterSetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class FluentGetterSetter
protected $setter;
protected $afterSetter;

private static array $readers = [];

/**
* Instantiate fluent getter/setter helper.
*
Expand Down Expand Up @@ -132,7 +134,12 @@ protected function runSetterLogic($value)

private function getProperty($object, $property)
{
return (fn () => $this->{$property})->call($object);
$class = get_class($object);

// One property reader per class, rather than a new bound closure on every read.
$reader = self::$readers[$class] ??= Closure::bind(static fn ($object, $property) => $object->{$property}, null, $class);

return $reader($object, $property);
}

private function setProperty($object, $property, $value)
Expand Down
49 changes: 49 additions & 0 deletions tests/Fields/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,55 @@ public function shouldParseAntlersFromRawString(): bool

$this->assertEquals('augmented_value', (string) $value);
}

#[Test]
public function it_remembers_the_augmented_value_of_fieldtypes_that_only_depend_on_the_raw_value()
{
$fieldtype = new class extends \Statamic\Fieldtypes\Text
{
public $augmented = 0;

public function augment($value)
{
$this->augmented++;

return strtoupper($value);
}
};

// A subclass could do anything when augmenting, so it isn't remembered.
$value = new Value('foo', 'test', $fieldtype);
$this->assertEquals('FOO', $value->value());
$this->assertEquals('FOO', $value->value());
$this->assertSame(2, $fieldtype->augmented);

$select = (new \Statamic\Fieldtypes\Select)->setField(new Field('test', ['type' => 'select', 'options' => ['a' => 'Alfa']]));
$value = new Value('a', 'test', $select);
$this->assertSame($value->value(), $value->value());
$this->assertEquals('Alfa', $value->value()->label());

$value->setFieldtype((new \Statamic\Fieldtypes\Select)->setField(new Field('test', ['type' => 'select', 'options' => ['a' => 'Changed']])));
$this->assertEquals('Changed', $value->value()->label());
}

#[Test]
public function it_augments_other_fieldtypes_on_every_read()
{
$fieldtype = new class extends Fieldtype
{
public $augmented = 0;

public function augment($value)
{
return $value.' '.++$this->augmented;
}
};

$value = new Value('foo', 'test', $fieldtype);

$this->assertEquals('foo 1', $value->value());
$this->assertEquals('foo 2', $value->value());
}
}

class DummyAugmentable implements \Statamic\Contracts\Data\Augmentable
Expand Down
21 changes: 21 additions & 0 deletions tests/Sites/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ public function gets_absolute_url()
);
}

#[Test]
public function the_absolute_url_of_a_relative_site_follows_the_request()
{
$site = new Site('en', ['url' => '/fr/']);

$this->assertEquals('http://absolute-url-resolved-from-request.com/fr', $site->absoluteUrl());

$this->get('https://another-host.com/something');
$this->assertEquals('https://another-host.com/fr', $site->absoluteUrl());

url()->forceRootUrl('https://forced-root.com');
$this->assertEquals('https://forced-root.com/fr', $site->absoluteUrl());

url()->forceRootUrl(null);
$this->assertEquals('https://another-host.com/fr', $site->absoluteUrl());

\Statamic\Facades\URL::enforceTrailingSlashes();
$this->assertEquals('https://another-host.com/fr/', $site->absoluteUrl());
\Statamic\Facades\URL::enforceTrailingSlashes(false);
}

#[Test]
public function gets_path()
{
Expand Down
Loading