2.0 RC 1 - new ConfigurationService (#3810)
* use html5 email validation * remove static cache seed, for compatibility with non default (file based) caches * added caching ConfigurationService, removed use of doctrine result cache * simplify configuration API
This commit is contained in:
@@ -9,18 +9,13 @@
|
||||
|
||||
namespace App\Configuration;
|
||||
|
||||
use App\Entity\Configuration;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
interface ConfigLoaderInterface
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
* @return ?Configuration
|
||||
*/
|
||||
public function getConfiguration(string $name): ?Configuration;
|
||||
|
||||
/**
|
||||
* @return Configuration[]
|
||||
* @return array<string, string|null>
|
||||
*/
|
||||
public function getConfigurations(): array;
|
||||
}
|
||||
|
||||
72
src/Configuration/ConfigurationService.php
Normal file
72
src/Configuration/ConfigurationService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Configuration;
|
||||
|
||||
use App\Entity\Configuration;
|
||||
use App\Form\Model\SystemConfiguration;
|
||||
use App\Repository\ConfigurationRepository;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
final class ConfigurationService implements ConfigLoaderInterface
|
||||
{
|
||||
/**
|
||||
* @var array<string, string|null>
|
||||
*/
|
||||
private static array $cacheAll = [];
|
||||
private static bool $initialized = false;
|
||||
|
||||
public function __construct(private ConfigurationRepository $configurationRepository, private CacheInterface $cache)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string|null>
|
||||
*/
|
||||
public function getConfigurations(): array
|
||||
{
|
||||
if (self::$initialized === true) {
|
||||
return self::$cacheAll;
|
||||
}
|
||||
|
||||
self::$cacheAll = $this->cache->get('configurations', function (ItemInterface $item) {
|
||||
$item->expiresAfter(86400); // one day
|
||||
|
||||
return $this->configurationRepository->getConfigurations();
|
||||
});
|
||||
|
||||
self::$initialized = true;
|
||||
|
||||
return self::$cacheAll;
|
||||
}
|
||||
|
||||
public function getConfiguration(string $name): ?Configuration
|
||||
{
|
||||
return $this->configurationRepository->findOneBy(['name' => $name]);
|
||||
}
|
||||
|
||||
public function clearCache(): void
|
||||
{
|
||||
$this->cache->delete('configurations');
|
||||
self::$initialized = false;
|
||||
}
|
||||
|
||||
public function saveConfiguration(Configuration $configuration): void
|
||||
{
|
||||
$this->configurationRepository->saveConfiguration($configuration);
|
||||
$this->clearCache();
|
||||
}
|
||||
|
||||
public function saveSystemConfiguration(SystemConfiguration $model): void
|
||||
{
|
||||
$this->configurationRepository->saveSystemConfiguration($model);
|
||||
$this->clearCache();
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ final class SystemConfiguration
|
||||
{
|
||||
private bool $initialized = false;
|
||||
|
||||
public function __construct(private ConfigLoaderInterface $repository, private ?array $settings)
|
||||
public function __construct(private ConfigLoaderInterface $repository, private array $settings = [])
|
||||
{
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ final class SystemConfiguration
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->repository->getConfigurations() as $configuration) {
|
||||
$this->set($configuration->getName(), $configuration->getValue());
|
||||
foreach ($this->repository->getConfigurations() as $key => $value) {
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
$this->initialized = true;
|
||||
@@ -35,7 +35,6 @@ final class SystemConfiguration
|
||||
*
|
||||
* If no key is given to the method, the entire array will be replaced.
|
||||
*
|
||||
* @see https://github.com/divineomega/array_undot
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
@@ -70,6 +69,7 @@ final class SystemConfiguration
|
||||
/**
|
||||
* This method should be avoided if possible, use plain keys instead.
|
||||
*
|
||||
* @see https://github.com/divineomega/array_undot
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
@@ -219,7 +219,7 @@ final class SystemConfiguration
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<mixed>
|
||||
* @return array<int, array<'saml'|'kimai', string>>
|
||||
*/
|
||||
public function getSamlRolesMapping(): array
|
||||
{
|
||||
@@ -227,7 +227,7 @@ final class SystemConfiguration
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<mixed>
|
||||
* @return array<string, array<mixed>|bool>
|
||||
*/
|
||||
public function getSamlConnection(): array
|
||||
{
|
||||
@@ -235,7 +235,7 @@ final class SystemConfiguration
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<mixed>
|
||||
* @return array<int, array<'saml'|'kimai', string>>
|
||||
*/
|
||||
public function getSamlAttributeMapping(): array
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Configuration\ConfigurationService;
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Event\SystemConfigurationEvent;
|
||||
use App\Form\Model\Configuration;
|
||||
@@ -30,7 +31,6 @@ use App\Form\Type\TimezoneType;
|
||||
use App\Form\Type\TrackingModeType;
|
||||
use App\Form\Type\WeekDaysType;
|
||||
use App\Form\Type\YesNoType;
|
||||
use App\Repository\ConfigurationRepository;
|
||||
use App\Timesheet\LockdownService;
|
||||
use App\Utils\PageSetup;
|
||||
use App\Validator\Constraints\ColorChoices;
|
||||
@@ -61,7 +61,7 @@ use Symfony\Component\Validator\Constraints\Regex;
|
||||
#[IsGranted('system_configuration')]
|
||||
final class SystemConfigurationController extends AbstractController
|
||||
{
|
||||
public function __construct(private EventDispatcherInterface $eventDispatcher, private ConfigurationRepository $repository, private SystemConfiguration $systemConfiguration, private LockdownService $lockdownService)
|
||||
public function __construct(private EventDispatcherInterface $eventDispatcher, private ConfigurationService $repository, private SystemConfiguration $systemConfiguration, private LockdownService $lockdownService)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -142,7 +142,9 @@ final class SystemConfigurationController extends AbstractController
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
try {
|
||||
$this->repository->saveSystemConfiguration($form->getData());
|
||||
/** @var SystemConfigurationModel $saveModel */
|
||||
$saveModel = $form->getData();
|
||||
$this->repository->saveSystemConfiguration($saveModel);
|
||||
$this->flashSuccess('action.update.success');
|
||||
} catch (\Exception $ex) {
|
||||
$this->handleFormUpdateException($ex, $form);
|
||||
|
||||
@@ -21,7 +21,7 @@ final class MailType extends AbstractType
|
||||
$resolver->setDefaults([
|
||||
'label' => 'email',
|
||||
'constraints' => [
|
||||
new Email(['mode' => 'loose'])
|
||||
new Email(['mode' => 'html5'])
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Configuration\ConfigLoaderInterface;
|
||||
use App\Entity\Configuration;
|
||||
use App\Form\Model\SystemConfiguration;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
@@ -17,74 +16,36 @@ use Doctrine\ORM\Exception\ORMException;
|
||||
|
||||
/**
|
||||
* @extends EntityRepository<Configuration>
|
||||
* @internal use App\Configuration\ConfigurationService instead
|
||||
* @final
|
||||
*/
|
||||
class ConfigurationRepository extends EntityRepository implements ConfigLoaderInterface
|
||||
class ConfigurationRepository extends EntityRepository
|
||||
{
|
||||
private const CACHE_KEY = 'ConfigurationRepository_All';
|
||||
/**
|
||||
* @var array<string, Configuration>
|
||||
*/
|
||||
private static array $cacheAll = [];
|
||||
private static bool $initialized = false;
|
||||
|
||||
public function clearCache(): void
|
||||
{
|
||||
self::$cacheAll = [];
|
||||
self::$initialized = false;
|
||||
|
||||
$cache = $this->getEntityManager()->getConfiguration()->getResultCache();
|
||||
if ($cache !== null && $cache->hasItem(self::CACHE_KEY)) {
|
||||
$cache->deleteItem(self::CACHE_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
private function prefillCache(): void
|
||||
{
|
||||
if (self::$initialized === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
$query = $this->createQueryBuilder('s')->getQuery();
|
||||
$query->enableResultCache(86400, self::CACHE_KEY);
|
||||
|
||||
$configs = $query->getResult();
|
||||
foreach ($configs as $config) {
|
||||
self::$cacheAll[$config->getName()] = $config;
|
||||
}
|
||||
self::$initialized = true;
|
||||
}
|
||||
|
||||
public function saveConfiguration(Configuration $configuration): void
|
||||
{
|
||||
$entityManager = $this->getEntityManager();
|
||||
$entityManager->persist($configuration);
|
||||
$entityManager->flush();
|
||||
$this->clearCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Configuration[]
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getConfigurations(): array
|
||||
{
|
||||
$this->prefillCache();
|
||||
$query = $this->createQueryBuilder('s')->select('s.name')->addSelect('s.value')->getQuery();
|
||||
/** @var array<int, array<'name'|'value', string>> $result */
|
||||
$result = $query->getArrayResult();
|
||||
|
||||
return array_values(self::$cacheAll);
|
||||
}
|
||||
|
||||
public function getConfiguration(string $name): ?Configuration
|
||||
{
|
||||
$this->prefillCache();
|
||||
|
||||
if (!\array_key_exists($name, self::$cacheAll)) {
|
||||
return null;
|
||||
$all = [];
|
||||
foreach ($result as $row) {
|
||||
$all[$row['name']] = $row['value'];
|
||||
}
|
||||
|
||||
return self::$cacheAll[$name];
|
||||
return $all;
|
||||
}
|
||||
|
||||
public function saveSystemConfiguration(SystemConfiguration $model)
|
||||
public function saveSystemConfiguration(SystemConfiguration $model): void
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
$em->beginTransaction();
|
||||
@@ -120,7 +81,5 @@ class ConfigurationRepository extends EntityRepository implements ConfigLoaderIn
|
||||
$em->rollback();
|
||||
throw $ex;
|
||||
}
|
||||
|
||||
$this->clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user