diff --git a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php new file mode 100644 index 000000000..d1c5cf714 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php @@ -0,0 +1,162 @@ +. + */ + +namespace Doctrine\ORM\Tools\Export; + +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Doctrine\ORM\Mapping\ClassMetadata; + +/** + * Class used for converting your mapping information between the + * supported formats: yaml, xml, and php/annotation. + * + * [php] + * // Unify all your mapping information which is written in php, xml, yml + * // and convert it to a single set of yaml files. + * + * $cme = new Doctrine\ORM\Tools\Export\ClassMetadataExporter(); + * $cme->addMappingDir(__DIR__ . '/Entities', 'php'); + * $cme->addMappingDir(__DIR__ . '/xml', 'xml'); + * $cme->addMappingDir(__DIR__ . '/yaml', 'yaml'); + * + * $exporter = $cme->getExporter('yaml'); + * $exporter->setOutputDir(__DIR__ . '/new_yaml'); + * $exporter->export(); + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Jonathan Wage + */ +class ClassMetadataExporter +{ + private $_exporterDrivers = array( + 'xml' => 'Doctrine\ORM\Tools\Export\Driver\XmlExporter', + 'yaml' => 'Doctrine\ORM\Tools\Export\Driver\YamlExporter', + 'yml' => 'Doctrine\ORM\Tools\Export\Driver\YamlExporter', + 'php' => 'Doctrine\ORM\Tools\Export\Driver\PhpExporter', + 'annotation' => 'Doctrine\ORM\Tools\Export\Driver\AnnotationExporter' + ); + + private $_mappingDrivers = array( + 'annotation' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', + 'yaml' => 'Doctrine\ORM\Mapping\Driver\YamlDriver', + 'yml' => 'Doctrine\ORM\Mapping\Driver\YamlDriver', + 'xml' => 'Doctrine\ORM\Mapping\Driver\XmlDriver' + ); + + private $_mappingDirectories = array(); + + public function addMappingDir($dir, $type) + { + if ($type === 'php') { + $this->_mappingDirectories[] = array($dir, $type); + } else { + if ( ! isset($this->_mappingDrivers[$type])) { + throw DoctrineException::invalidMappingDriverType($type); + } + + $class = $this->_mappingDrivers[$type]; + if (is_subclass_of($class, 'Doctrine\ORM\Mapping\Driver\AbstractFileDriver')) { + $driver = new $class($dir, constant($class . '::PRELOAD')); + } else { + $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache); + $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); + $driver = new $class($reader); + } + $this->_mappingDirectories[] = array($dir, $driver); + } + } + + private function _getMetadataInstances() + { + $classes = array(); + + foreach ($this->_mappingDirectories as $d) { + list($dir, $driver) = $d; + if ($driver == 'php') { + $iter = new \FilesystemIterator($dir); + + foreach ($iter as $item) { + include $item->getPathName(); + $vars = get_defined_vars(); + foreach ($vars as $var) { + if ($var instanceof \Doctrine\ORM\Mapping\ClassMetadataInfo) { + $classes[] = $var; + } + } + } + $classes = array_unique($classes); + $classes = array_values($classes); + } else { + if ($driver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) { + $iter = new \FilesystemIterator($dir); + + $declared = get_declared_classes(); + foreach ($iter as $item) { + $baseName = $item->getBaseName(); + if ($baseName[0] == '.') { + continue; + } + require_once $item->getPathName(); + } + $declared = array_diff(get_declared_classes(), $declared); + + foreach ($declared as $className) { + if ( ! $driver->isTransient($className)) { + $metadata = new ClassMetadataInfo($className); + $driver->loadMetadataForClass($className, $metadata); + $classes[] = $metadata; + } + } + } else { + $preloadedClasses = $driver->preload(true); + foreach ($preloadedClasses as $className) { + $metadata = new ClassMetadataInfo($className); + $driver->loadMetadataForClass($className, $metadata); + $classes[] = $metadata; + } + } + } + } + + foreach ($classes as $key => $class) { + if ($class->isMappedSuperclass) { + unset($classes[$key]); + } + } + $classes = array_values($classes); + return $classes; + } + + public function getExporter($type, $dir = null) + { + if ( ! isset($this->_exporterDrivers[$type])) { + throw DoctrineException::invalidExporterDriverType($type); + } + + $class = $this->_exporterDrivers[$type]; + return new $class($this->_getMetadataInstances()); + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php b/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php new file mode 100644 index 000000000..100cd985c --- /dev/null +++ b/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php @@ -0,0 +1,13 @@ +fieldMappings as $fieldMapping): ?>hasProperty($fieldMapping['fieldName'], $metadata)) continue; ?> +getFieldMappingAnnotation($fieldMapping, $metadata)."\n" ?> +_numSpaces) ?>private $; + + +associationMappings as $associationMapping): ?>hasProperty($associationMapping->sourceFieldName, $metadata)) continue; ?> +getAssociationMappingAnnotation($associationMapping, $metadata)."\n" ?> +_numSpaces) ?>private $sourceFieldName ?>isManyToMany()): ?> = array() ; + + +getMethods($metadata) as $method): ?> + + \ No newline at end of file