Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 19, 2023
1 parent fa5f7df commit c5057f3
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 70 deletions.
42 changes: 21 additions & 21 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\DocumentManager;
Expand Down Expand Up @@ -582,55 +580,57 @@ 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,
$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))),
$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';

Check warning on line 605 in DependencyInjection/DoctrineMongoDBExtension.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/DoctrineMongoDBExtension.php#L602-L605

Added lines #L602 - L605 were not covered by tests
$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))),
$cacheDriver['namespace'] ?? '',
]);

Check warning on line 616 in DependencyInjection/DoctrineMongoDBExtension.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/DoctrineMongoDBExtension.php#L613-L616

Added lines #L613 - L616 were not covered by tests

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

Check warning on line 633 in DependencyInjection/DoctrineMongoDBExtension.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/DoctrineMongoDBExtension.php#L633

Added line #L633 was not covered by tests
}

$cacheDef->setPublic(false);
Expand Down
14 changes: 0 additions & 14 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@
<parameter key="doctrine_mongodb.odm.hydrator_cache_warmer.class">Doctrine\Bundle\MongoDBBundle\CacheWarmer\HydratorCacheWarmer</parameter>
<parameter key="doctrine_mongodb.odm.persistent_collection_cache_warmer.class">Doctrine\Bundle\MongoDBBundle\CacheWarmer\PersistentCollectionCacheWarmer</parameter>

<!-- cache -->
<parameter key="doctrine_mongodb.odm.cache.array.class">Doctrine\Common\Cache\ArrayCache</parameter>
<parameter key="doctrine_mongodb.odm.cache.apc.class">Doctrine\Common\Cache\ApcCache</parameter>
<parameter key="doctrine_mongodb.odm.cache.apcu.class">Doctrine\Common\Cache\ApcuCache</parameter>
<parameter key="doctrine_mongodb.odm.cache.memcache.class">Doctrine\Common\Cache\MemcacheCache</parameter>
<parameter key="doctrine_mongodb.odm.cache.memcache_host">localhost</parameter>
<parameter key="doctrine_mongodb.odm.cache.memcache_port">11211</parameter>
<parameter key="doctrine_mongodb.odm.cache.memcache_instance.class">Memcache</parameter>
<parameter key="doctrine_mongodb.odm.cache.xcache.class">Doctrine\Common\Cache\XcacheCache</parameter>

<!-- metadata -->
<parameter key="doctrine_mongodb.odm.metadata.driver_chain.class">Doctrine\Persistence\Mapping\Driver\MappingDriverChain</parameter>
<parameter key="doctrine_mongodb.odm.metadata.attribute.class">Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver</parameter>
Expand All @@ -49,7 +39,6 @@

<services>
<!-- defaults -->
<service id="doctrine_mongodb.odm.cache" alias="doctrine_mongodb.odm.cache.array" />
<service id="%doctrine_mongodb.odm.document_manager.class%" alias="doctrine_mongodb.odm.document_manager" public="false" />
<service id="%doctrine_mongodb.odm.class%" alias="doctrine_mongodb" public="false" />

Expand Down Expand Up @@ -113,9 +102,6 @@
<argument type="collection" />
</service>

<!-- cache -->
<service id="doctrine_mongodb.odm.cache.array" class="%doctrine_mongodb.odm.cache.array.class%" />

<!-- logger -->
<service id="doctrine_mongodb.odm.command_logger_registry" class="Doctrine\Bundle\MongoDBBundle\APM\CommandLoggerRegistry" public="true">
<argument type="tagged" tag="doctrine_mongodb.odm.command_logger" />
Expand Down
4 changes: 2 additions & 2 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 @@ -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
Expand Down
32 changes: 9 additions & 23 deletions Tests/DependencyInjection/AbstractMongoDBExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));

Expand Down Expand Up @@ -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 */
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/Fixtures/config/xml/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<doctrine:connection id="conn2" server="mongodb://localhost:27017" />

<doctrine:document-manager id="dm1" metadata-cache-driver="xcache" connection="conn1" />
<doctrine:document-manager id="dm2" connection="conn2" metadata-cache-driver="apc" />
<doctrine:document-manager id="dm1" metadata-cache-driver="array" connection="conn1" />
<doctrine:document-manager id="dm2" connection="conn2" metadata-cache-driver="apcu" />
</doctrine:mongodb>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<doctrine:document-manager id="default" connection="default">
<doctrine:metadata-cache-driver type="memcached">
<doctrine:class>Doctrine\Common\Cache\MemcachedCache</doctrine:class>
<doctrine:class>Symfony\Component\Cache\Adapter\MemcachedAdapter</doctrine:class>
<doctrine:host>localhost</doctrine:host>
<doctrine:port>11211</doctrine:port>
<doctrine:instance-class>Memcached</doctrine:instance-class>
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/Fixtures/config/yml/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c5057f3

Please sign in to comment.