Skip to content

Commit 72d5bae

Browse files
[1.x] fix(core): change caching logic for validation attributes (#4216)
* fix(core): change caching logic for validation attributes * docs: deprecate recently added constant I guess we could get away with just removing it, but as there is a remote chance a third party implementation is already relying on it I wouldn't remove it right now.. * refactor: use string interpolation * fix: call `getRules() instead` of using `$this->rules` directly Changing this also adds support for the `UserValidator` which defines `getRules()` itself
1 parent 6141e5e commit 72d5bae

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

framework/core/src/Foundation/AbstractValidator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ abstract class AbstractValidator
2020
use ExtensionIdTrait;
2121

2222
/**
23+
* @deprecated This constant is no longer used internally and will be removed in a future release.
24+
* Use dynamic cache keys based on class and locale instead.
2325
* @var string
2426
*/
2527
public static $CORE_VALIDATION_CACHE_KEY = 'core.validation.extension_id_class_names';
@@ -96,19 +98,21 @@ protected function getAttributeNames()
9698
{
9799
$cache = resolve(Cache::class);
98100

99-
if ($cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) {
100-
return $cache->get(self::$CORE_VALIDATION_CACHE_KEY);
101+
$cacheKey = 'core.validation.attributes.' . $this->translator->getLocale() . '.' . static::class;
102+
103+
if ($cached = $cache->get($cacheKey)) {
104+
return $cached;
101105
}
102106

103107
$extId = $this->getClassExtensionId();
104108
$attributeNames = [];
105109

106-
foreach (array_keys($this->rules) as $attribute) {
110+
foreach (array_keys($this->getRules()) as $attribute) {
107111
$key = $extId ? "$extId.validation.attributes.$attribute" : "validation.attributes.$attribute";
108112
$attributeNames[$attribute] = $this->translator->trans($key);
109113
}
110114

111-
$cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames);
115+
$cache->forever($cacheKey, $attributeNames);
112116

113117
return $attributeNames;
114118
}

0 commit comments

Comments
 (0)