From b9911bafd77801d62856f1857ea7a906dbc2ff64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kon=C3=A1=C5=A1?= Date: Sun, 21 Jul 2024 04:12:09 +0200 Subject: [PATCH 1/2] Composer: allow symfony/console 7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a6c7542..98867d6 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "require": { "php": ">=8.1", "nette/di": "^3.1.2", - "symfony/console": "^5.3.0 || ^6.2.0", + "symfony/console": "^5.3.0 || ^6.2.0 || ^7.0.0 ", "nettrine/annotations": "^0.8.0 || ^0.9.0", "nettrine/cache": "^0.4.0 || ^0.5.0", "nettrine/dbal": "^0.8.0 || ^0.9.0", From 7d527f4d917d22235cd39b89fd5c76a7bb06311f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kon=C3=A1=C5=A1?= Date: Sun, 21 Jul 2024 12:54:15 +0200 Subject: [PATCH 2/2] Add option to turn off second level cache --- .docs/README.md | 6 ++++++ src/DI/OrmCacheExtension.php | 9 +++++++-- tests/Cases/DI/OrmCacheExtension.phpt | 29 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.docs/README.md b/.docs/README.md index f3ad068..aa4990d 100755 --- a/.docs/README.md +++ b/.docs/README.md @@ -102,6 +102,12 @@ nettrine.orm.cache: secondLevelCache: @cacheConfigurationFactory::create('bar') ``` +You can turn off `secondLevelCache` by setting it to `false`: + +```neon +nettrine.orm.cache: + secondLevelCache: false +``` ### `symfony/console` diff --git a/src/DI/OrmCacheExtension.php b/src/DI/OrmCacheExtension.php index 90a971f..9a287c4 100644 --- a/src/DI/OrmCacheExtension.php +++ b/src/DI/OrmCacheExtension.php @@ -26,7 +26,7 @@ public function getConfigSchema(): Schema 'hydrationCache' => $this->getServiceSchema(), 'metadataCache' => $this->getServiceSchema(), 'resultCache' => $this->getServiceSchema(), - 'secondLevelCache' => $this->getServiceSchema(), + 'secondLevelCache' => Expect::anyOf($this->getServiceSchema(), false), ]); } @@ -93,8 +93,13 @@ private function loadMetadataCacheConfiguration(): void private function loadSecondLevelCacheConfiguration(): void { - $builder = $this->getContainerBuilder(); $config = $this->config; + + if ($config->secondLevelCache === false) { + return; + } + + $builder = $this->getContainerBuilder(); $configurationDef = $this->getConfigurationDef(); if ($config->secondLevelCache !== null) { diff --git a/tests/Cases/DI/OrmCacheExtension.phpt b/tests/Cases/DI/OrmCacheExtension.phpt index edc627c..457ebf8 100644 --- a/tests/Cases/DI/OrmCacheExtension.phpt +++ b/tests/Cases/DI/OrmCacheExtension.phpt @@ -30,6 +30,8 @@ Toolkit::test(function (): void { Assert::type(PhpFileCache::class, $em->getConfiguration()->getMetadataCacheImpl()); Assert::type(PhpFileCache::class, $em->getConfiguration()->getQueryCacheImpl()); Assert::type(PhpFileCache::class, $em->getConfiguration()->getResultCacheImpl()); + Assert::true($em->getConfiguration()->isSecondLevelCacheEnabled()); + Assert::notNull($em->getConfiguration()->getSecondLevelCacheConfiguration()); }); // Provide cache drivers @@ -57,4 +59,31 @@ Toolkit::test(function (): void { Assert::type(ArrayCache::class, $em->getConfiguration()->getMetadataCacheImpl()); Assert::type(ApcuCache::class, $em->getConfiguration()->getQueryCacheImpl()); Assert::type(ArrayCache::class, $em->getConfiguration()->getResultCacheImpl()); + Assert::true($em->getConfiguration()->isSecondLevelCacheEnabled()); + Assert::notNull($em->getConfiguration()->getSecondLevelCacheConfiguration()); +}); + +// Turn off second level cache +Toolkit::test(function (): void { + $container = Container::of() + ->withDefaults() + ->withCompiler(function (Compiler $compiler): void { + $compiler->addExtension('nettrine.orm.cache', new OrmCacheExtension()); + $compiler->addConfig([ + 'nettrine.orm.cache' => [ + 'secondLevelCache' => false, + ], + ]); + }) + ->build(); + + /** @var EntityManagerDecorator $em */ + $em = $container->getByType(EntityManagerDecorator::class); + + Assert::false($em->getConfiguration()->isSecondLevelCacheEnabled()); + Assert::null($em->getConfiguration()->getSecondLevelCacheConfiguration()); + Assert::type(PhpFileCache::class, $em->getConfiguration()->getHydrationCacheImpl()); + Assert::type(PhpFileCache::class, $em->getConfiguration()->getMetadataCacheImpl()); + Assert::type(PhpFileCache::class, $em->getConfiguration()->getQueryCacheImpl()); + Assert::type(PhpFileCache::class, $em->getConfiguration()->getResultCacheImpl()); });