From 0b68e9473dcdbca3c9776050eda73129285608a1 Mon Sep 17 00:00:00 2001 From: jwage Date: Wed, 24 Mar 2010 15:31:54 +0000 Subject: [PATCH] [2.0][DDC-449] Fixing issue with ClassMetadataReader and existing driver sources being added --- .../ORM/Tools/ClassMetadataReader.php | 30 +++++++++++++------ .../Tools/Export/ClassMetadataExporter.php | 1 + .../ORM/Tools/Export/ExportException.php | 11 +++++-- .../ORM/Functional/DatabaseDriverTest.php | 7 ++--- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/Doctrine/ORM/Tools/ClassMetadataReader.php b/lib/Doctrine/ORM/Tools/ClassMetadataReader.php index 7e95a78f4..5d8cd8e8b 100644 --- a/lib/Doctrine/ORM/Tools/ClassMetadataReader.php +++ b/lib/Doctrine/ORM/Tools/ClassMetadataReader.php @@ -26,7 +26,8 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo, Doctrine\ORM\Mapping\MappingException, Doctrine\ORM\Mapping\Driver\Driver, Doctrine\ORM\Mapping\Driver\AnnotationDriver, - Doctrine\ORM\EntityManager; + Doctrine\ORM\EntityManager, + Doctrine\ORM\Tools\Export\ExportException; /** * Class to read metadata mapping information from multiple sources into an array @@ -92,9 +93,15 @@ class ClassMetadataReader * directories. Reads the mapping directories and populates ClassMetadataInfo * instances. * + * If you specify $autoload = true then this method will return ClassMetadata + * instances instead of ClassMetadataInfo instances. Keep in mind that if you + * specify it to autoload and it doesn't find the class your autoloader may + * throw an error. + * + * @param bool $autoload Whether or to try and autoload the classes * @return array $classes */ - public function getMetadatas() + public function getMetadatas($autoload = false) { $classes = array(); @@ -104,7 +111,7 @@ class ClassMetadataReader $allClasses = $driver->getAllClassNames(); foreach ($allClasses as $className) { - if (class_exists($className, false)) { + if (class_exists($className, $autoload)) { $metadata = new ClassMetadata($className); } else { $metadata = new ClassMetadataInfo($className); @@ -183,9 +190,12 @@ class ClassMetadataReader private function _determineSourceType($source) { + if ($source instanceof \Doctrine\ORM\Mapping\Driver\Driver) { + $type = array_search(get_class($source), self::$_mappingDrivers); + return $type; // If the --from= is a directory lets determine if it is // annotations, yaml, xml, etc. - if (is_dir($source)) { + } else if (is_dir($source)) { $source = realpath($source); // Find the files in the directory @@ -218,12 +228,14 @@ class ClassMetadataReader private function _getSourceByType($type, $source) { - $source = realpath($source); - // If --from==database then the source is an instance of SchemaManager - // for the current EntityMAnager - if ($type == 'database' && $this->_em) { - return $this->_em->getConnection()->getSchemaManager(); + // for the current EntityManager + if ($type == 'database') { + if ($source instanceof \Doctrine\ORM\Mapping\Driver\DatabaseDriver) { + return $source; + } else if ($this->_em) { + return $this->_em->getConnection()->getSchemaManager(); + } // If source is annotation then lets try and find the existing annotation // driver for the source instead of re-creating a new instance } else if ($type == 'annotation') { diff --git a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php index 6dfff46e9..e93f77e35 100644 --- a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php @@ -23,6 +23,7 @@ namespace Doctrine\ORM\Tools\Export; use Doctrine\ORM\Tools\ClassMetadataReader, + Doctrine\ORM\Tools\Export\ExportException, Doctrine\ORM\EntityManager; /** diff --git a/lib/Doctrine/ORM/Tools/Export/ExportException.php b/lib/Doctrine/ORM/Tools/Export/ExportException.php index 01012e93f..6e7826e44 100644 --- a/lib/Doctrine/ORM/Tools/Export/ExportException.php +++ b/lib/Doctrine/ORM/Tools/Export/ExportException.php @@ -2,12 +2,17 @@ namespace Doctrine\ORM\Tools\Export; -class ExportException extends ORMException { - public static function invalidExporterDriverType($type) { +use Doctrine\ORM\ORMException; + +class ExportException extends ORMException +{ + public static function invalidExporterDriverType($type) + { return new self("The specified export driver '$type' does not exist"); } - public static function invalidMappingDriverType($type) { + public static function invalidMappingDriverType($type) + { return new self("The mapping driver '$type' does not exist"); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php index b74c5835d..6b963c098 100644 --- a/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php @@ -4,7 +4,7 @@ namespace Doctrine\Tests\ORM\Functional; require_once __DIR__ . '/../../TestInit.php'; -use Doctrine\ORM\Tools\Export\ClassMetadataExporter; +use Doctrine\ORM\Tools\ClassMetadataReader; class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase { @@ -88,9 +88,8 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase */ protected function extractClassMetadata($className) { - $cm = new ClassMetadataExporter(); - $cm->addMappingSource($this->_sm); - $exporter = $cm->getExporter('yaml'); + $cm = new ClassMetadataReader(); + $cm->addMappingSource(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm)); $metadatas = $cm->getMetadatas(); $output = false;