ClassMetadataExporter.php 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?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;

25
use Doctrine\ORM\Tools\ClassMetadataReader,
26
    Doctrine\ORM\Tools\Export\ExportException,
27
    Doctrine\ORM\EntityManager;
28 29

/**
30
 * Class used for converting your mapping information between the
31 32
 * supported formats: yaml, xml, and php/annotation.
 *
33 34 35
 *     [php]
 *     // Unify all your mapping information which is written in php, xml, yml
 *     // and convert it to a single set of yaml files.
36
 *
37
 *     $cme = new Doctrine\ORM\Tools\Export\ClassMetadataExporter();
38 39 40
 *     $cme->addMappingSource(__DIR__ . '/Entities');
 *     $cme->addMappingSource(__DIR__ . '/xml');
 *     $cme->addMappingSource(__DIR__ . '/yaml');
41
 *
42 43
 *     $exporter = $cme->getExporter('yaml');
 *     $exporter->setOutputDir(__DIR__ . '/new_yaml');
44
 *
45
 *     $exporter->setMetadatas($cme->getMetadatas());
46
 *     $exporter->export();
47 48 49 50 51 52 53 54 55
 *
 * @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
{
56
    private static $_exporterDrivers = array(
57 58 59 60 61 62 63
        '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'
    );

64 65 66 67
    public function __construct()
    {
        $this->_reader = new ClassMetadataReader();
    }
68

69
    /**
70
     * Register a new exporter driver class under a specified name
71
     *
72 73 74 75 76 77 78 79 80 81 82
     * @param string $name
     * @param string $class
     */
    public static function registerExportDriver($name, $class)
    {
        self::$_exporterDrivers[$name] = $class;
    }

    /**
     * Optionally set the EntityManager instance to get the AnnotationDriver
     * from instead of creating a new instance of the AnnotationDriver
83
     *
84
     * @param EntityManager $em
85 86
     * @return void
     */
87
    public function setEntityManager(EntityManager $em)
88
    {
89
        $this->_reader->setEntityManager($em);
90 91
    }

92
    /**
93
     * Get a exporter driver instance
94
     *
95 96 97
     * @param string $type   The type to get (yml, xml, etc.)
     * @param string $source    The directory where the exporter will export to
     * @return AbstractExporter $exporter
98
     */
99
    public function getExporter($type, $source = null)
100
    {
101 102
        if ( ! isset(self::$_exporterDrivers[$type])) {
            throw ExportException::invalidExporterDriverType($type);
103
        }
104

105
        $class = self::$_exporterDrivers[$type];
106

107
        return new $class($source);
108 109
    }

110
    /**
111 112 113 114 115 116 117
     * 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();
     *     $cme->addMappingSource(__DIR__ . '/yaml');
     *     $cme->addMappingSource($schemaManager);
118
     *
119 120 121
     * @param string $source   The source for the mapping files
     * @param string $type  The type of mapping files (yml, xml, etc.)
     * @return void
122
     */
123
    public function addMappingSource($source, $type = null)
124
    {
125
        $this->_reader->addMappingSource($source, $type);
126 127
    }

128 129 130 131 132 133 134
    /**
     * Get an array of ClassMetadataInfo instances for all the configured mapping
     * directories. Reads the mapping directories and populates ClassMetadataInfo
     * instances.
     *
     * @return array $classes
     */
135
    public function getMetadatas()
136
    {
137
        return $this->_reader->getMetadatas();
138 139
    }
}