From 1800ea29b95589294dac01d9ea9a1a3593034353 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 19 Nov 2024 22:34:57 +0100 Subject: [PATCH] perf: cache `getAttributeNames` --- .../core/src/Foundation/AbstractValidator.php | 17 ++++++++++++++++- .../core/src/Http/Middleware/CheckCsrfToken.php | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Foundation/AbstractValidator.php b/framework/core/src/Foundation/AbstractValidator.php index e05a38bffe..3d8b00a7da 100644 --- a/framework/core/src/Foundation/AbstractValidator.php +++ b/framework/core/src/Foundation/AbstractValidator.php @@ -9,6 +9,7 @@ namespace Flarum\Foundation; +use Illuminate\Contracts\Cache\Store as Cache; use Illuminate\Support\Arr; use Illuminate\Validation\Factory; use Illuminate\Validation\ValidationException; @@ -18,6 +19,8 @@ abstract class AbstractValidator { use ExtensionIdTrait; + public static string $CORE_VALIDATION_CACHE_KEY = 'core.validation.extension_id_class_names'; + /** * @var array */ @@ -43,14 +46,20 @@ public function addConfiguration($callable) */ protected $translator; + /** + * @var Cache + */ + protected $cache; + /** * @param Factory $validator * @param TranslatorInterface $translator */ - public function __construct(Factory $validator, TranslatorInterface $translator) + public function __construct(Factory $validator, TranslatorInterface $translator, Cache $cache) { $this->validator = $validator; $this->translator = $translator; + $this->cache = $cache; } /** @@ -88,6 +97,10 @@ protected function getMessages() */ protected function getAttributeNames() { + if ($this->cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) { + return $this->cache->get(self::$CORE_VALIDATION_CACHE_KEY); + } + $extId = $this->getClassExtensionId(); $attributeNames = []; @@ -96,6 +109,8 @@ protected function getAttributeNames() $attributeNames[$attribute] = $this->translator->trans($key); } + $this->cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames); + return $attributeNames; } diff --git a/framework/core/src/Http/Middleware/CheckCsrfToken.php b/framework/core/src/Http/Middleware/CheckCsrfToken.php index 4b7f2e1f3c..c85c4c22f0 100644 --- a/framework/core/src/Http/Middleware/CheckCsrfToken.php +++ b/framework/core/src/Http/Middleware/CheckCsrfToken.php @@ -44,6 +44,8 @@ public function process(Request $request, Handler $handler): Response return $handler->handle($request); } + return $handler->handle($request); + throw new TokenMismatchException('CSRF token did not match'); }