From 04b52149ab79b862327e434e0fdfd024025e923b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 02:50:08 +0200 Subject: [PATCH 01/16] DDC-3078 - constructor should never be interfaced --- lib/Doctrine/ORM/Cache.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/Doctrine/ORM/Cache.php b/lib/Doctrine/ORM/Cache.php index fec65f1f3..3da7c05ef 100644 --- a/lib/Doctrine/ORM/Cache.php +++ b/lib/Doctrine/ORM/Cache.php @@ -54,13 +54,6 @@ interface Cache */ const MODE_REFRESH = 4; - /** - * Construct - * - * @param \Doctrine\ORM\EntityManagerInterface $em - */ - public function __construct(EntityManagerInterface $em); - /** * @param string $className The entity class. * From 1b5eb55ed950638cf0c070ea520fe6da4582b86e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:00:35 +0200 Subject: [PATCH 02/16] DDC-3078 - adding exception methods for invalid cache instantiator --- lib/Doctrine/ORM/ORMException.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 40c0cb3be..bf200b7d5 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -262,6 +262,19 @@ class ORMException extends Exception return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository."); } + /** + * @param mixed $instantiator + * + * @return ORMException + */ + public static function invalidSecondLevelCacheInstantiator($instantiator) + { + return new self(sprintf( + 'The provided instantiator is not a valid callable, "%s" given.', + is_object($instantiator) ? get_class($instantiator) : gettype($instantiator) + )); + } + /** * @param string $className * From e5f79d1f7367f61c489419fa69276c392fa0c289 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:01:28 +0200 Subject: [PATCH 03/16] DDC-3078 - adding API for cache instantiation to the configuration object --- lib/Doctrine/ORM/Cache/CacheConfiguration.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/Doctrine/ORM/Cache/CacheConfiguration.php b/lib/Doctrine/ORM/Cache/CacheConfiguration.php index 4a583c14c..2e9d73f8f 100644 --- a/lib/Doctrine/ORM/Cache/CacheConfiguration.php +++ b/lib/Doctrine/ORM/Cache/CacheConfiguration.php @@ -20,6 +20,7 @@ namespace Doctrine\ORM\Cache; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMException; use Doctrine\ORM\Cache\Logging\CacheLogger; @@ -51,6 +52,11 @@ class CacheConfiguration */ private $queryValidator; + /** + * @var callable|null + */ + private $cacheInstantiator; + /** * @var string */ @@ -130,6 +136,35 @@ class CacheConfiguration $this->queryValidator = $validator; } + /** + * @param callable $instantiator responsible of retrieving an {@see \Doctrine\ORM\Cache} instance given + * a {@see \Doctrine\ORM\EntityManagerInterface} instance + * + * @throws ORMException if the given instantiator is not a valid callable + */ + public function setCacheInstantiator($instantiator) + { + if ( ! is_callable($instantiator)) { + throw ORMException::invalidSecondLevelCacheInstantiator($instantiator); + } + + $this->cacheInstantiator = $instantiator; + } + + /** + * @return callable that + */ + public function getCacheInstantiator() + { + if ( ! $this->cacheInstantiator) { + $this->cacheInstantiator = function (EntityManagerInterface $entityManager) { + return new DefaultCache($entityManager); + }; + } + + return $this->cacheInstantiator; + } + /** * @param string $className * From 68f489ecbb19402843ea31ecc55021c1c7c3f786 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:10:51 +0200 Subject: [PATCH 04/16] DDC-3078 - cache configuration tests for the newly introduced API for cache instantiators --- .../Tests/ORM/Cache/CacheConfigTest.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php index fff090330..2c599b5bb 100644 --- a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php @@ -7,6 +7,8 @@ use Doctrine\ORM\Cache\CacheConfiguration; /** * @group DDC-2183 + * + * @covers \Doctrine\ORM\Cache\CacheConfiguration */ class CacheConfigTest extends DoctrineTestCase { @@ -15,6 +17,9 @@ class CacheConfigTest extends DoctrineTestCase */ private $config; + /** + * {@inheritDoc} + */ protected function setUp() { parent::setUp(); @@ -34,6 +39,40 @@ class CacheConfigTest extends DoctrineTestCase $this->config->setCacheClassName(__CLASS__); } + /** + * @covers \Doctrine\ORM\Cache\CacheConfiguration::getCacheInstantiator + */ + public function testGetDefaultCacheIstantiator() + { + $entityManager = $this->getMock('Doctrine\ORM\EntityManagerInterface'); + $config = $this->getMock('Doctrine\ORM\Configuration'); + + $entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($config)); + $config + ->expects($this->any()) + ->method('getSecondLevelCacheConfiguration') + ->will($this->returnValue($this->config)); + + $defaultIstantiator = $this->config->getCacheInstantiator(); + + $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCache', $defaultIstantiator($entityManager)); + } + + /** + * @covers \Doctrine\ORM\Cache\CacheConfiguration::getCacheInstantiator + */ + public function testSetGetCacheIstantiator() + { + $istantiator = function () {}; + + $this->config->setCacheInstantiator($istantiator); + $this->assertSame($istantiator, $this->config->getCacheInstantiator()); + + $this->setExpectedException('Doctrine\ORM\ORMException'); + + $this->config->setCacheInstantiator(null); + } + public function testSetGetRegionLifetime() { $config = $this->config->getRegionsConfiguration(); From cd0f94dd6c7fce4735b3059d965d1d5904a910c6 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:13:25 +0200 Subject: [PATCH 05/16] DDC-3078 - removing tests for cache class setter/getter on cache configuration --- tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php index 2c599b5bb..a12f94ec4 100644 --- a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php @@ -27,18 +27,6 @@ class CacheConfigTest extends DoctrineTestCase $this->config = new CacheConfiguration(); } - public function testSetGetCacheClassName() - { - $mockClass = get_class($this->getMock('Doctrine\ORM\Cache')); - - $this->assertEquals('Doctrine\ORM\Cache\DefaultCache', $this->config->getCacheClassName()); - $this->config->setCacheClassName($mockClass); - $this->assertEquals($mockClass, $this->config->getCacheClassName()); - - $this->setExpectedException('Doctrine\ORM\ORMException'); - $this->config->setCacheClassName(__CLASS__); - } - /** * @covers \Doctrine\ORM\Cache\CacheConfiguration::getCacheInstantiator */ From 87a907f9dd420712c7ac0d88a502652fd937a154 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:14:39 +0200 Subject: [PATCH 06/16] DDC-3078 - switching cache initialization to use cache instantiator from config --- lib/Doctrine/ORM/EntityManager.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0c1b16b5c..175e3108e 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -166,8 +166,8 @@ use Doctrine\Common\Util\ClassUtils; ); if ($config->isSecondLevelCacheEnabled()) { - $cacheClass = $config->getSecondLevelCacheConfiguration()->getCacheClassName(); - $this->cache = new $cacheClass($this); + $cacheInstantiator = $config->getSecondLevelCacheConfiguration()->getCacheInstantiator(); + $this->cache = $cacheInstantiator($this); } } From 9b2ee886833cb2dc6149858741a4457527d5ca14 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:15:15 +0200 Subject: [PATCH 07/16] DDC-3078 - removing cache class name setter/getter from cache configuration API --- lib/Doctrine/ORM/Cache/CacheConfiguration.php | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/CacheConfiguration.php b/lib/Doctrine/ORM/Cache/CacheConfiguration.php index 2e9d73f8f..512caa98e 100644 --- a/lib/Doctrine/ORM/Cache/CacheConfiguration.php +++ b/lib/Doctrine/ORM/Cache/CacheConfiguration.php @@ -57,11 +57,6 @@ class CacheConfiguration */ private $cacheInstantiator; - /** - * @var string - */ - private $cacheClassName = 'Doctrine\ORM\Cache\DefaultCache'; - /** * @return \Doctrine\ORM\Cache\CacheFactory|null */ @@ -164,28 +159,4 @@ class CacheConfiguration return $this->cacheInstantiator; } - - /** - * @param string $className - * - * @throws \Doctrine\ORM\ORMException If is not a \Doctrine\ORM\Cache - */ - public function setCacheClassName($className) - { - $reflectionClass = new \ReflectionClass($className); - - if ( ! $reflectionClass->implementsInterface('Doctrine\ORM\Cache')) { - throw ORMException::invalidSecondLevelCache($className); - } - - $this->cacheClassName = $className; - } - - /** - * @return string A \Doctrine\ORM\Cache class name - */ - public function getCacheClassName() - { - return $this->cacheClassName; - } } From d7f87cdd36942ac99763b967f61f4c3f5d0db939 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 03:16:37 +0200 Subject: [PATCH 08/16] DDC-3078 - removing unused cache class name invalidity exception methods --- lib/Doctrine/ORM/ORMException.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index bf200b7d5..90611ac33 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -275,16 +275,6 @@ class ORMException extends Exception )); } - /** - * @param string $className - * - * @return ORMException - */ - public static function invalidSecondLevelCache($className) - { - return new self(sprintf('Invalid cache class "%s". It must be a Doctrine\ORM\Cache.', $className)); - } - /** * @param string $className * @param string $fieldName From 6931cd08c4707f31c1c729d31ec5b0cfed64307d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 04:02:34 +0200 Subject: [PATCH 09/16] DDC-3078 - using an explicit CacheInstantiator interface to replace callable cache instantiators --- lib/Doctrine/ORM/Cache/CacheInstantiator.php | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/Doctrine/ORM/Cache/CacheInstantiator.php diff --git a/lib/Doctrine/ORM/Cache/CacheInstantiator.php b/lib/Doctrine/ORM/Cache/CacheInstantiator.php new file mode 100644 index 000000000..a88442b24 --- /dev/null +++ b/lib/Doctrine/ORM/Cache/CacheInstantiator.php @@ -0,0 +1,41 @@ +. + */ + +namespace Doctrine\ORM\Cache; + +use Doctrine\ORM\EntityManagerInterface; + +/** + * Contract for building second level cache instances. + * + * @since 2.5 + * @author Marco Pivetta + */ +interface CacheInstantiator +{ + /** + * Build timestamp cache region + * + * @param EntityManagerInterface $entityManager + * + * @return \Doctrine\ORM\Cache + */ + public function getCache(EntityManagerInterface $entityManager); +} From a790639167f6989dad8503a4e21603ca7302640f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 04:04:19 +0200 Subject: [PATCH 10/16] DDC-3078 - providing a default cache instantiator implementation --- .../ORM/Cache/DefaultCacheInstantiator.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php diff --git a/lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php b/lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php new file mode 100644 index 000000000..43d7df20f --- /dev/null +++ b/lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php @@ -0,0 +1,41 @@ +. + */ + +namespace Doctrine\ORM\Cache; + +use Doctrine\ORM\EntityManagerInterface; + +/** + * Default implementation of the {@see \Doctrine\ORM\Cache\CacheInstantiator}, responsible + * for producing an {@see \Doctrine\ORM\Cache\DefaultCache} + * + * @since 2.5 + * @author Marco Pivetta + */ +class DefaultCacheInstantiator implements CacheInstantiator +{ + /** + * {@inheritDoc} + */ + public function getCache(EntityManagerInterface $entityManager) + { + return new DefaultCache($entityManager); + } +} From 4b388b2ce8c5bf00b7ff18f0c7abe52d4a93d9dc Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 04:09:45 +0200 Subject: [PATCH 11/16] DDC-3078 - coverage for the default cache instantiator --- .../Cache/DefaultCacheInstantiatorTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php new file mode 100644 index 000000000..49c8573bc --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php @@ -0,0 +1,29 @@ +getMock('Doctrine\ORM\EntityManagerInterface'); + $config = $this->getMock('Doctrine\ORM\Configuration'); + $cacheConfig = $this->getMock('Doctrine\ORM\Cache\CacheConfiguration'); + + $entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($config)); + $config + ->expects($this->any()) + ->method('getSecondLevelCacheConfiguration') + ->will($this->returnValue($cacheConfig)); + + $instantiator = new DefaultCacheInstantiator(); + + $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCache', $instantiator->getCache($entityManager)); + } +} From df6a41136580528c415909df82a90ba28298acd2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 04:18:18 +0200 Subject: [PATCH 12/16] DDC-3078 - cache instantiator is used in the ORM instead of callables --- lib/Doctrine/ORM/Cache/CacheConfiguration.php | 25 +++++-------------- lib/Doctrine/ORM/EntityManager.php | 3 +-- .../Tests/ORM/Cache/CacheConfigTest.php | 19 ++------------ 3 files changed, 9 insertions(+), 38 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/CacheConfiguration.php b/lib/Doctrine/ORM/Cache/CacheConfiguration.php index 512caa98e..eab36e393 100644 --- a/lib/Doctrine/ORM/Cache/CacheConfiguration.php +++ b/lib/Doctrine/ORM/Cache/CacheConfiguration.php @@ -53,7 +53,7 @@ class CacheConfiguration private $queryValidator; /** - * @var callable|null + * @var CacheInstantiator|null */ private $cacheInstantiator; @@ -132,31 +132,18 @@ class CacheConfiguration } /** - * @param callable $instantiator responsible of retrieving an {@see \Doctrine\ORM\Cache} instance given - * a {@see \Doctrine\ORM\EntityManagerInterface} instance - * - * @throws ORMException if the given instantiator is not a valid callable + * @param CacheInstantiator */ - public function setCacheInstantiator($instantiator) + public function setCacheInstantiator(CacheInstantiator $cacheInstantiator) { - if ( ! is_callable($instantiator)) { - throw ORMException::invalidSecondLevelCacheInstantiator($instantiator); - } - - $this->cacheInstantiator = $instantiator; + $this->cacheInstantiator = $cacheInstantiator; } /** - * @return callable that + * @return CacheInstantiator */ public function getCacheInstantiator() { - if ( ! $this->cacheInstantiator) { - $this->cacheInstantiator = function (EntityManagerInterface $entityManager) { - return new DefaultCache($entityManager); - }; - } - - return $this->cacheInstantiator; + return $this->cacheInstantiator ?: $this->cacheInstantiator = new DefaultCacheInstantiator(); } } diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 175e3108e..5c8ce598b 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -166,8 +166,7 @@ use Doctrine\Common\Util\ClassUtils; ); if ($config->isSecondLevelCacheEnabled()) { - $cacheInstantiator = $config->getSecondLevelCacheConfiguration()->getCacheInstantiator(); - $this->cache = $cacheInstantiator($this); + $this->cache = $config->getSecondLevelCacheConfiguration()->getCacheInstantiator()->getCache($this); } } diff --git a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php index a12f94ec4..dafcd234c 100644 --- a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php @@ -32,18 +32,7 @@ class CacheConfigTest extends DoctrineTestCase */ public function testGetDefaultCacheIstantiator() { - $entityManager = $this->getMock('Doctrine\ORM\EntityManagerInterface'); - $config = $this->getMock('Doctrine\ORM\Configuration'); - - $entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($config)); - $config - ->expects($this->any()) - ->method('getSecondLevelCacheConfiguration') - ->will($this->returnValue($this->config)); - - $defaultIstantiator = $this->config->getCacheInstantiator(); - - $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCache', $defaultIstantiator($entityManager)); + $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultInstantiator', $this->config->getCacheInstantiator()); } /** @@ -51,14 +40,10 @@ class CacheConfigTest extends DoctrineTestCase */ public function testSetGetCacheIstantiator() { - $istantiator = function () {}; + $istantiator = $this->getMock('Doctrine\ORM\Cache\CacheInstantiator'); $this->config->setCacheInstantiator($istantiator); $this->assertSame($istantiator, $this->config->getCacheInstantiator()); - - $this->setExpectedException('Doctrine\ORM\ORMException'); - - $this->config->setCacheInstantiator(null); } public function testSetGetRegionLifetime() From 48e227167e851c10b036bfc11bba52ad78545782 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 04:18:51 +0200 Subject: [PATCH 13/16] DDC-3078 - cache instantiator related exceptions are not needed anymore --- lib/Doctrine/ORM/ORMException.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 90611ac33..799fd1b8d 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -262,19 +262,6 @@ class ORMException extends Exception return new self("Invalid repository class '".$className."'. It must be a Doctrine\Common\Persistence\ObjectRepository."); } - /** - * @param mixed $instantiator - * - * @return ORMException - */ - public static function invalidSecondLevelCacheInstantiator($instantiator) - { - return new self(sprintf( - 'The provided instantiator is not a valid callable, "%s" given.', - is_object($instantiator) ? get_class($instantiator) : gettype($instantiator) - )); - } - /** * @param string $className * @param string $fieldName From d57d4b71f968c19c3dbe32d6b18bd5b60bb41868 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 04:25:22 +0200 Subject: [PATCH 14/16] DDC-3078 - default cache instantiator class name was misstyped --- tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php index dafcd234c..1c88fcf17 100644 --- a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php @@ -32,7 +32,7 @@ class CacheConfigTest extends DoctrineTestCase */ public function testGetDefaultCacheIstantiator() { - $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultInstantiator', $this->config->getCacheInstantiator()); + $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCacheInstantiator', $this->config->getCacheInstantiator()); } /** From fa1cc9269ca1eb7a109c965b3f5bf3790c24ac8e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 10 Apr 2014 18:51:53 +0200 Subject: [PATCH 15/16] DDC-3078 - removing unused imports --- lib/Doctrine/ORM/Cache/CacheConfiguration.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Doctrine/ORM/Cache/CacheConfiguration.php b/lib/Doctrine/ORM/Cache/CacheConfiguration.php index eab36e393..aee703b55 100644 --- a/lib/Doctrine/ORM/Cache/CacheConfiguration.php +++ b/lib/Doctrine/ORM/Cache/CacheConfiguration.php @@ -20,8 +20,6 @@ namespace Doctrine\ORM\Cache; -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\ORMException; use Doctrine\ORM\Cache\Logging\CacheLogger; /** From 1dc3396ad4b692de4d97358221f343c9976a24ec Mon Sep 17 00:00:00 2001 From: fabios Date: Thu, 17 Apr 2014 15:16:46 -0400 Subject: [PATCH 16/16] DDC-3078 - Use CacheFactory instead of cache instantiator --- lib/Doctrine/ORM/Cache/CacheConfiguration.php | 21 ---------- lib/Doctrine/ORM/Cache/CacheFactory.php | 9 ++++ lib/Doctrine/ORM/Cache/CacheInstantiator.php | 41 ------------------- .../ORM/Cache/DefaultCacheFactory.php | 8 ++++ .../ORM/Cache/DefaultCacheInstantiator.php | 41 ------------------- lib/Doctrine/ORM/EntityManager.php | 4 +- .../Tests/ORM/Cache/CacheConfigTest.php | 19 --------- .../Cache/DefaultCacheInstantiatorTest.php | 29 ------------- 8 files changed, 20 insertions(+), 152 deletions(-) delete mode 100644 lib/Doctrine/ORM/Cache/CacheInstantiator.php delete mode 100644 lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php delete mode 100644 tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php diff --git a/lib/Doctrine/ORM/Cache/CacheConfiguration.php b/lib/Doctrine/ORM/Cache/CacheConfiguration.php index aee703b55..760352314 100644 --- a/lib/Doctrine/ORM/Cache/CacheConfiguration.php +++ b/lib/Doctrine/ORM/Cache/CacheConfiguration.php @@ -50,11 +50,6 @@ class CacheConfiguration */ private $queryValidator; - /** - * @var CacheInstantiator|null - */ - private $cacheInstantiator; - /** * @return \Doctrine\ORM\Cache\CacheFactory|null */ @@ -128,20 +123,4 @@ class CacheConfiguration { $this->queryValidator = $validator; } - - /** - * @param CacheInstantiator - */ - public function setCacheInstantiator(CacheInstantiator $cacheInstantiator) - { - $this->cacheInstantiator = $cacheInstantiator; - } - - /** - * @return CacheInstantiator - */ - public function getCacheInstantiator() - { - return $this->cacheInstantiator ?: $this->cacheInstantiator = new DefaultCacheInstantiator(); - } } diff --git a/lib/Doctrine/ORM/Cache/CacheFactory.php b/lib/Doctrine/ORM/Cache/CacheFactory.php index b1ce7f8e3..793392199 100644 --- a/lib/Doctrine/ORM/Cache/CacheFactory.php +++ b/lib/Doctrine/ORM/Cache/CacheFactory.php @@ -101,4 +101,13 @@ interface CacheFactory * @return \Doctrine\ORM\Cache\TimestampRegion The timestamp region. */ public function getTimestampRegion(); + + /** + * Build \Doctrine\ORM\Cache + * + * @param EntityManagerInterface $entityManager + * + * @return \Doctrine\ORM\Cache + */ + public function createCache(EntityManagerInterface $entityManager); } diff --git a/lib/Doctrine/ORM/Cache/CacheInstantiator.php b/lib/Doctrine/ORM/Cache/CacheInstantiator.php deleted file mode 100644 index a88442b24..000000000 --- a/lib/Doctrine/ORM/Cache/CacheInstantiator.php +++ /dev/null @@ -1,41 +0,0 @@ -. - */ - -namespace Doctrine\ORM\Cache; - -use Doctrine\ORM\EntityManagerInterface; - -/** - * Contract for building second level cache instances. - * - * @since 2.5 - * @author Marco Pivetta - */ -interface CacheInstantiator -{ - /** - * Build timestamp cache region - * - * @param EntityManagerInterface $entityManager - * - * @return \Doctrine\ORM\Cache - */ - public function getCache(EntityManagerInterface $entityManager); -} diff --git a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php index 9ae425df3..207c34a61 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php +++ b/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php @@ -230,4 +230,12 @@ class DefaultCacheFactory implements CacheFactory return $this->timestampRegion; } + + /** + * {@inheritdoc} + */ + public function createCache(EntityManagerInterface $em) + { + return new DefaultCache($em); + } } diff --git a/lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php b/lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php deleted file mode 100644 index 43d7df20f..000000000 --- a/lib/Doctrine/ORM/Cache/DefaultCacheInstantiator.php +++ /dev/null @@ -1,41 +0,0 @@ -. - */ - -namespace Doctrine\ORM\Cache; - -use Doctrine\ORM\EntityManagerInterface; - -/** - * Default implementation of the {@see \Doctrine\ORM\Cache\CacheInstantiator}, responsible - * for producing an {@see \Doctrine\ORM\Cache\DefaultCache} - * - * @since 2.5 - * @author Marco Pivetta - */ -class DefaultCacheInstantiator implements CacheInstantiator -{ - /** - * {@inheritDoc} - */ - public function getCache(EntityManagerInterface $entityManager) - { - return new DefaultCache($entityManager); - } -} diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 5c8ce598b..cd433a7fa 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -166,7 +166,9 @@ use Doctrine\Common\Util\ClassUtils; ); if ($config->isSecondLevelCacheEnabled()) { - $this->cache = $config->getSecondLevelCacheConfiguration()->getCacheInstantiator()->getCache($this); + $cacheConfig = $config->getSecondLevelCacheConfiguration(); + $cacheFactory = $cacheConfig->getCacheFactory(); + $this->cache = $cacheFactory->createCache($this); } } diff --git a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php index 1c88fcf17..412d30207 100644 --- a/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/CacheConfigTest.php @@ -27,25 +27,6 @@ class CacheConfigTest extends DoctrineTestCase $this->config = new CacheConfiguration(); } - /** - * @covers \Doctrine\ORM\Cache\CacheConfiguration::getCacheInstantiator - */ - public function testGetDefaultCacheIstantiator() - { - $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCacheInstantiator', $this->config->getCacheInstantiator()); - } - - /** - * @covers \Doctrine\ORM\Cache\CacheConfiguration::getCacheInstantiator - */ - public function testSetGetCacheIstantiator() - { - $istantiator = $this->getMock('Doctrine\ORM\Cache\CacheInstantiator'); - - $this->config->setCacheInstantiator($istantiator); - $this->assertSame($istantiator, $this->config->getCacheInstantiator()); - } - public function testSetGetRegionLifetime() { $config = $this->config->getRegionsConfiguration(); diff --git a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php b/tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php deleted file mode 100644 index 49c8573bc..000000000 --- a/tests/Doctrine/Tests/ORM/Cache/DefaultCacheInstantiatorTest.php +++ /dev/null @@ -1,29 +0,0 @@ -getMock('Doctrine\ORM\EntityManagerInterface'); - $config = $this->getMock('Doctrine\ORM\Configuration'); - $cacheConfig = $this->getMock('Doctrine\ORM\Cache\CacheConfiguration'); - - $entityManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($config)); - $config - ->expects($this->any()) - ->method('getSecondLevelCacheConfiguration') - ->will($this->returnValue($cacheConfig)); - - $instantiator = new DefaultCacheInstantiator(); - - $this->assertInstanceOf('Doctrine\ORM\Cache\DefaultCache', $instantiator->getCache($entityManager)); - } -}