2009-10-05 08:29:26 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools\Export;
|
|
|
|
|
2009-10-08 22:55:02 +04:00
|
|
|
use Doctrine\ORM\EntityManager,
|
|
|
|
Doctrine\ORM\Mapping\ClassMetadataInfo,
|
|
|
|
Doctrine\ORM\Mapping\ClassMetadata,
|
|
|
|
Doctrine\Common\DoctrineException;
|
2009-10-05 08:29:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used for converting your mapping information between the
|
|
|
|
* supported formats: yaml, xml, and php/annotation.
|
|
|
|
*
|
2009-10-07 02:38:34 +04:00
|
|
|
* [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();
|
2009-10-07 08:07:23 +04:00
|
|
|
* $cme->addMappingSource(__DIR__ . '/Entities', 'php');
|
|
|
|
* $cme->addMappingSource(__DIR__ . '/xml', 'xml');
|
|
|
|
* $cme->addMappingSource(__DIR__ . '/yaml', 'yaml');
|
2009-10-07 02:38:34 +04:00
|
|
|
*
|
|
|
|
* $exporter = $cme->getExporter('yaml');
|
|
|
|
* $exporter->setOutputDir(__DIR__ . '/new_yaml');
|
2009-10-05 08:29:26 +04:00
|
|
|
*
|
2009-10-07 08:07:23 +04:00
|
|
|
* $exporter->setMetadatas($cme->getMetadatasForMappingSources());
|
2009-10-07 02:38:34 +04:00
|
|
|
* $exporter->export();
|
2009-10-05 08:29:26 +04:00
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
*/
|
|
|
|
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',
|
2009-10-07 08:07:23 +04:00
|
|
|
'xml' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
|
2009-10-08 02:14:13 +04:00
|
|
|
'php' => 'Doctrine\ORM\Mapping\Driver\PhpDriver',
|
2009-10-07 08:07:23 +04:00
|
|
|
'database' => 'Doctrine\ORM\Mapping\Driver\DatabaseDriver'
|
2009-10-05 08:29:26 +04:00
|
|
|
);
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
private $_mappingSources = array();
|
2009-10-05 08:29:26 +04:00
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
/**
|
|
|
|
* Add a new mapping directory to the array of directories to convert and export
|
|
|
|
* to another format
|
|
|
|
*
|
|
|
|
* [php]
|
|
|
|
* $cme = new Doctrine\ORM\Tools\Export\ClassMetadataExporter();
|
2009-10-07 08:07:23 +04:00
|
|
|
* $cme->addMappingSource(__DIR__ . '/yaml', 'yaml');
|
|
|
|
* $cme->addMappingSource($schemaManager, 'database');
|
2009-10-07 02:38:34 +04:00
|
|
|
*
|
2009-10-07 08:07:23 +04:00
|
|
|
* @param string $source The source for the mapping
|
2009-10-07 02:38:34 +04:00
|
|
|
* @param string $type The type of mapping files (yml, xml, etc.)
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-10-07 08:07:23 +04:00
|
|
|
public function addMappingSource($source, $type)
|
2009-10-05 08:29:26 +04:00
|
|
|
{
|
2009-10-08 02:14:13 +04:00
|
|
|
if ( ! isset($this->_mappingDrivers[$type])) {
|
|
|
|
throw DoctrineException::invalidMappingDriverType($type);
|
2009-10-05 08:29:26 +04:00
|
|
|
}
|
2009-10-08 02:14:13 +04:00
|
|
|
|
|
|
|
$driver = $this->getMappingDriver($type, $source);
|
|
|
|
$this->_mappingSources[] = array($source, $driver);
|
2009-10-05 08:29:26 +04:00
|
|
|
}
|
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
/**
|
|
|
|
* Get an instance of a mapping driver
|
|
|
|
*
|
|
|
|
* @param string $type The type of mapping driver (yaml, xml, annotation, etc.)
|
2009-10-07 08:07:23 +04:00
|
|
|
* @param string $source The source for the driver
|
2009-10-07 02:38:34 +04:00
|
|
|
* @return AbstractDriver $driver
|
|
|
|
*/
|
2009-10-07 08:07:23 +04:00
|
|
|
public function getMappingDriver($type, $source = null)
|
2009-10-06 02:09:29 +04:00
|
|
|
{
|
|
|
|
if ( ! isset($this->_mappingDrivers[$type])) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-28 15:46:12 +03:00
|
|
|
|
2009-10-06 02:09:29 +04:00
|
|
|
$class = $this->_mappingDrivers[$type];
|
2010-01-28 15:46:12 +03:00
|
|
|
|
|
|
|
if (is_subclass_of($class, 'Doctrine\ORM\Mapping\Driver\AbstractDriver')) {
|
2009-10-07 08:07:23 +04:00
|
|
|
if (is_null($source)) {
|
2009-10-07 02:38:34 +04:00
|
|
|
throw DoctrineException::fileMappingDriversRequireDirectoryPath();
|
|
|
|
}
|
2010-01-28 15:46:12 +03:00
|
|
|
|
|
|
|
if ($class == 'Doctrine\ORM\Mapping\Driver\AnnotationDriver') {
|
|
|
|
$reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
|
|
|
|
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
|
|
|
|
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
|
|
|
|
} else {
|
|
|
|
$driver = new $class();
|
|
|
|
}
|
|
|
|
|
|
|
|
$driver->addPaths((array) $source);
|
2009-10-08 02:14:13 +04:00
|
|
|
} else {
|
|
|
|
$driver = new $class($source);
|
2009-10-06 02:09:29 +04:00
|
|
|
}
|
2010-01-28 15:46:12 +03:00
|
|
|
|
2009-10-06 02:09:29 +04:00
|
|
|
return $driver;
|
|
|
|
}
|
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
/**
|
|
|
|
* Get the array of added mapping directories
|
|
|
|
*
|
|
|
|
* @return array $mappingDirectories
|
|
|
|
*/
|
2009-10-07 08:07:23 +04:00
|
|
|
public function getMappingSources()
|
2009-10-06 02:09:29 +04:00
|
|
|
{
|
2009-10-07 08:07:23 +04:00
|
|
|
return $this->_mappingSources;
|
2009-10-06 02:09:29 +04:00
|
|
|
}
|
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
/**
|
|
|
|
* Get an array of ClassMetadataInfo instances for all the configured mapping
|
|
|
|
* directories. Reads the mapping directories and populates ClassMetadataInfo
|
|
|
|
* instances.
|
|
|
|
*
|
|
|
|
* @return array $classes
|
|
|
|
*/
|
2009-10-07 08:07:23 +04:00
|
|
|
public function getMetadatasForMappingSources()
|
2009-10-05 08:29:26 +04:00
|
|
|
{
|
|
|
|
$classes = array();
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
foreach ($this->_mappingSources as $d) {
|
|
|
|
list($source, $driver) = $d;
|
2009-10-08 02:14:13 +04:00
|
|
|
|
2009-12-16 00:06:32 +03:00
|
|
|
$allClasses = $driver->getAllClassNames();
|
2010-01-28 15:46:12 +03:00
|
|
|
|
2009-12-16 00:06:32 +03:00
|
|
|
foreach ($allClasses as $className) {
|
2009-10-08 02:14:13 +04:00
|
|
|
if (class_exists($className, false)) {
|
|
|
|
$metadata = new ClassMetadata($className);
|
2009-10-05 08:29:26 +04:00
|
|
|
} else {
|
2009-10-08 02:14:13 +04:00
|
|
|
$metadata = new ClassMetadataInfo($className);
|
|
|
|
}
|
2010-01-28 15:46:12 +03:00
|
|
|
|
2009-10-08 02:14:13 +04:00
|
|
|
$driver->loadMetadataForClass($className, $metadata);
|
2010-01-28 15:46:12 +03:00
|
|
|
|
2009-10-08 02:14:13 +04:00
|
|
|
if ( ! $metadata->isMappedSuperclass) {
|
|
|
|
$classes[$metadata->name] = $metadata;
|
2009-10-05 08:29:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
2009-10-07 02:38:34 +04:00
|
|
|
/**
|
|
|
|
* Get a exporter driver instance
|
|
|
|
*
|
|
|
|
* @param string $type The type to get (yml, xml, etc.)
|
2009-10-07 08:07:23 +04:00
|
|
|
* @param string $source The directory where the exporter will export to
|
2009-10-07 02:38:34 +04:00
|
|
|
* @return AbstractExporter $exporter
|
|
|
|
*/
|
2009-10-07 08:07:23 +04:00
|
|
|
public function getExporter($type, $source = null)
|
2009-10-05 08:29:26 +04:00
|
|
|
{
|
|
|
|
if ( ! isset($this->_exporterDrivers[$type])) {
|
|
|
|
throw DoctrineException::invalidExporterDriverType($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = $this->_exporterDrivers[$type];
|
2010-01-28 15:46:12 +03:00
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
return new $class($source);
|
2009-10-05 08:29:26 +04:00
|
|
|
}
|
|
|
|
}
|