Skip to content

Commit

Permalink
Drop support for Doctrine cache adapter for metadata (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored Dec 19, 2023
1 parent 0e5e5de commit 232f6c3
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 239 deletions.
100 changes: 0 additions & 100 deletions DependencyInjection/Compiler/CacheCompatibilityPass.php

This file was deleted.

30 changes: 11 additions & 19 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\Configuration as ODMConfiguration;
Expand Down Expand Up @@ -581,40 +579,34 @@ 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'] ?? 'Memcached';
$memcachedHost = $cacheDriver['host'] ?? 'localhost';
$memcachedPort = $cacheDriver['port'] ?? '11211';
$memcachedInstance = new Definition($memcachedInstanceClass);
$memcachedInstance->addMethodCall('addServer', [
$memcachedHost,
$memcachedPort,
]);
$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)))]);

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,
$redisPort,
]);
$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)))]);

break;

Expand All @@ -629,7 +621,7 @@ protected function loadCacheDriver(string $cacheName, string $objectManagerName,
break;

default:
return parent::loadCacheDriver($cacheName, $objectManagerName, $cacheDriver, $container);
throw new InvalidArgumentException(sprintf('"%s" is an unrecognized cache driver.', $cacheDriver['type']));
}

$cacheDef->setPublic(false);
Expand Down
2 changes: 0 additions & 2 deletions DoctrineMongoDBBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Doctrine\Bundle\MongoDBBundle;

use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
Expand Down Expand Up @@ -35,7 +34,6 @@ class DoctrineMongoDBBundle extends Bundle

public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new CacheCompatibilityPass());
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine_mongodb.odm.connections', 'doctrine_mongodb.odm.%s_connection.event_manager', 'doctrine_mongodb.odm'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
Expand Down
20 changes: 10 additions & 10 deletions Resources/doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,7 +67,7 @@ Sample Configuration
server: '%env(resolve:MONGODB_URL)%'
If you wish to use memcache to cache your metadata, you need to configure the
``Memcache`` instance; for example, you can do the following:
``Memcached`` instance; for example, you can do the following:

.. configuration-block::

Expand All @@ -85,11 +85,11 @@ If you wish to use memcache to cache your metadata, you need to configure the
mappings:
AcmeDemoBundle: ~
metadata_cache_driver:
type: memcache
class: Doctrine\Common\Cache\MemcacheCache
type: memcached
class: Symfony\Component\Cache\Adapter\MemcachedAdapter
host: localhost
port: 11211
instance_class: Memcache
instance_class: Memcached
.. code-block:: xml
Expand All @@ -104,11 +104,11 @@ If you wish to use memcache to cache your metadata, you need to configure the
<doctrine_mongodb:config default-database="hello_%kernel.environment%">
<doctrine_mongodb:document-manager id="default">
<doctrine_mongodb:mapping name="AcmeDemoBundle" />
<doctrine_mongodb:metadata-cache-driver type="memcache">
<doctrine_mongodb:class>Doctrine\Common\Cache\MemcacheCache</doctrine_mongodb:class>
<doctrine_mongodb:metadata-cache-driver type="memcached">
<doctrine_mongodb:class>Symfony\Component\Cache\Adapter\MemcachedAdapter</doctrine_mongodb:class>
<doctrine_mongodb:host>localhost</doctrine_mongodb:host>
<doctrine_mongodb:port>11211</doctrine_mongodb:port>
<doctrine_mongodb:instance-class>Memcache</doctrine_mongodb:instance-class>
<doctrine_mongodb:instance-class>Memcached</doctrine_mongodb:instance-class>
</doctrine_mongodb:metadata-cache-driver>
</doctrine_mongodb:document-manager>
<doctrine_mongodb:connection id="default" server="mongodb://localhost:27017">
Expand Down Expand Up @@ -330,7 +330,7 @@ following syntax:
dm1:
connection: conn1
database: db1
metadata_cache_driver: xcache
metadata_cache_driver: array
mappings:
AcmeDemoBundle: ~
dm2:
Expand Down Expand Up @@ -363,7 +363,7 @@ following syntax:
<doctrine_mongodb:options>
</doctrine_mongodb:options>
</doctrine_mongodb:connection>
<doctrine_mongodb:document-manager id="dm1" metadata-cache-driver="xcache" connection="conn1" database="db1">
<doctrine_mongodb:document-manager id="dm1" metadata-cache-driver="array" connection="conn1" database="db1">
<doctrine_mongodb:mapping name="AcmeDemoBundle" />
</doctrine_mongodb:document-manager>
<doctrine_mongodb:document-manager id="dm2" connection="conn2" database="db2">
Expand Down
31 changes: 8 additions & 23 deletions Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
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\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;
Expand Down Expand Up @@ -51,16 +47,6 @@ public function testDependencyInjectionConfigurationDefaults(): void

$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'));

$config = DoctrineMongoDBExtensionTest::buildConfiguration([
'proxy_namespace' => 'MyProxies',
Expand Down Expand Up @@ -343,10 +329,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 */
Expand All @@ -363,11 +349,10 @@ 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]);

$definition = $container->getDefinition('doctrine_mongodb.odm.default_memcached_instance');
$this->assertEquals('Memcached', $definition->getClass());
Expand Down
Loading

0 comments on commit 232f6c3

Please sign in to comment.