From 35228c84963c7541482fd652d13a8cef4d510d20 Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 12 Jun 2023 01:00:36 +0200 Subject: [PATCH] Allow caching of parameter values Increase composer php version to match --- composer.json | 2 +- src/Service/ParameterService.php | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 0b1d43b..7a373be 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Simple bundle for storing parameters in a database", "type": "symfony-bundle", "require": { - "php": "^8.0", + "php": "^8.1", "doctrine/orm": "^2.0", "symfony/form": "^5.0|^6.0", "symfony/twig-bundle": "^5.0|^6.0" diff --git a/src/Service/ParameterService.php b/src/Service/ParameterService.php index 3ddd508..93db331 100644 --- a/src/Service/ParameterService.php +++ b/src/Service/ParameterService.php @@ -15,6 +15,8 @@ class ParameterService { private array $config; + private array $cache = []; + public function __construct( private readonly EntityManagerInterface $em, private readonly ParameterBagInterface $parameter, @@ -29,6 +31,10 @@ class ParameterService */ public function get(string $name, bool $parse = true): mixed { + if (array_key_exists($name, $this->cache)) { + return $this->cache[$name]; + } + $split = explode('_', $name); $category = array_shift($split); @@ -53,7 +59,8 @@ class ParameterService $value = $parameter->getValue(); } - return Parameter::parseValue($value, $this->config[$category][$name]['type']); + $this->cache[$name] = Parameter::parseValue($value, $this->config[$category][$name]['type']); + return $this->cache[$name]; } else { return null; } @@ -73,6 +80,7 @@ class ParameterService $parameter->setValue($value); $this->em->persist($parameter); $this->em->flush(); + $this->cache[$name] = $value; } private function supplementConfig(array $rawConfig): array