From 84cd22d7f776b5ef30625116d349539a98d6e53c Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 8 Sep 2011 14:36:13 -0300 Subject: [PATCH 1/3] new branch for DDC-753 --- lib/Doctrine/ORM/Configuration.php | 27 +++++++++++++++++++ .../ORM/Mapping/ClassMetadataInfo.php | 5 ++++ lib/Doctrine/ORM/ORMException.php | 5 ++++ 3 files changed, 37 insertions(+) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index d16b22b54..99ee397cc 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -516,4 +516,31 @@ class Configuration extends \Doctrine\DBAL\Configuration } return $this->_attributes['classMetadataFactoryName']; } + + /** + * Set default repository class. + * + * @since 2.2 + * @param string $className + * @throws ORMException If not implements Doctrine\Common\Persistence\ObjectRepository + */ + public function setDefaultRepositoryClassName($className) + { + if (!is_subclass_of($className, 'Doctrine\Common\Persistence\ObjectRepository')) { + throw ORMException::invalidObjectRepository($className); + } + $this->_attributes['defaultRepositoryClassName'] = $className; + } + + /** + * Get default repository class. + * + * @since 2.2 + * @return string + */ + public function getDefaultRepositoryClassName() + { + return isset($this->_attributes['defaultRepositoryClassName']) ? + $this->_attributes['defaultRepositoryClassName'] : 'Doctrine\ORM\EntityRepository'; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 17255cb23..e34db3f2d 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1519,12 +1519,17 @@ class ClassMetadataInfo implements ClassMetadata * Registers a custom repository class for the entity class. * * @param string $mapperClassName The class name of the custom mapper. + * @throws MappingException If not implements Doctrine\Common\Persistence\ObjectRepository */ public function setCustomRepositoryClass($repositoryClassName) { if ($repositoryClassName !== null && strpos($repositoryClassName, '\\') === false && strlen($this->namespace) > 0) { $repositoryClassName = $this->namespace . '\\' . $repositoryClassName; + + if (!is_subclass_of($repositoryClassName, 'Doctrine\Common\Persistence\ObjectRepository')) { + throw MappingException::invalidObjectRepository($repositoryClassName); + } } $this->customRepositoryClassName = $repositoryClassName; } diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index 89203359c..c1115c260 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -130,4 +130,9 @@ class ORMException extends Exception "Unknown Entity namespace alias '$entityNamespaceAlias'." ); } + + public static function invalidObjectRepository($className) { + return new self("Invalid repository class '".$className."'. ". + "it must implement Doctrine\Common\Persistence\ObjectRepository."); + } } From 43ccd9ead6b1558cd2fa3976b63ca43f65a3ebc1 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 8 Sep 2011 15:54:49 -0300 Subject: [PATCH 2/3] tests for DDC-753 --- lib/Doctrine/ORM/Configuration.php | 3 +- lib/Doctrine/ORM/EntityManager.php | 3 +- .../ORM/Mapping/ClassMetadataInfo.php | 5 -- .../Models/DDC753/DDC753CustomRepository.php | 36 ++++++++ .../Models/DDC753/DDC753DefaultRepository.php | 82 +++++++++++++++++++ .../DDC753EntityWithCustomRepository.php | 39 +++++++++ ...DC753EntityWithDefaultCustomRepository.php | 39 +++++++++ .../DDC753EntityWithInvalidRepository.php | 39 +++++++++ .../Models/DDC753/DDC753InvalidRepository.php | 28 +++++++ .../ORM/Functional/EntityRepositoryTest.php | 53 ++++++++++++ 10 files changed, 320 insertions(+), 7 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 99ee397cc..42ceded0d 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -526,7 +526,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function setDefaultRepositoryClassName($className) { - if (!is_subclass_of($className, 'Doctrine\Common\Persistence\ObjectRepository')) { + $class = new \ReflectionClass($className); + if (!$class->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) { throw ORMException::invalidObjectRepository($className); } $this->_attributes['defaultRepositoryClassName'] = $className; diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0379cc435..9be5b50da 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -576,7 +576,8 @@ class EntityManager implements ObjectManager if ($customRepositoryClassName !== null) { $repository = new $customRepositoryClassName($this, $metadata); } else { - $repository = new EntityRepository($this, $metadata); + $repositoryClass = $this->config->getDefaultRepositoryClassName(); + $repository = new $repositoryClass($this, $metadata); } $this->repositories[$entityName] = $repository; diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index e34db3f2d..17255cb23 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1519,17 +1519,12 @@ class ClassMetadataInfo implements ClassMetadata * Registers a custom repository class for the entity class. * * @param string $mapperClassName The class name of the custom mapper. - * @throws MappingException If not implements Doctrine\Common\Persistence\ObjectRepository */ public function setCustomRepositoryClass($repositoryClassName) { if ($repositoryClassName !== null && strpos($repositoryClassName, '\\') === false && strlen($this->namespace) > 0) { $repositoryClassName = $this->namespace . '\\' . $repositoryClassName; - - if (!is_subclass_of($repositoryClassName, 'Doctrine\Common\Persistence\ObjectRepository')) { - throw MappingException::invalidObjectRepository($repositoryClassName); - } } $this->customRepositoryClassName = $repositoryClassName; } diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php new file mode 100644 index 000000000..d277cf8aa --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php @@ -0,0 +1,36 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +use Doctrine\ORM\EntityRepository; + +class DDC753CustomRepository extends EntityRepository +{ + + /** + * @return bool + */ + public function isCustomRepository() + { + return true; + } + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php new file mode 100644 index 000000000..85a981c09 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php @@ -0,0 +1,82 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +use Doctrine\ORM\EntityRepository; + +class DDC753DefaultRepository implements \Doctrine\Common\Persistence\ObjectRepository +{ + + /** + * @var Doctrine\ORM\EntityRepository + */ + private $repository; + + /** + * @param EntityManager $em + * @param ClassMetadata $classMetadata + */ + public function __construct($em, $class) + { + $this->repository = new EntityRepository($em, $class); + } + + /** + * {@inheritdoc} + */ + public function find($id) + { + return $this->repository->find($id); + } + + /** + * {@inheritdoc} + */ + public function findAll() + { + return $this->repository->findAll; + } + + /** + * {@inheritdoc} + */ + public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + /** + * {@inheritdoc} + */ + public function findOneBy(array $criteria) + { + return $this->repository->findOneBy($criteria); + } + + /** + * @return bool + */ + public function isDefaultRepository() + { + return true; + } + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php new file mode 100644 index 000000000..fa8a77957 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php @@ -0,0 +1,39 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +/** + * @Entity(repositoryClass = "Doctrine\Tests\Models\DDC753\DDC753CustomRepository") + */ +class DDC753EntityWithCustomRepository +{ + + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** @column(type="string") */ + protected $name; + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php new file mode 100644 index 000000000..43f435a98 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php @@ -0,0 +1,39 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +/** + * @Entity() + */ +class DDC753EntityWithDefaultCustomRepository +{ + + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** @column(type="string") */ + protected $name; + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php new file mode 100644 index 000000000..e7c21762a --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php @@ -0,0 +1,39 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +/** + * @Entity(repositoryClass = "\stdClass") + */ +class DDC753EntityWithInvalidRepository +{ + + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** @column(type="string") */ + protected $name; + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php new file mode 100644 index 000000000..9f0937aa1 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php @@ -0,0 +1,28 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +use Doctrine\ORM\EntityRepository; + +class DDC753InvalidRepository +{ + +} \ 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 16d3b7bb4..40133c3cf 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -433,5 +433,58 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertSame($usersAsc[0], $usersDesc[1]); $this->assertSame($usersAsc[1], $usersDesc[0]); } + + + /** + * @group DDC-753 + */ + public function testDefaultRepositoryClassName() + { + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository'); + $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository", $repos); + $this->assertTrue($repos->isDefaultRepository()); + + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository'); + $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753CustomRepository", $repos); + $this->assertTrue($repos->isCustomRepository()); + + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); + $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\ORM\EntityRepository"); + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + + } + + + /** + * @group DDC-753 + * @expectedException Doctrine\ORM\ORMException + * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. it must implement Doctrine\Common\Persistence\ObjectRepository. + */ + public function testSetDefaultRepositoryInvalidClassError() + { + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); + } + + + /** + * @group DDC-753 + * @todo check it is necessary throws exception when a repository is not a Doctrine\Common\Persistence\ObjectRepository + * ClassMetadataInfo#setCustomRepositoryClass + */ + public function testEntityWithInvalidRepositoryError() + { + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithInvalidRepository'); + + $this->assertInstanceOf("\stdClass", $repos); + + $this->markTestIncomplete(); + } } From 3707b34cbb1bb37dfe6b0f50d915d31303d3b7ea Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 8 Sep 2011 17:21:06 -0300 Subject: [PATCH 3/3] Change repository type for EntityRepository --- lib/Doctrine/ORM/Configuration.php | 8 +-- lib/Doctrine/ORM/ORMException.php | 5 +- .../Models/DDC753/DDC753DefaultRepository.php | 49 +------------------ .../ORM/Functional/EntityRepositoryTest.php | 17 +------ 4 files changed, 9 insertions(+), 70 deletions(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 42ceded0d..ee6496087 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -522,13 +522,13 @@ class Configuration extends \Doctrine\DBAL\Configuration * * @since 2.2 * @param string $className - * @throws ORMException If not implements Doctrine\Common\Persistence\ObjectRepository + * @throws ORMException If not is a Doctrine\ORM\EntityRepository */ public function setDefaultRepositoryClassName($className) { - $class = new \ReflectionClass($className); - if (!$class->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) { - throw ORMException::invalidObjectRepository($className); + if ($className != "Doctrine\ORM\EntityRepository" && + !is_subclass_of($className, 'Doctrine\ORM\EntityRepository')){ + throw ORMException::invalidEntityRepository($className); } $this->_attributes['defaultRepositoryClassName'] = $className; } diff --git a/lib/Doctrine/ORM/ORMException.php b/lib/Doctrine/ORM/ORMException.php index c1115c260..aa9657552 100644 --- a/lib/Doctrine/ORM/ORMException.php +++ b/lib/Doctrine/ORM/ORMException.php @@ -131,8 +131,9 @@ class ORMException extends Exception ); } - public static function invalidObjectRepository($className) { + public static function invalidEntityRepository($className) + { return new self("Invalid repository class '".$className."'. ". - "it must implement Doctrine\Common\Persistence\ObjectRepository."); + "it must be a Doctrine\ORM\EntityRepository."); } } diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php index 85a981c09..feba2cb50 100644 --- a/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php @@ -22,55 +22,8 @@ namespace Doctrine\Tests\Models\DDC753; use Doctrine\ORM\EntityRepository; -class DDC753DefaultRepository implements \Doctrine\Common\Persistence\ObjectRepository +class DDC753DefaultRepository extends EntityRepository { - - /** - * @var Doctrine\ORM\EntityRepository - */ - private $repository; - - /** - * @param EntityManager $em - * @param ClassMetadata $classMetadata - */ - public function __construct($em, $class) - { - $this->repository = new EntityRepository($em, $class); - } - - /** - * {@inheritdoc} - */ - public function find($id) - { - return $this->repository->find($id); - } - - /** - * {@inheritdoc} - */ - public function findAll() - { - return $this->repository->findAll; - } - - /** - * {@inheritdoc} - */ - public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) - { - return $this->repository->findBy($criteria, $orderBy, $limit, $offset); - } - - /** - * {@inheritdoc} - */ - public function findOneBy(array $criteria) - { - return $this->repository->findOneBy($criteria); - } - /** * @return bool */ diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 40133c3cf..c5216fb94 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -463,7 +463,7 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase /** * @group DDC-753 * @expectedException Doctrine\ORM\ORMException - * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. it must implement Doctrine\Common\Persistence\ObjectRepository. + * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. it must be a Doctrine\ORM\EntityRepository. */ public function testSetDefaultRepositoryInvalidClassError() { @@ -471,20 +471,5 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); } - - /** - * @group DDC-753 - * @todo check it is necessary throws exception when a repository is not a Doctrine\Common\Persistence\ObjectRepository - * ClassMetadataInfo#setCustomRepositoryClass - */ - public function testEntityWithInvalidRepositoryError() - { - $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); - $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithInvalidRepository'); - - $this->assertInstanceOf("\stdClass", $repos); - - $this->markTestIncomplete(); - } }