Compare commits

...

2 Commits

Author SHA1 Message Date
tim
306414e24b Update to symfony 7+ 2024-07-05 23:08:05 +02:00
tim
35228c8496 Allow caching of parameter values
Increase composer php version to match
2023-06-12 01:00:36 +02:00
4 changed files with 21 additions and 22 deletions

View File

@ -3,10 +3,10 @@
"description": "Simple bundle for storing parameters in a database", "description": "Simple bundle for storing parameters in a database",
"type": "symfony-bundle", "type": "symfony-bundle",
"require": { "require": {
"php": "^8.0", "php": "^8.1",
"doctrine/orm": "^2.0", "doctrine/orm": "^2.0",
"symfony/form": "^5.0|^6.0", "symfony/form": "^5.0|^6.0|^7.0",
"symfony/twig-bundle": "^5.0|^6.0" "symfony/twig-bundle": "^5.0|^6.0|^7.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -7,7 +7,7 @@ namespace Ardent\ParameterBundle\Controller;
use Ardent\ParameterBundle\Service\ParameterService; use Ardent\ParameterBundle\Service\ParameterService;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Attribute\Route;
class ParameterController extends BaseController class ParameterController extends BaseController
{ {

View File

@ -2,32 +2,23 @@
namespace Ardent\ParameterBundle\Entity; namespace Ardent\ParameterBundle\Entity;
use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\NumberType; use Symfony\Component\Form\Extension\Core\Type\NumberType;
/** #[ORM\Entity()]
* @ORM\Entity #[ORM\Table(name: 'parameter_bundle_parameters')]
* @ORM\Table(name="parameter_bundle_parameters")
*/
class Parameter class Parameter
{ {
/** #[ORM\Id]
* @ORM\Id #[ORM\GeneratedValue]
* @ORM\GeneratedValue(strategy="AUTO") #[ORM\Column]
* @ORM\Column(type="integer") private ?int $id = null;
*/
private ?int $id;
/** #[ORM\Column]
* @ORM\Column(type="string")
*/
private ?string $name; private ?string $name;
/** #[ORM\Column(type: 'string')]
* @ORM\Column(type="string")
*/
private mixed $value; private mixed $value;
public function getId(): int public function getId(): int

View File

@ -15,6 +15,8 @@ class ParameterService
{ {
private array $config; private array $config;
private array $cache = [];
public function __construct( public function __construct(
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
private readonly ParameterBagInterface $parameter, private readonly ParameterBagInterface $parameter,
@ -29,6 +31,10 @@ class ParameterService
*/ */
public function get(string $name, bool $parse = true): mixed public function get(string $name, bool $parse = true): mixed
{ {
if (array_key_exists($name, $this->cache)) {
return $this->cache[$name];
}
$split = explode('_', $name); $split = explode('_', $name);
$category = array_shift($split); $category = array_shift($split);
@ -53,7 +59,8 @@ class ParameterService
$value = $parameter->getValue(); $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 { } else {
return null; return null;
} }
@ -73,6 +80,7 @@ class ParameterService
$parameter->setValue($value); $parameter->setValue($value);
$this->em->persist($parameter); $this->em->persist($parameter);
$this->em->flush(); $this->em->flush();
$this->cache[$name] = $value;
} }
private function supplementConfig(array $rawConfig): array private function supplementConfig(array $rawConfig): array