Skip to content

Commit

Permalink
perf: cache getAttributeNames
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideIadeluca committed Nov 19, 2024
1 parent e761327 commit 1800ea2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 16 additions & 1 deletion framework/core/src/Foundation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,8 @@ abstract class AbstractValidator
{
use ExtensionIdTrait;

public static string $CORE_VALIDATION_CACHE_KEY = 'core.validation.extension_id_class_names';

/**
* @var array
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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 = [];

Expand All @@ -96,6 +109,8 @@ protected function getAttributeNames()
$attributeNames[$attribute] = $this->translator->trans($key);
}

$this->cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames);

return $attributeNames;
}

Expand Down
2 changes: 2 additions & 0 deletions framework/core/src/Http/Middleware/CheckCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Check failure on line 49 in framework/core/src/Http/Middleware/CheckCsrfToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Unreachable statement - code above always terminates.

Check failure on line 49 in framework/core/src/Http/Middleware/CheckCsrfToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Unreachable statement - code above always terminates.

Check failure on line 49 in framework/core/src/Http/Middleware/CheckCsrfToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Unreachable statement - code above always terminates.

Check failure on line 49 in framework/core/src/Http/Middleware/CheckCsrfToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Unreachable statement - code above always terminates.

Check failure on line 49 in framework/core/src/Http/Middleware/CheckCsrfToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Unreachable statement - code above always terminates.
}

Expand Down

0 comments on commit 1800ea2

Please sign in to comment.