Skip to content

Commit 0fa18ea

Browse files
franmomuphansys
authored andcommitted
Get cache from MetadataFactory
1 parent 5427146 commit 0fa18ea

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ a release.
3030
- Sluggable: Cast slug to string before passing it as argument 2 to `preg_match()` (#2473)
3131
- Sortable: [SortableGroup] Fix sorting date columns in SQLite (#2462).
3232
- PHPDoc of `AbstractMaterializedPath::removeNode()` and `AbstractMaterializedPath::getChildren()`
33+
- Retrieving the proper metadata cache from Doctrine when using a CacheWarmer.
3334

3435
## [3.7.0] - 2022-05-17
3536
## Added

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ parameters:
180180
count: 1
181181
path: src/Mapping/MappedEventSubscriber.php
182182

183+
-
184+
message: "#^Call to protected method getCache\\(\\) of class Doctrine\\\\Persistence\\\\Mapping\\\\AbstractClassMetadataFactory\\<Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\>\\.$#"
185+
count: 1
186+
path: src/Mapping/MappedEventSubscriber.php
187+
183188
-
184189
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:getFieldMapping\\(\\)\\.$#"
185190
count: 1

src/Mapping/MappedEventSubscriber.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\Common\EventSubscriber;
2222
use Doctrine\ODM\MongoDB\DocumentManager;
2323
use Doctrine\ORM\EntityManagerInterface;
24+
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
2425
use Doctrine\Persistence\Mapping\ClassMetadata;
2526
use Doctrine\Persistence\ObjectManager;
2627
use Gedmo\Mapping\Event\AdapterInterface;
@@ -291,8 +292,17 @@ private function getCacheItemPool(ObjectManager $objectManager): CacheItemPoolIn
291292
return $this->cacheItemPool;
292293
}
293294

295+
// TODO: The user should configure its own cache, we are using the one from Doctrine for BC. We should deprecate using
296+
// the one from Doctrine when the bundle offers an easy way to configure this cache, otherwise users using the bundle
297+
// will see lots of deprecations without an easy way to avoid them.
298+
294299
if ($objectManager instanceof EntityManagerInterface || $objectManager instanceof DocumentManager) {
295-
$metadataCache = $objectManager->getConfiguration()->getMetadataCache();
300+
$metadataFactory = $objectManager->getMetadataFactory();
301+
$getCache = \Closure::bind(static function (AbstractClassMetadataFactory $metadataFactory): ?CacheItemPoolInterface {
302+
return $metadataFactory->getCache();
303+
}, null, \get_class($metadataFactory));
304+
305+
$metadataCache = $getCache($metadataFactory);
296306

297307
if (null !== $metadataCache) {
298308
$this->cacheItemPool = $metadataCache;

src/Tree/Strategy/ORM/Closure.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
use Doctrine\ORM\EntityManagerInterface;
1414
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
1515
use Doctrine\ORM\Mapping\ClassMetadataInfo;
16+
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
1617
use Doctrine\Persistence\Mapping\ClassMetadata;
1718
use Doctrine\Persistence\ObjectManager;
1819
use Gedmo\Exception\RuntimeException;
1920
use Gedmo\Mapping\Event\AdapterInterface;
2021
use Gedmo\Tool\Wrapper\AbstractWrapper;
2122
use Gedmo\Tree\Strategy;
2223
use Gedmo\Tree\TreeListener;
24+
use Psr\Cache\CacheItemPoolInterface;
2325

2426
/**
2527
* This strategy makes tree act like
@@ -194,16 +196,21 @@ public function processMetadataLoad($em, $meta)
194196
}
195197

196198
if (!$hasTheUserExplicitlyDefinedMapping) {
197-
$cacheDriver = $em->getConfiguration()->getMetadataCache();
199+
$metadataFactory = $em->getMetadataFactory();
200+
$getCache = \Closure::bind(static function (AbstractClassMetadataFactory $metadataFactory): ?CacheItemPoolInterface {
201+
return $metadataFactory->getCache();
202+
}, null, \get_class($metadataFactory));
198203

199-
if (null !== $cacheDriver) {
204+
$metadataCache = $getCache($metadataFactory);
205+
206+
if (null !== $metadataCache) {
200207
// @see https://github.com/doctrine/persistence/pull/144
201208
// @see \Doctrine\Persistence\Mapping\AbstractClassMetadataFactory::getCacheKey()
202209
$cacheKey = str_replace('\\', '__', $closureMetadata->getName()).'__CLASSMETADATA__';
203210

204-
$item = $cacheDriver->getItem($cacheKey);
211+
$item = $metadataCache->getItem($cacheKey);
205212

206-
$cacheDriver->save($item->set($closureMetadata));
213+
$metadataCache->save($item->set($closureMetadata));
207214
}
208215
}
209216
}

tests/Gedmo/Mapping/MappingEventSubscriberTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
use Doctrine\Common\EventManager;
1616
use Doctrine\ORM\EntityManager;
1717
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
18+
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
1819
use Gedmo\Mapping\ExtensionMetadataFactory;
1920
use Gedmo\Sluggable\SluggableListener;
2021
use Gedmo\Tests\Mapping\Fixture\Sluggable;
22+
use Psr\Cache\CacheItemPoolInterface;
2123

2224
final class MappingEventSubscriberTest extends ORMMappingTestCase
2325
{
@@ -42,9 +44,14 @@ protected function setUp(): void
4244
$this->em = EntityManager::create($conn, $config, new EventManager());
4345
}
4446

45-
public function testGetConfigurationCachedFromDoctrine(): void
47+
public function testGetMetadataFactoryCacheFromDoctrine(): void
4648
{
47-
$cache = $this->em->getConfiguration()->getMetadataCache();
49+
$metadataFactory = $this->em->getMetadataFactory();
50+
$getCache = \Closure::bind(static function (AbstractClassMetadataFactory $metadataFactory): ?CacheItemPoolInterface {
51+
return $metadataFactory->getCache();
52+
}, null, \get_class($metadataFactory));
53+
54+
$cache = $getCache($metadataFactory);
4855

4956
$cacheKey = ExtensionMetadataFactory::getCacheId(Sluggable::class, 'Gedmo\Sluggable');
5057

0 commit comments

Comments
 (0)