diff --git a/lib/Doctrine/ORM/Tools/Setup.php b/lib/Doctrine/ORM/Tools/Setup.php index 666d627a4..2e8a8f915 100644 --- a/lib/Doctrine/ORM/Tools/Setup.php +++ b/lib/Doctrine/ORM/Tools/Setup.php @@ -123,31 +123,7 @@ class Setup { $proxyDir = $proxyDir ?: sys_get_temp_dir(); - if ($isDevMode === false && $cache === null) { - if (extension_loaded('apc')) { - $cache = new \Doctrine\Common\Cache\ApcCache(); - } elseif (ini_get('xcache.cacher')) { - $cache = new \Doctrine\Common\Cache\XcacheCache(); - } elseif (extension_loaded('memcache')) { - $memcache = new \Memcache(); - $memcache->connect('127.0.0.1'); - $cache = new \Doctrine\Common\Cache\MemcacheCache(); - $cache->setMemcache($memcache); - } elseif (extension_loaded('redis')) { - $redis = new \Redis(); - $redis->connect('127.0.0.1'); - $cache = new \Doctrine\Common\Cache\RedisCache(); - $cache->setRedis($redis); - } else { - $cache = new ArrayCache(); - } - } elseif ($cache === null) { - $cache = new ArrayCache(); - } - - if ($cache instanceof CacheProvider) { - $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions - } + $cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache); $config = new Configuration(); $config->setMetadataCacheImpl($cache); @@ -159,4 +135,64 @@ class Setup return $config; } + + private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, ?Cache $cache) : Cache + { + $cache = self::createCacheInstance($isDevMode, $cache); + + if ( ! $cache instanceof CacheProvider) { + return $cache; + } + + $namespace = $cache->getNamespace(); + + if ($namespace !== '') { + $namespace .= ':'; + } + + $cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions + + return $cache; + } + + private static function createCacheInstance(bool $isDevMode, ?Cache $cache) : Cache + { + if ($cache !== null) { + return $cache; + } + + if ($isDevMode === true) { + return new ArrayCache(); + } + + if (extension_loaded('apc')) { + return new \Doctrine\Common\Cache\ApcCache(); + } + + if (ini_get('xcache.cacher')) { + return new \Doctrine\Common\Cache\XcacheCache(); + } + + if (extension_loaded('memcache')) { + $memcache = new \Memcache(); + $memcache->connect('127.0.0.1'); + + $cache = new \Doctrine\Common\Cache\MemcacheCache(); + $cache->setMemcache($memcache); + + return $cache; + } + + if (extension_loaded('redis')) { + $redis = new \Redis(); + $redis->connect('127.0.0.1'); + + $cache = new \Doctrine\Common\Cache\RedisCache(); + $cache->setRedis($redis); + + return $cache; + } + + return new ArrayCache(); + } } diff --git a/tests/Doctrine/Tests/ORM/Tools/SetupTest.php b/tests/Doctrine/Tests/ORM/Tools/SetupTest.php index fc5bd6e71..c7ec7c6d1 100644 --- a/tests/Doctrine/Tests/ORM/Tools/SetupTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/SetupTest.php @@ -76,6 +76,43 @@ class SetupTest extends OrmTestCase $this->assertInstanceOf(YamlDriver::class, $config->getMetadataDriverImpl()); } + /** + * @group 5904 + */ + public function testCacheNamespaceShouldBeGeneratedWhenCacheIsNotGiven() : void + { + $config = Setup::createConfiguration(false, '/foo'); + $cache = $config->getMetadataCacheImpl(); + + self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf_', $cache->getNamespace()); + } + + /** + * @group 5904 + */ + public function testCacheNamespaceShouldBeGeneratedWhenCacheIsGivenButHasNoNamespace() : void + { + $config = Setup::createConfiguration(false, '/foo', new ArrayCache()); + $cache = $config->getMetadataCacheImpl(); + + self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf_', $cache->getNamespace()); + } + + /** + * @group 5904 + */ + public function testConfiguredCacheNamespaceShouldBeUsedAsPrefixOfGeneratedNamespace() : void + { + $originalCache = new ArrayCache(); + $originalCache->setNamespace('foo'); + + $config = Setup::createConfiguration(false, '/foo', $originalCache); + $cache = $config->getMetadataCacheImpl(); + + self::assertSame($originalCache, $cache); + self::assertSame('foo:dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf_', $cache->getNamespace()); + } + /** * @group DDC-1350 */