From a665cb0229a9caf53b29b2f42b436caf24e169a1 Mon Sep 17 00:00:00 2001 From: Giorgio Premi Date: Mon, 18 Aug 2014 12:02:51 +0200 Subject: [PATCH 01/10] DefaultRepositoryFactory: single repository for aliased entities --- .../ORM/Repository/DefaultRepositoryFactory.php | 10 ++++++---- .../ORM/Functional/EntityRepositoryTest.php | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index 2774dea68..d756ed4f9 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -43,13 +43,15 @@ class DefaultRepositoryFactory implements RepositoryFactory { $entityName = ltrim($entityName, '\\'); - if (isset($this->repositoryList[$entityName])) { - return $this->repositoryList[$entityName]; + $class = $entityManager->getClassMetadata($entityName)->getName(); + + if (isset($this->repositoryList[$class])) { + return $this->repositoryList[$class]; } $repository = $this->createRepository($entityManager, $entityName); - $this->repositoryList[$entityName] = $repository; + $this->repositoryList[$class] = $repository; return $repository; } @@ -74,4 +76,4 @@ class DefaultRepositoryFactory implements RepositoryFactory return new $repositoryClassName($entityManager, $metadata); } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 12b941efc..193497a1b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -611,6 +611,23 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); } + public function testSingleRepositoryInstanceForAnEntity() + { + $config = $this->_em->getConfiguration(); + $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); + $config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS'); + + $this->assertSame( + $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'), + $this->_em->getRepository('Aliased:CmsUser') + ); + + $this->assertSame( + $this->_em->getRepository('Aliased:CmsUser'), + $this->_em->getRepository('AliasedAgain:CmsUser') + ); + } + /** * @group DDC-1376 * From 7865de92abcbf714e2bd9360cd3940e8d86be175 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:01:52 +0200 Subject: [PATCH 02/10] #1112 - renamed $class to $className --- lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index d756ed4f9..a0564ba94 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -43,15 +43,15 @@ class DefaultRepositoryFactory implements RepositoryFactory { $entityName = ltrim($entityName, '\\'); - $class = $entityManager->getClassMetadata($entityName)->getName(); + $className = $entityManager->getClassMetadata($entityName)->getName(); - if (isset($this->repositoryList[$class])) { - return $this->repositoryList[$class]; + if (isset($this->repositoryList[$className])) { + return $this->repositoryList[$className]; } $repository = $this->createRepository($entityManager, $entityName); - $this->repositoryList[$class] = $repository; + $this->repositoryList[$className] = $repository; return $repository; } From 3fed769b4030a5b5f4342c9cbee9a287482f4d92 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:02:34 +0200 Subject: [PATCH 03/10] #1112 - avoiding useless assignments/splitted return statement --- lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index a0564ba94..e7f0aca13 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -49,11 +49,7 @@ class DefaultRepositoryFactory implements RepositoryFactory return $this->repositoryList[$className]; } - $repository = $this->createRepository($entityManager, $entityName); - - $this->repositoryList[$className] = $repository; - - return $repository; + return $this->repositoryList[$className] = $this->createRepository($entityManager, $entityName); } /** From 01f22988b17e5b2d93438a0cc502b830d5816899 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:06:37 +0200 Subject: [PATCH 04/10] #1112 - cleaning up repository test - makes assertions more clear/simpler to read --- .../Tests/ORM/Functional/EntityRepositoryTest.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 193497a1b..4f11adc84 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -611,21 +611,17 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); } - public function testSingleRepositoryInstanceForAnEntity() + public function testSingleRepositoryInstanceForDifferentEntityAliases() { $config = $this->_em->getConfiguration(); + $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); $config->addEntityNamespace('AliasedAgain', 'Doctrine\Tests\Models\CMS'); - $this->assertSame( - $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'), - $this->_em->getRepository('Aliased:CmsUser') - ); + $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); - $this->assertSame( - $this->_em->getRepository('Aliased:CmsUser'), - $this->_em->getRepository('AliasedAgain:CmsUser') - ); + $this->assertSame($repository, $this->_em->getRepository('Aliased:CmsUser')); + $this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser')); } /** From ae16afa42899ffa70f179010c3e9fb47c0e7e946 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:07:07 +0200 Subject: [PATCH 05/10] #1112 - adding DDC-3257 group to test method --- tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 4f11adc84..59778b42d 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -611,6 +611,9 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); } + /** + * @group DDC-3257 + */ public function testSingleRepositoryInstanceForDifferentEntityAliases() { $config = $this->_em->getConfiguration(); From 36bbd28b75f6ee6bc4596e306fd20feaba7f8b31 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:11:39 +0200 Subject: [PATCH 06/10] #1112 - adding test to verify that leading backslash is not relevant when fetching repositories --- .../Tests/ORM/Functional/EntityRepositoryTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 59778b42d..705802306 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -627,6 +627,17 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertSame($repository, $this->_em->getRepository('AliasedAgain:CmsUser')); } + /** + * @group DDC-3257 + */ + public function testCanRetrieveRepositoryFromClassNameWithLeadingBackslash() + { + $this->assertSame( + $this->_em->getRepository('\\Doctrine\\Tests\\Models\\CMS\\CmsUser'), + $this->_em->getRepository('Doctrine\\Tests\\Models\\CMS\\CmsUser') + ); + } + /** * @group DDC-1376 * From 19d3552f2a36ab7f005f59615f5119961f2a71a7 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:14:53 +0200 Subject: [PATCH 07/10] #1112 - Removing useless trimming of the entity name being passed in --- lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index e7f0aca13..24f5f2f84 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -41,8 +41,6 @@ class DefaultRepositoryFactory implements RepositoryFactory */ public function getRepository(EntityManagerInterface $entityManager, $entityName) { - $entityName = ltrim($entityName, '\\'); - $className = $entityManager->getClassMetadata($entityName)->getName(); if (isset($this->repositoryList[$className])) { From bf03694e28b3c5e080114caf845ba3b5338eef61 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:16:45 +0200 Subject: [PATCH 08/10] #1112 - Yoday need you may, better IDE hinting as well needed is. --- lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index 24f5f2f84..ae46c2438 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -60,10 +60,11 @@ class DefaultRepositoryFactory implements RepositoryFactory */ protected function createRepository(EntityManagerInterface $entityManager, $entityName) { + /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */ $metadata = $entityManager->getClassMetadata($entityName); $repositoryClassName = $metadata->customRepositoryClassName; - if ($repositoryClassName === null) { + if (null === $repositoryClassName) { $configuration = $entityManager->getConfiguration(); $repositoryClassName = $configuration->getDefaultRepositoryClassName(); } From aab7fce2d403ddee4cff2a3c50e2aa6290e567fe Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:17:54 +0200 Subject: [PATCH 09/10] #1112 - Elvis operator reduces code duplication even more --- lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index ae46c2438..b09430816 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -62,12 +62,8 @@ class DefaultRepositoryFactory implements RepositoryFactory { /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */ $metadata = $entityManager->getClassMetadata($entityName); - $repositoryClassName = $metadata->customRepositoryClassName; - - if (null === $repositoryClassName) { - $configuration = $entityManager->getConfiguration(); - $repositoryClassName = $configuration->getDefaultRepositoryClassName(); - } + $repositoryClassName = $metadata->customRepositoryClassName + ?: $entityManager->getConfiguration()->getDefaultRepositoryClassName(); return new $repositoryClassName($entityManager, $metadata); } From dfbaac0401dd146194708e52043800ed9b573ac0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 18 Aug 2014 15:18:59 +0200 Subject: [PATCH 10/10] #1112 - Fixed type-hint that is incompatible with most IDEs --- lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php index b09430816..b9afbfabf 100644 --- a/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php +++ b/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php @@ -32,7 +32,7 @@ class DefaultRepositoryFactory implements RepositoryFactory /** * The list of EntityRepository instances. * - * @var array<\Doctrine\Common\Persistence\ObjectRepository> + * @var \Doctrine\Common\Persistence\ObjectRepository[] */ private $repositoryList = array();