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,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