Skip to content

Commit b22bac1

Browse files
committed
Use cache from persistence layer configuration
Instead of relying on the deprecated ClassMetadataFactory::getCacheDriver method, now we get the metadata cache from the configuration.
1 parent 8c45c1c commit b22bac1

File tree

9 files changed

+42
-44
lines changed

9 files changed

+42
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ a release.
1919
---
2020

2121
## [Unreleased]
22+
## Changed
23+
- Removed call to deprecated `ClassMetadataFactory::getCacheDriver()` method.
24+
- Dropped support for doctrine/mongodb-odm < 2.3.
2225

2326
## [3.6.0] - 2022-03-19
2427
### Added

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@
5151
},
5252
"require-dev": {
5353
"doctrine/dbal": "^2.13.1 || ^3.2",
54-
"doctrine/deprecations": "^0.5.3",
5554
"doctrine/doctrine-bundle": "^2.3",
56-
"doctrine/mongodb-odm": "^2.2",
55+
"doctrine/mongodb-odm": "^2.3",
5756
"doctrine/orm": "^2.10.2",
5857
"friendsofphp/php-cs-fixer": "~3.4.0",
5958
"nesbot/carbon": "^2.55",
@@ -68,7 +67,7 @@
6867
"conflict": {
6968
"doctrine/cache": "<1.11",
7069
"doctrine/dbal": "<2.13.1 || ^3.0 <3.2",
71-
"doctrine/mongodb-odm": "<2.2",
70+
"doctrine/mongodb-odm": "<2.3",
7271
"doctrine/orm": "<2.10.2",
7372
"sebastian/comparator": "<2.0"
7473
},

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ parameters:
175175
count: 1
176176
path: src/Mapping/MappedEventSubscriber.php
177177

178-
-
179-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadataFactory\\<Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\<object\\>\\>\\:\\:getCacheDriver\\(\\)\\.$#"
180-
count: 1
181-
path: src/Mapping/MappedEventSubscriber.php
182-
183178
-
184179
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
185180
count: 1

src/Mapping/MappedEventSubscriber.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Doctrine\Common\Cache\Psr6\CacheAdapter;
1919
use Doctrine\Common\EventArgs;
2020
use Doctrine\Common\EventSubscriber;
21+
use Doctrine\ODM\MongoDB\DocumentManager;
22+
use Doctrine\ORM\EntityManagerInterface;
2123
use Doctrine\Persistence\Mapping\ClassMetadata;
2224
use Doctrine\Persistence\ObjectManager;
2325
use Gedmo\Mapping\Event\AdapterInterface;
@@ -286,16 +288,17 @@ private function getCacheItemPool(ObjectManager $objectManager): CacheItemPoolIn
286288
return $this->cacheItemPool;
287289
}
288290

289-
$factory = $objectManager->getMetadataFactory();
290-
$cacheDriver = $factory->getCacheDriver();
291+
if ($objectManager instanceof EntityManagerInterface || $objectManager instanceof DocumentManager) {
292+
$metadataCache = $objectManager->getConfiguration()->getMetadataCache();
291293

292-
if (null === $cacheDriver) {
293-
$this->cacheItemPool = new ArrayAdapter();
294+
if (null !== $metadataCache) {
295+
$this->cacheItemPool = $metadataCache;
294296

295-
return $this->cacheItemPool;
297+
return $this->cacheItemPool;
298+
}
296299
}
297300

298-
$this->cacheItemPool = CacheAdapter::wrap($cacheDriver);
301+
$this->cacheItemPool = new ArrayAdapter();
299302

300303
return $this->cacheItemPool;
301304
}

src/Tree/Strategy/ORM/Closure.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace Gedmo\Tree\Strategy\ORM;
1111

12-
use Doctrine\Common\Cache\Cache;
1312
use Doctrine\DBAL\Connection;
1413
use Doctrine\ORM\EntityManagerInterface;
1514
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
@@ -195,15 +194,16 @@ public function processMetadataLoad($em, $meta)
195194
}
196195

197196
if (!$hasTheUserExplicitlyDefinedMapping) {
198-
$cacheDriver = $cmf->getCacheDriver();
197+
$cacheDriver = $em->getConfiguration()->getMetadataCache();
199198

200-
if ($cacheDriver instanceof Cache) {
199+
if (null !== $cacheDriver) {
201200
// @see https://github.com/doctrine/persistence/pull/144
202201
// @see \Doctrine\Persistence\Mapping\AbstractClassMetadataFactory::getCacheKey()
203-
$cacheDriver->save(
204-
str_replace('\\', '__', $closureMetadata->getName()).'__CLASSMETADATA__',
205-
$closureMetadata
206-
);
202+
$cacheKey = str_replace('\\', '__', $closureMetadata->getName()).'__CLASSMETADATA__';
203+
204+
$item = $cacheDriver->getItem($cacheKey);
205+
206+
$cacheDriver->save($item->set($closureMetadata));
207207
}
208208
}
209209
}

tests/Gedmo/Mapping/MappingEventSubscriberTest.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Doctrine\Common\EventManager;
16-
use Doctrine\Deprecations\Deprecation;
17-
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
1816
use Doctrine\ORM\EntityManager;
1917
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
20-
use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionPropertyBase;
18+
use Gedmo\Mapping\ExtensionMetadataFactory;
2119
use Gedmo\Sluggable\SluggableListener;
22-
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
20+
use Gedmo\Tests\Mapping\Fixture\Sluggable;
2321

2422
final class MappingEventSubscriberTest extends ORMMappingTestCase
2523
{
26-
use VerifyDeprecations;
27-
use ExpectDeprecationTrait;
28-
2924
/**
3025
* @var EntityManager
3126
*/
@@ -47,28 +42,25 @@ protected function setUp(): void
4742
$this->em = EntityManager::create($conn, $config, new EventManager());
4843
}
4944

50-
/**
51-
* @group legacy
52-
*/
5345
public function testGetConfigurationCachedFromDoctrine(): void
5446
{
55-
// doctrine/persistence changed from trigger_error to doctrine/deprecations in 2.2.1. In 2.2.2 this trait was
56-
// added, this is used to know if the doctrine/persistence version is using trigger_error or
57-
// doctrine/deprecations. This "if" check can be removed once we drop support for doctrine/persistence < 2.2.1
58-
if (trait_exists(TypedNoDefaultReflectionPropertyBase::class)) {
59-
Deprecation::enableWithTriggerError();
47+
$cache = $this->em->getConfiguration()->getMetadataCache();
6048

61-
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/persistence/issues/184');
62-
} else {
63-
$this->expectDeprecation('Doctrine\Persistence\Mapping\AbstractClassMetadataFactory::getCacheDriver is deprecated. Use getCache() instead.');
64-
}
49+
$cacheKey = ExtensionMetadataFactory::getCacheId(Sluggable::class, 'Gedmo\Sluggable');
50+
51+
static::assertFalse($cache->hasItem($cacheKey));
6552

6653
$subscriber = new SluggableListener();
67-
$subscriber->getExtensionMetadataFactory($this->em);
54+
$classMetadata = $this->em->getClassMetadata(Sluggable::class);
55+
$subscriber->getExtensionMetadataFactory($this->em)->getExtensionMetadata($classMetadata);
56+
57+
static::assertTrue($cache->hasItem($cacheKey));
6858
}
6959

7060
protected function getUsedEntityFixtures(): array
7161
{
72-
return [];
62+
return [
63+
Sluggable::class,
64+
];
7365
}
7466
}

tests/Gedmo/Mapping/TreeMappingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public function testApcCached(): void
8686
$this->em->getClassMetadata(self::YAML_CLOSURE_CATEGORY);
8787
$this->em->getClassMetadata(CategoryClosureWithoutMapping::class);
8888

89-
$meta = $this->em->getMetadataFactory()->getCacheDriver()->fetch(
89+
$meta = $this->em->getConfiguration()->getMetadataCache()->getItem(
9090
'Gedmo__Tests__Tree__Fixture__Closure__CategoryClosureWithoutMapping__CLASSMETADATA__'
91-
);
91+
)->get();
9292
static::assertNotFalse($meta);
9393
static::assertTrue($meta->hasAssociation('ancestor'));
9494
static::assertTrue($meta->hasAssociation('descendant'));

tests/Gedmo/Tool/BaseTestCaseMongoODM.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Gedmo\Timestampable\TimestampableListener;
2525
use Gedmo\Translatable\TranslatableListener;
2626
use MongoDB\Client;
27+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2728

2829
/**
2930
* Base test case contains common mock objects
@@ -115,6 +116,7 @@ protected function getMockAnnotatedConfig(): Configuration
115116
$config->setAutoGenerateProxyClasses(Configuration::AUTOGENERATE_EVAL);
116117
$config->setAutoGenerateHydratorClasses(Configuration::AUTOGENERATE_EVAL);
117118
$config->setMetadataDriverImpl($this->getMetadataDriverImplementation());
119+
$config->setMetadataCache(new ArrayAdapter());
118120

119121
return $config;
120122
}
@@ -131,6 +133,7 @@ protected function getDefaultConfiguration(): Configuration
131133
$config->setAutoGenerateProxyClasses(Configuration::AUTOGENERATE_EVAL);
132134
$config->setAutoGenerateHydratorClasses(Configuration::AUTOGENERATE_EVAL);
133135
$config->setMetadataDriverImpl($this->getMetadataDefaultDriverImplementation());
136+
$config->setMetadataCache(new ArrayAdapter());
134137

135138
return $config;
136139
}

tests/Gedmo/Tool/BaseTestCaseOM.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Gedmo\Translatable\TranslatableListener;
3838
use Gedmo\Tree\TreeListener;
3939
use MongoDB\Client;
40+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
4041

4142
/**
4243
* Base test case contains common mock objects
@@ -167,6 +168,7 @@ private function getMockODMMongoDBConfig(string $dbName, MappingDriver $mappingD
167168
$config->setAutoGenerateProxyClasses(Configuration::AUTOGENERATE_EVAL);
168169
$config->setAutoGenerateHydratorClasses(Configuration::AUTOGENERATE_EVAL);
169170
$config->setMetadataDriverImpl($mappingDriver);
171+
$config->setMetadataCache(new ArrayAdapter());
170172

171173
return $config;
172174
}
@@ -187,6 +189,7 @@ private function getMockORMConfig(MappingDriver $mappingDriver = null): \Doctrin
187189
$config->setNamingStrategy(new DefaultNamingStrategy());
188190
$config->setMetadataDriverImpl($mappingDriver ?? $this->getORMDriver());
189191
$config->setRepositoryFactory(new DefaultRepositoryFactoryORM());
192+
$config->setMetadataCache(new ArrayAdapter());
190193

191194
return $config;
192195
}

0 commit comments

Comments
 (0)