Skip to content

Commit fe98b15

Browse files
committed
Use Doctrine MetadataCache if a cache is not set
1 parent 930a9fa commit fe98b15

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"php": "^7.2 || ^8.0",
4242
"behat/transliterator": "~1.2",
4343
"doctrine/annotations": "^1.13",
44+
"doctrine/cache": "^1.11 || ^2.0",
4445
"doctrine/collections": "^1.0",
4546
"doctrine/common": "^2.13 || ^3.0",
4647
"doctrine/event-manager": "^1.0",
@@ -49,8 +50,8 @@
4950
"symfony/cache": "^4.4 || ^5.3 || ^6.0"
5051
},
5152
"require-dev": {
52-
"doctrine/cache": "^1.11 || ^2.0",
5353
"doctrine/dbal": "^2.13.1 || ^3.2",
54+
"doctrine/deprecations": "^0.5.3",
5455
"doctrine/doctrine-bundle": "^2.3",
5556
"doctrine/mongodb-odm": "^2.2",
5657
"doctrine/orm": "^2.10.2",

src/Mapping/MappedEventSubscriber.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ abstract class MappedEventSubscriber implements EventSubscriber
8181
private static $defaultAnnotationReader;
8282

8383
/**
84-
* @var CacheItemPoolInterface
84+
* @var CacheItemPoolInterface|null
8585
*/
8686
private $cacheItemPool;
8787

8888
public function __construct()
8989
{
9090
$parts = explode('\\', $this->getNamespace());
9191
$this->name = end($parts);
92-
$this->cacheItemPool = new ArrayAdapter();
9392
}
9493

9594
/**
@@ -108,7 +107,7 @@ public function getConfiguration(ObjectManager $objectManager, $class)
108107

109108
$config = [];
110109

111-
$cacheItemPool = $this->getCacheItemPool();
110+
$cacheItemPool = $this->getCacheItemPool($objectManager);
112111

113112
$cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
114113
$cacheItem = $cacheItemPool->getItem($cacheId);
@@ -149,7 +148,7 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager)
149148
$objectManager,
150149
$this->getNamespace(),
151150
$this->annotationReader,
152-
$this->getCacheItemPool()
151+
$this->getCacheItemPool($objectManager)
153152
);
154153
}
155154

@@ -281,8 +280,23 @@ private function getDefaultAnnotationReader(): Reader
281280
return self::$defaultAnnotationReader;
282281
}
283282

284-
private function getCacheItemPool(): CacheItemPoolInterface
283+
private function getCacheItemPool(ObjectManager $objectManager): CacheItemPoolInterface
285284
{
285+
if (null !== $this->cacheItemPool) {
286+
return $this->cacheItemPool;
287+
}
288+
289+
$factory = $objectManager->getMetadataFactory();
290+
$cacheDriver = $factory->getCacheDriver();
291+
292+
if (null === $cacheDriver) {
293+
$this->cacheItemPool = new ArrayAdapter();
294+
295+
return $this->cacheItemPool;
296+
}
297+
298+
$this->cacheItemPool = CacheAdapter::wrap($cacheDriver);
299+
286300
return $this->cacheItemPool;
287301
}
288302
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Mapping;
13+
14+
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Doctrine\Common\EventManager;
16+
use Doctrine\Deprecations\Deprecation;
17+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
18+
use Doctrine\ORM\EntityManager;
19+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
20+
use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionPropertyBase;
21+
use Gedmo\Sluggable\SluggableListener;
22+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
23+
24+
final class MappingEventSubscriberTest extends ORMMappingTestCase
25+
{
26+
use VerifyDeprecations;
27+
use ExpectDeprecationTrait;
28+
29+
/**
30+
* @var EntityManager
31+
*/
32+
private $em;
33+
34+
protected function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
$config = $this->getBasicConfiguration();
39+
40+
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
41+
42+
$conn = [
43+
'driver' => 'pdo_sqlite',
44+
'memory' => true,
45+
];
46+
47+
$this->em = EntityManager::create($conn, $config, new EventManager());
48+
}
49+
50+
/**
51+
* @group legacy
52+
*/
53+
public function testGetConfigurationCachedFromDoctrine(): void
54+
{
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();
60+
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+
}
65+
66+
$subscriber = new SluggableListener();
67+
$subscriber->getExtensionMetadataFactory($this->em);
68+
}
69+
70+
protected function getUsedEntityFixtures(): array
71+
{
72+
return [];
73+
}
74+
}

0 commit comments

Comments
 (0)