From c5057f3201362d99607d51b81655bfd68c4c8319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 19 Dec 2023 15:30:09 +0100 Subject: [PATCH] - --- .../DoctrineMongoDBExtension.php | 42 +++++++++---------- Resources/config/mongodb.xml | 14 ------- Resources/doc/config.rst | 4 +- .../AbstractMongoDBExtensionTestCase.php | 32 ++++---------- .../DependencyInjection/ConfigurationTest.php | 2 +- .../Fixtures/config/xml/full.xml | 2 +- .../mongodb_service_multiple_connections.xml | 4 +- ...ngodb_service_simple_single_connection.xml | 2 +- .../Fixtures/config/yml/full.yml | 2 +- .../mongodb_service_multiple_connections.yml | 4 +- ...ngodb_service_simple_single_connection.yml | 2 +- .../yml/mongodb_service_single_connection.yml | 2 +- 12 files changed, 42 insertions(+), 70 deletions(-) diff --git a/DependencyInjection/DoctrineMongoDBExtension.php b/DependencyInjection/DoctrineMongoDBExtension.php index fc4e6be6..bba98479 100644 --- a/DependencyInjection/DoctrineMongoDBExtension.php +++ b/DependencyInjection/DoctrineMongoDBExtension.php @@ -13,8 +13,6 @@ use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface; use Doctrine\Bundle\MongoDBBundle\Fixture\ODMFixtureInterface; use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface; -use Doctrine\Common\Cache\MemcacheCache; -use Doctrine\Common\Cache\RedisCache; use Doctrine\Common\DataFixtures\Loader as DataFixturesLoader; use Doctrine\Common\EventSubscriber; use Doctrine\ODM\MongoDB\DocumentManager; @@ -582,13 +580,10 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName, return $cacheDriverServiceId; case 'memcached': - if (! empty($cacheDriver['class']) && $cacheDriver['class'] !== MemcacheCache::class) { - return parent::loadCacheDriver($cacheName, $objectManagerName, $cacheDriver, $container); - } - - $memcachedInstanceClass = ! empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%' . $this->getObjectManagerElementName('cache.memcached_instance.class') . '%'; - $memcachedHost = ! empty($cacheDriver['host']) ? $cacheDriver['host'] : '%' . $this->getObjectManagerElementName('cache.memcached_host') . '%'; - $memcachedPort = ! empty($cacheDriver['port']) ? $cacheDriver['port'] : '%' . $this->getObjectManagerElementName('cache.memcached_port') . '%'; + $memcachedClass = $cacheDriver['class'] ?? MemcachedAdapter::class; + $memcachedInstanceClass = $cacheDriver['instance_class'] ?? 'Memcache'; + $memcachedHost = $cacheDriver['host'] ?? 'localhost'; + $memcachedPort = $cacheDriver['port'] ?? '11211'; $memcachedInstance = new Definition($memcachedInstanceClass); $memcachedInstance->addMethodCall('addServer', [ $memcachedHost, @@ -596,18 +591,18 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName, ]); $container->setDefinition($this->getObjectManagerElementName(sprintf('%s_memcached_instance', $objectManagerName)), $memcachedInstance); - $cacheDef = new Definition(MemcachedAdapter::class, [new Reference($this->getObjectManagerElementName(sprintf('%s_memcached_instance', $objectManagerName)))]); + $cacheDef = new Definition($memcachedClass, [ + new Reference($this->getObjectManagerElementName(sprintf('%s_memcached_instance', $objectManagerName))), + $cacheDriver['namespace'] ?? '', + ]); break; case 'redis': - if (! empty($cacheDriver['class']) && $cacheDriver['class'] !== RedisCache::class) { - return parent::loadCacheDriver($cacheName, $objectManagerName, $cacheDriver, $container); - } - - $redisInstanceClass = ! empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%' . $this->getObjectManagerElementName('cache.redis_instance.class') . '%'; - $redisHost = ! empty($cacheDriver['host']) ? $cacheDriver['host'] : '%' . $this->getObjectManagerElementName('cache.redis_host') . '%'; - $redisPort = ! empty($cacheDriver['port']) ? $cacheDriver['port'] : '%' . $this->getObjectManagerElementName('cache.redis_port') . '%'; + $redisClass = $cacheDriver['class'] ?? RedisAdapter::class; + $redisInstanceClass = $cacheDriver['instance_class'] ?? 'Redis'; + $redisHost = $cacheDriver['host'] ?? 'localhost'; + $redisPort = $cacheDriver['port'] ?? '6379'; $redisInstance = new Definition($redisInstanceClass); $redisInstance->addMethodCall('connect', [ $redisHost, @@ -615,22 +610,27 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName, ]); $container->setDefinition($this->getObjectManagerElementName(sprintf('%s_redis_instance', $objectManagerName)), $redisInstance); - $cacheDef = new Definition(RedisAdapter::class, [new Reference($this->getObjectManagerElementName(sprintf('%s_redis_instance', $objectManagerName)))]); + $cacheDef = new Definition($redisClass, [ + new Reference($this->getObjectManagerElementName(sprintf('%s_redis_instance', $objectManagerName))), + $cacheDriver['namespace'] ?? '', + ]); break; case 'apcu': - $cacheDef = new Definition(ApcuAdapter::class); + $cacheDef = new Definition($cacheDriver['class'] ?? ApcuAdapter::class, [ + $cacheDriver['namespace'] ?? '', + ]); break; case 'array': - $cacheDef = new Definition(ArrayAdapter::class); + $cacheDef = new Definition($cacheDriver['class'] ?? ArrayAdapter::class); break; default: - return parent::loadCacheDriver($cacheName, $objectManagerName, $cacheDriver, $container); + throw new InvalidArgumentException(sprintf('"%s" is an unrecognized cache driver.', $cacheDriver['type'])); } $cacheDef->setPublic(false); diff --git a/Resources/config/mongodb.xml b/Resources/config/mongodb.xml index 0cf838b6..304037ce 100644 --- a/Resources/config/mongodb.xml +++ b/Resources/config/mongodb.xml @@ -19,16 +19,6 @@ Doctrine\Bundle\MongoDBBundle\CacheWarmer\HydratorCacheWarmer Doctrine\Bundle\MongoDBBundle\CacheWarmer\PersistentCollectionCacheWarmer - - Doctrine\Common\Cache\ArrayCache - Doctrine\Common\Cache\ApcCache - Doctrine\Common\Cache\ApcuCache - Doctrine\Common\Cache\MemcacheCache - localhost - 11211 - Memcache - Doctrine\Common\Cache\XcacheCache - Doctrine\Persistence\Mapping\Driver\MappingDriverChain Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver @@ -49,7 +39,6 @@ - @@ -113,9 +102,6 @@ - - - diff --git a/Resources/doc/config.rst b/Resources/doc/config.rst index 5435a0a7..cfc3aa01 100644 --- a/Resources/doc/config.rst +++ b/Resources/doc/config.rst @@ -23,7 +23,7 @@ Sample Configuration filter-name: class: Class\Example\Filter\ODM\ExampleFilter enabled: true - metadata_cache_driver: array # array, apc, apcu, memcache, memcached, redis, wincache, zenddata, xcache + metadata_cache_driver: array # array, service, apcu, memcached, redis .. code-block:: xml @@ -86,7 +86,7 @@ If you wish to use memcache to cache your metadata, you need to configure the AcmeDemoBundle: ~ metadata_cache_driver: type: memcache - class: Doctrine\Common\Cache\MemcacheCache + class: Symfony\Component\Cache\Adapter\MemcachedAdapter host: localhost port: 11211 instance_class: Memcache diff --git a/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php b/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php index 1376052c..3a109702 100644 --- a/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php +++ b/Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php @@ -5,24 +5,20 @@ namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection; use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension; -use Doctrine\Bundle\MongoDBBundle\Mapping\Driver\XmlDriver; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Filter\BasicFilter; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Filter\ComplexFilter; use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Filter\DisabledFilter; use Doctrine\Bundle\MongoDBBundle\Tests\TestCase; -use Doctrine\Common\Cache\ApcCache; -use Doctrine\Common\Cache\ArrayCache; -use Doctrine\Common\Cache\MemcacheCache; -use Doctrine\Common\Cache\MemcachedCache; -use Doctrine\Common\Cache\XcacheCache; use Doctrine\Common\EventSubscriber; use Doctrine\ODM\MongoDB\Configuration; use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver; -use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use MongoDB\Client; use PHPUnit\Framework\AssertionFailedError; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; +use Symfony\Component\Cache\Adapter\ApcuAdapter; +use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Symfony\Component\Cache\Adapter\MemcachedAdapter; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -55,16 +51,6 @@ public function testDependencyInjectionConfigurationDefaults(): void $this->assertEquals(DocumentManager::class, $container->getParameter('doctrine_mongodb.odm.document_manager.class')); $this->assertEquals('MongoDBODMProxies', $container->getParameter('doctrine_mongodb.odm.proxy_namespace')); $this->assertEquals(Configuration::AUTOGENERATE_EVAL, $container->getParameter('doctrine_mongodb.odm.auto_generate_proxy_classes')); - $this->assertEquals(ArrayCache::class, $container->getParameter('doctrine_mongodb.odm.cache.array.class')); - $this->assertEquals(ApcCache::class, $container->getParameter('doctrine_mongodb.odm.cache.apc.class')); - $this->assertEquals(MemcacheCache::class, $container->getParameter('doctrine_mongodb.odm.cache.memcache.class')); - $this->assertEquals('localhost', $container->getParameter('doctrine_mongodb.odm.cache.memcache_host')); - $this->assertEquals('11211', $container->getParameter('doctrine_mongodb.odm.cache.memcache_port')); - $this->assertEquals('Memcache', $container->getParameter('doctrine_mongodb.odm.cache.memcache_instance.class')); - $this->assertEquals(XcacheCache::class, $container->getParameter('doctrine_mongodb.odm.cache.xcache.class')); - $this->assertEquals(MappingDriverChain::class, $container->getParameter('doctrine_mongodb.odm.metadata.driver_chain.class')); - $this->assertEquals(AttributeDriver::class, $container->getParameter('doctrine_mongodb.odm.metadata.attribute.class')); - $this->assertEquals(XmlDriver::class, $container->getParameter('doctrine_mongodb.odm.metadata.xml.class')); $this->assertEquals(UniqueEntityValidator::class, $container->getParameter('doctrine_odm.mongodb.validator.unique.class')); @@ -349,10 +335,10 @@ public function testDocumentManagerMetadataCacheDriverConfiguration(): void $container->compile(); $definition = $container->getDefinition('doctrine_mongodb.odm.dm1_metadata_cache'); - $this->assertEquals('%doctrine_mongodb.odm.cache.xcache.class%', $definition->getClass()); + $this->assertEquals(ArrayAdapter::class, $definition->getClass()); $definition = $container->getDefinition('doctrine_mongodb.odm.dm2_metadata_cache'); - $this->assertEquals('%doctrine_mongodb.odm.cache.apc.class%', $definition->getClass()); + $this->assertEquals(ApcuAdapter::class, $definition->getClass()); } /** @psalm-suppress UndefinedClass this won't be necessary when removing metadata cache configuration */ @@ -369,11 +355,11 @@ public function testDocumentManagerMemcachedMetadataCacheDriverConfiguration(): $container->compile(); $definition = $container->getDefinition('doctrine_mongodb.odm.default_metadata_cache'); - $this->assertEquals(MemcachedCache::class, $definition->getClass()); + $this->assertEquals(MemcachedAdapter::class, $definition->getClass()); - $calls = $definition->getMethodCalls(); - $this->assertEquals('setMemcached', $calls[0][0]); - $this->assertEquals('doctrine_mongodb.odm.default_memcached_instance', (string) $calls[0][1][0]); + $args = $definition->getArguments(); + $this->assertEquals('doctrine_mongodb.odm.default_memcached_instance', (string) $args[0]); + $this->assertEquals('', $args[1]); $definition = $container->getDefinition('doctrine_mongodb.odm.default_memcached_instance'); $this->assertEquals('Memcached', $definition->getClass()); diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index 3c041d74..8036bfb7 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -183,7 +183,7 @@ public function testFullConfiguration(array $config): void 'persistent_collection_factory' => null, 'auto_mapping' => false, 'filters' => [], - 'metadata_cache_driver' => ['type' => 'apc'], + 'metadata_cache_driver' => ['type' => 'apcu'], 'mappings' => [ 'BarBundle' => [ 'type' => 'xml', diff --git a/Tests/DependencyInjection/Fixtures/config/xml/full.xml b/Tests/DependencyInjection/Fixtures/config/xml/full.xml index 939844be..893d4227 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/full.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/full.xml @@ -100,7 +100,7 @@ id="dm2" connection="dm2_connection" database="db1" - metadata-cache-driver="apc" + metadata-cache-driver="apcu" logging="true" repository-factory="doctrine_mongodb.odm.container_repository_factory" default-document-repository-class="Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Repository\CustomRepository" diff --git a/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_multiple_connections.xml b/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_multiple_connections.xml index 4281734f..13b7b382 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_multiple_connections.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_multiple_connections.xml @@ -16,7 +16,7 @@ - - + + diff --git a/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_simple_single_connection.xml b/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_simple_single_connection.xml index bde73256..ce0ad039 100644 --- a/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_simple_single_connection.xml +++ b/Tests/DependencyInjection/Fixtures/config/xml/mongodb_service_simple_single_connection.xml @@ -11,7 +11,7 @@ - Doctrine\Common\Cache\MemcachedCache + Symfony\Component\Cache\Adapter\MemcachedAdapter localhost 11211 Memcached diff --git a/Tests/DependencyInjection/Fixtures/config/yml/full.yml b/Tests/DependencyInjection/Fixtures/config/yml/full.yml index 89538af7..d75ddc8b 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/full.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/full.yml @@ -105,5 +105,5 @@ doctrine_mongodb: prefix: prefix_val alias: alias_val is_bundle: false - metadata_cache_driver: apc + metadata_cache_driver: apcu logging: true diff --git a/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_multiple_connections.yml b/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_multiple_connections.yml index e4328140..16800e35 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_multiple_connections.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_multiple_connections.yml @@ -9,7 +9,7 @@ doctrine_mongodb: document_managers: dm1: connection: conn1 - metadata_cache_driver: xcache + metadata_cache_driver: array dm2: connection: conn2 - metadata_cache_driver: apc + metadata_cache_driver: apcu diff --git a/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_simple_single_connection.yml b/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_simple_single_connection.yml index b7b20dcf..d3bb827d 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_simple_single_connection.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_simple_single_connection.yml @@ -7,7 +7,7 @@ doctrine_mongodb: default: metadata_cache_driver: type: memcached - class: Doctrine\Common\Cache\MemcachedCache + class: Symfony\Component\Cache\Adapter\MemcachedAdapter host: localhost port: 11211 instance_class: Memcached diff --git a/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_single_connection.yml b/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_single_connection.yml index fb3ed665..f9a98d64 100644 --- a/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_single_connection.yml +++ b/Tests/DependencyInjection/Fixtures/config/yml/mongodb_service_single_connection.yml @@ -7,7 +7,7 @@ doctrine_mongodb: connection: default metadata_cache_driver: type: memcached - class: Doctrine\Common\Cache\MemcachedCache + class: Symfony\Component\Cache\Adapter\MemcachedAdapter host: localhost port: 11211 instance_class: Memcached