From dff5dae416b09494094612782eab505e846e1037 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 27 Nov 2010 20:53:26 +0100 Subject: [PATCH] DDC-897 - Make ClassMetadataFactory configurable. --- lib/Doctrine/ORM/Configuration.php | 21 +++++++++++++++++++ lib/Doctrine/ORM/EntityManager.php | 6 +++++- .../ORM/Mapping/ClassMetadataFactory.php | 6 ++---- .../Command/GenerateEntitiesCommand.php | 3 ++- .../ORM/Mapping/AnnotationDriverTest.php | 3 ++- .../Mapping/BasicInheritanceMappingTest.php | 3 ++- .../ORM/Mapping/ClassMetadataFactoryTest.php | 3 ++- .../ORM/Mapping/YamlMappingDriverTest.php | 3 ++- .../ORM/Tools/ConvertDoctrine1SchemaTest.php | 3 ++- .../AbstractClassMetadataExporterTest.php | 6 ++++-- 10 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 5e5341d1d..0bc206d27 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -462,4 +462,25 @@ class Configuration extends \Doctrine\DBAL\Configuration { $this->_attributes['customHydrationModes'][$modeName] = $hydrator; } + + /** + * Set a class metadata factory. + * + * @param string $cmf + */ + public function setClassMetadataFactoryName($cmfName) + { + $this->_attributes['classMetadataFactoryName'] = $cmfName; + } + + /** + * @return string + */ + public function getClassMetadataFactoryName() + { + if (!isset($this->_attributes['classMetadataFactoryName'])) { + $this->_attributes['classMetadataFactoryName'] = 'Doctrine\ORM\Mapping\ClassMetadataFactory'; + } + return $this->_attributes['classMetadataFactoryName']; + } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index abd89b0d4..0ab1541ba 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -118,8 +118,12 @@ class EntityManager $this->conn = $conn; $this->config = $config; $this->eventManager = $eventManager; - $this->metadataFactory = new ClassMetadataFactory($this); + + $metadataFactoryClassName = $config->getClassMetadataFactoryName(); + $this->metadataFactory = new $metadataFactoryClassName; + $this->metadataFactory->setEntityManager($this); $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); + $this->unitOfWork = new UnitOfWork($this); $this->proxyFactory = new ProxyFactory($this, $config->getProxyDir(), diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php index 806d41e78..174e95eaf 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php @@ -74,11 +74,9 @@ class ClassMetadataFactory private $initialized = false; /** - * Creates a new factory instance that uses the given metadata driver implementation. - * - * @param $driver The metadata driver to use. + * @param EntityManager $$em */ - public function __construct(EntityManager $em) + public function setEntityManager(EntityManager $em) { $this->em = $em; } diff --git a/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php b/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php index 08654ba18..3014d57ae 100644 --- a/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php +++ b/lib/Doctrine/ORM/Tools/Console/Command/GenerateEntitiesCommand.php @@ -96,7 +96,8 @@ EOT { $em = $this->getHelper('em')->getEntityManager(); - $cmf = new DisconnectedClassMetadataFactory($em); + $cmf = new DisconnectedClassMetadataFactory(); + $cmf->setEntityManager($em); $metadatas = $cmf->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php index b34d727a7..c2f3a13c4 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php @@ -122,7 +122,8 @@ class AnnotationDriverTest extends AbstractMappingDriverTest $em = $this->_getTestEntityManager(); $em->getConfiguration()->setMetadataDriverImpl($annotationDriver); - $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory($em); + $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory(); + $factory->setEntityManager($em); $classPage = new ClassMetadata('Doctrine\Tests\Models\DirectoryTree\File'); $classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File'); diff --git a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php index fdaf30f41..671028852 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/BasicInheritanceMappingTest.php @@ -11,7 +11,8 @@ class BasicInheritanceMappingTest extends \Doctrine\Tests\OrmTestCase private $_factory; protected function setUp() { - $this->_factory = new ClassMetadataFactory($this->_getTestEntityManager()); + $this->_factory = new ClassMetadataFactory(); + $this->_factory->setEntityManager($this->_getTestEntityManager()); } /** diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index cbd9ce5e5..72a49a701 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -42,7 +42,8 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); // SUT - $cmf = new ClassMetadataFactoryTestSubject($entityManager); + $cmf = new ClassMetadataFactoryTestSubject(); + $cmf->setEntityManager($entityManager); $cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1); // Prechecks diff --git a/tests/Doctrine/Tests/ORM/Mapping/YamlMappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/YamlMappingDriverTest.php index 4e58485f1..34c3fa810 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/YamlMappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/YamlMappingDriverTest.php @@ -31,7 +31,8 @@ class YamlMappingDriverTest extends AbstractMappingDriverTest $em = $this->_getTestEntityManager(); $em->getConfiguration()->setMetadataDriverImpl($yamlDriver); - $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory($em); + $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory(); + $factory->setEntityManager($em); $classPage = new ClassMetadata('Doctrine\Tests\Models\DirectoryTree\File'); $classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File'); diff --git a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php index 6f542dba9..b944c26d9 100644 --- a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php @@ -77,7 +77,8 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase $metadataDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/convert'); $em = $this->_createEntityManager($metadataDriver); - $cmf = new DisconnectedClassMetadataFactory($em); + $cmf = new DisconnectedClassMetadataFactory(); + $cmf->setEntityManager($em); $metadata = $cmf->getAllMetadata(); $profileClass = $metadata[0]; $userClass = $metadata[1]; diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php index 95759c539..4a85918d6 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/AbstractClassMetadataExporterTest.php @@ -88,10 +88,12 @@ abstract class AbstractClassMetadataExporterTest extends \Doctrine\Tests\OrmTest protected function _createClassMetadataFactory($em, $type) { if ($type === 'annotation') { - return new ClassMetadataFactory($em); + $factory = new ClassMetadataFactory(); } else { - return new DisconnectedClassMetadataFactory($em); + $factory = new DisconnectedClassMetadataFactory(); } + $factory->setEntityManager($em); + return $factory; } public function testExportDirectoryAndFilesAreCreated()