Skip to content

Commit

Permalink
Use Doctrine MetadataCache if a cache is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Jan 10, 2022
1 parent 930a9fa commit fe98b15
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"php": "^7.2 || ^8.0",
"behat/transliterator": "~1.2",
"doctrine/annotations": "^1.13",
"doctrine/cache": "^1.11 || ^2.0",
"doctrine/collections": "^1.0",
"doctrine/common": "^2.13 || ^3.0",
"doctrine/event-manager": "^1.0",
Expand All @@ -49,8 +50,8 @@
"symfony/cache": "^4.4 || ^5.3 || ^6.0"
},
"require-dev": {
"doctrine/cache": "^1.11 || ^2.0",
"doctrine/dbal": "^2.13.1 || ^3.2",
"doctrine/deprecations": "^0.5.3",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/mongodb-odm": "^2.2",
"doctrine/orm": "^2.10.2",
Expand Down
24 changes: 19 additions & 5 deletions src/Mapping/MappedEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ abstract class MappedEventSubscriber implements EventSubscriber
private static $defaultAnnotationReader;

/**
* @var CacheItemPoolInterface
* @var CacheItemPoolInterface|null
*/
private $cacheItemPool;

public function __construct()
{
$parts = explode('\\', $this->getNamespace());
$this->name = end($parts);
$this->cacheItemPool = new ArrayAdapter();
}

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

$config = [];

$cacheItemPool = $this->getCacheItemPool();
$cacheItemPool = $this->getCacheItemPool($objectManager);

$cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
$cacheItem = $cacheItemPool->getItem($cacheId);
Expand Down Expand Up @@ -149,7 +148,7 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager)
$objectManager,
$this->getNamespace(),
$this->annotationReader,
$this->getCacheItemPool()
$this->getCacheItemPool($objectManager)
);
}

Expand Down Expand Up @@ -281,8 +280,23 @@ private function getDefaultAnnotationReader(): Reader
return self::$defaultAnnotationReader;
}

private function getCacheItemPool(): CacheItemPoolInterface
private function getCacheItemPool(ObjectManager $objectManager): CacheItemPoolInterface
{
if (null !== $this->cacheItemPool) {
return $this->cacheItemPool;
}

$factory = $objectManager->getMetadataFactory();
$cacheDriver = $factory->getCacheDriver();

if (null === $cacheDriver) {
$this->cacheItemPool = new ArrayAdapter();

return $this->cacheItemPool;
}

$this->cacheItemPool = CacheAdapter::wrap($cacheDriver);

return $this->cacheItemPool;
}
}
74 changes: 74 additions & 0 deletions tests/Gedmo/Mapping/MappingEventSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gedmo\Tests\Mapping;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\EventManager;
use Doctrine\Deprecations\Deprecation;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Reflection\TypedNoDefaultReflectionPropertyBase;
use Gedmo\Sluggable\SluggableListener;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

final class MappingEventSubscriberTest extends ORMMappingTestCase
{
use VerifyDeprecations;
use ExpectDeprecationTrait;

/**
* @var EntityManager
*/
private $em;

protected function setUp(): void
{
parent::setUp();

$config = $this->getBasicConfiguration();

$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));

$conn = [
'driver' => 'pdo_sqlite',
'memory' => true,
];

$this->em = EntityManager::create($conn, $config, new EventManager());
}

/**
* @group legacy
*/
public function testGetConfigurationCachedFromDoctrine(): void
{
// doctrine/persistence changed from trigger_error to doctrine/deprecations in 2.2.1. In 2.2.2 this trait was
// added, this is used to know if the doctrine/persistence version is using trigger_error or
// doctrine/deprecations. This "if" check can be removed once we drop support for doctrine/persistence < 2.2.1
if (trait_exists(TypedNoDefaultReflectionPropertyBase::class)) {
Deprecation::enableWithTriggerError();

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/persistence/issues/184');
} else {
$this->expectDeprecation('Doctrine\Persistence\Mapping\AbstractClassMetadataFactory::getCacheDriver is deprecated. Use getCache() instead.');
}

$subscriber = new SluggableListener();
$subscriber->getExtensionMetadataFactory($this->em);
}

protected function getUsedEntityFixtures(): array
{
return [];
}
}

0 comments on commit fe98b15

Please sign in to comment.