Commit f3c672a2 authored by Roman S. Borschel's avatar Roman S. Borschel

Merged from upstream/master.

parents 10acab65 d6565667
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -30,10 +28,7 @@ use Doctrine\ORM\ORMException, ...@@ -30,10 +28,7 @@ use Doctrine\ORM\ORMException,
* metadata mapping informations of a class which describes how a class should be mapped * metadata mapping informations of a class which describes how a class should be mapped
* to a relational database. * to a relational database.
* *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
...@@ -191,6 +186,24 @@ class ClassMetadataFactory ...@@ -191,6 +186,24 @@ class ClassMetadataFactory
$this->_loadedMetadata[$className] = $class; $this->_loadedMetadata[$className] = $class;
} }
/**
* Get array of parent classes for the given entity class
*
* @param string $name
* @return array $parentClasses
*/
protected function _getParentClasses($name)
{
// Collect parent classes, ignoring transient (not-mapped) classes.
$parentClasses = array();
foreach (array_reverse(class_parents($name)) as $parentClass) {
if ( ! $this->_driver->isTransient($parentClass)) {
$parentClasses[] = $parentClass;
}
}
return $parentClasses;
}
/** /**
* Loads the metadata of the class in question and all it's ancestors whose metadata * Loads the metadata of the class in question and all it's ancestors whose metadata
* is still not loaded. * is still not loaded.
...@@ -206,13 +219,7 @@ class ClassMetadataFactory ...@@ -206,13 +219,7 @@ class ClassMetadataFactory
$loaded = array(); $loaded = array();
// Collect parent classes, ignoring transient (not-mapped) classes. $parentClasses = $this->_getParentClasses($name);
$parentClasses = array();
foreach (array_reverse(class_parents($name)) as $parentClass) {
if ( ! $this->_driver->isTransient($parentClass)) {
$parentClasses[] = $parentClass;
}
}
$parentClasses[] = $name; $parentClasses[] = $name;
// Move down the hierarchy of parent classes, starting from the topmost class // Move down the hierarchy of parent classes, starting from the topmost class
...@@ -353,7 +360,7 @@ class ClassMetadataFactory ...@@ -353,7 +360,7 @@ class ClassMetadataFactory
* *
* @param Doctrine\ORM\Mapping\ClassMetadata $class * @param Doctrine\ORM\Mapping\ClassMetadata $class
*/ */
private function _completeIdGeneratorMapping(ClassMetadata $class) private function _completeIdGeneratorMapping(ClassMetadataInfo $class)
{ {
$idGenType = $class->generatorType; $idGenType = $class->generatorType;
if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) { if ($idGenType == ClassMetadata::GENERATOR_TYPE_AUTO) {
......
This diff is collapsed.
...@@ -25,7 +25,9 @@ use Symfony\Components\Console\Input\InputArgument, ...@@ -25,7 +25,9 @@ use Symfony\Components\Console\Input\InputArgument,
Symfony\Components\Console\Input\InputOption, Symfony\Components\Console\Input\InputOption,
Symfony\Components\Console, Symfony\Components\Console,
Doctrine\ORM\Tools\Console\MetadataFilter, Doctrine\ORM\Tools\Console\MetadataFilter,
Doctrine\ORM\Tools\Export\ClassMetadataExporter; Doctrine\ORM\Tools\Export\ClassMetadataExporter,
Doctrine\ORM\Tools\EntityGenerator,
Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
/** /**
* Command to convert your mapping information between the various formats. * Command to convert your mapping information between the various formats.
...@@ -94,7 +96,8 @@ EOT ...@@ -94,7 +96,8 @@ EOT
); );
} }
$metadatas = $em->getMetadataFactory()->getAllMetadata(); $cmf = new DisconnectedClassMetadataFactory($em);
$metadatas = $cmf->getAllMetadata();
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
// Process destination directory // Process destination directory
......
...@@ -25,7 +25,8 @@ use Symfony\Components\Console\Input\InputArgument, ...@@ -25,7 +25,8 @@ use Symfony\Components\Console\Input\InputArgument,
Symfony\Components\Console\Input\InputOption, Symfony\Components\Console\Input\InputOption,
Symfony\Components\Console, Symfony\Components\Console,
Doctrine\ORM\Tools\Console\MetadataFilter, Doctrine\ORM\Tools\Console\MetadataFilter,
Doctrine\ORM\Tools\EntityGenerator; Doctrine\ORM\Tools\EntityGenerator,
Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
/** /**
* Command to generate entity classes and method stubs from your mapping information. * Command to generate entity classes and method stubs from your mapping information.
...@@ -95,7 +96,8 @@ EOT ...@@ -95,7 +96,8 @@ EOT
{ {
$em = $this->getHelper('em')->getEntityManager(); $em = $this->getHelper('em')->getEntityManager();
$metadatas = $em->getMetadataFactory()->getAllMetadata(); $cmf = new DisconnectedClassMetadataFactory($em);
$metadatas = $cmf->getAllMetadata();
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
// Process destination directory // Process destination directory
......
...@@ -62,7 +62,7 @@ class ConvertDoctrine1Schema ...@@ -62,7 +62,7 @@ class ConvertDoctrine1Schema
* *
* @return array $metadatas An array of ClassMetadataInfo instances * @return array $metadatas An array of ClassMetadataInfo instances
*/ */
public function getMetadatas() public function getMetadata()
{ {
$schema = array(); $schema = array();
foreach ($this->_from as $path) { foreach ($this->_from as $path) {
......
<?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;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
/**
* The DisconnectedClassMetadataFactory is used to create ClassMetadataInfo objects
* that do not require the entity class actually exist. This allows us to
* load some mapping information and use it to do things like generate code
* from the mapping information.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class DisconnectedClassMetadataFactory extends ClassMetadataFactory
{
/**
* @override
*/
protected function _newClassMetadataInstance($className)
{
return new ClassMetadataInfo($className);
}
/**
* @override
*/
protected function _getParentClasses($name)
{
return array();
}
}
\ No newline at end of file
...@@ -22,29 +22,13 @@ ...@@ -22,29 +22,13 @@
namespace Doctrine\ORM\Tools\Export; namespace Doctrine\ORM\Tools\Export;
use Doctrine\ORM\Tools\ClassMetadataReader, use Doctrine\ORM\Tools\Export\ExportException,
Doctrine\ORM\Tools\Export\ExportException,
Doctrine\ORM\EntityManager; Doctrine\ORM\EntityManager;
/** /**
* Class used for converting your mapping information between the * Class used for converting your mapping information between the
* supported formats: yaml, xml, and php/annotation. * 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->addMappingSource(__DIR__ . '/Entities');
* $cme->addMappingSource(__DIR__ . '/xml');
* $cme->addMappingSource(__DIR__ . '/yaml');
*
* $exporter = $cme->getExporter('yaml');
* $exporter->setOutputDir(__DIR__ . '/new_yaml');
*
* $exporter->setMetadatas($cme->getMetadatas());
* $exporter->export();
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
...@@ -61,11 +45,6 @@ class ClassMetadataExporter ...@@ -61,11 +45,6 @@ class ClassMetadataExporter
'annotation' => 'Doctrine\ORM\Tools\Export\Driver\AnnotationExporter' 'annotation' => 'Doctrine\ORM\Tools\Export\Driver\AnnotationExporter'
); );
public function __construct()
{
$this->_reader = new ClassMetadataReader();
}
/** /**
* Register a new exporter driver class under a specified name * Register a new exporter driver class under a specified name
* *
...@@ -77,18 +56,6 @@ class ClassMetadataExporter ...@@ -77,18 +56,6 @@ class ClassMetadataExporter
self::$_exporterDrivers[$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
*
* @param EntityManager $em
* @return void
*/
public function setEntityManager(EntityManager $em)
{
$this->_reader->setEntityManager($em);
}
/** /**
* Get a exporter driver instance * Get a exporter driver instance
* *
...@@ -96,7 +63,7 @@ class ClassMetadataExporter ...@@ -96,7 +63,7 @@ class ClassMetadataExporter
* @param string $source The directory where the exporter will export to * @param string $source The directory where the exporter will export to
* @return AbstractExporter $exporter * @return AbstractExporter $exporter
*/ */
public function getExporter($type, $source = null) public function getExporter($type, $dest)
{ {
if ( ! isset(self::$_exporterDrivers[$type])) { if ( ! isset(self::$_exporterDrivers[$type])) {
throw ExportException::invalidExporterDriverType($type); throw ExportException::invalidExporterDriverType($type);
...@@ -104,36 +71,6 @@ class ClassMetadataExporter ...@@ -104,36 +71,6 @@ class ClassMetadataExporter
$class = self::$_exporterDrivers[$type]; $class = self::$_exporterDrivers[$type];
return new $class($source); return new $class($dest);
}
/**
* 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);
*
* @param string $source The source for the mapping files
* @param string $type The type of mapping files (yml, xml, etc.)
* @return void
*/
public function addMappingSource($source, $type = null)
{
$this->_reader->addMappingSource($source, $type);
}
/**
* Get an array of ClassMetadataInfo instances for all the configured mapping
* directories. Reads the mapping directories and populates ClassMetadataInfo
* instances.
*
* @return array $classes
*/
public function getMetadatas()
{
return $this->_reader->getMetadatas();
} }
} }
\ No newline at end of file
...@@ -36,7 +36,7 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo; ...@@ -36,7 +36,7 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo;
*/ */
abstract class AbstractExporter abstract class AbstractExporter
{ {
protected $_metadatas = array(); protected $_metadata = array();
protected $_outputDir; protected $_outputDir;
protected $_extension; protected $_extension;
...@@ -57,12 +57,12 @@ abstract class AbstractExporter ...@@ -57,12 +57,12 @@ abstract class AbstractExporter
/** /**
* Set the array of ClassMetadataInfo instances to export * Set the array of ClassMetadataInfo instances to export
* *
* @param array $metadatas * @param array $metadata
* @return void * @return void
*/ */
public function setMetadatas(array $metadatas) public function setMetadata(array $metadata)
{ {
$this->_metadatas = $metadatas; $this->_metadata = $metadata;
} }
/** /**
...@@ -79,7 +79,7 @@ abstract class AbstractExporter ...@@ -79,7 +79,7 @@ abstract class AbstractExporter
* Set the directory to output the mapping files to * Set the directory to output the mapping files to
* *
* [php] * [php]
* $exporter = new YamlExporter($metadatas); * $exporter = new YamlExporter($metadata);
* $exporter->setOutputDir(__DIR__ . '/yaml'); * $exporter->setOutputDir(__DIR__ . '/yaml');
* $exporter->export(); * $exporter->export();
* *
...@@ -103,7 +103,7 @@ abstract class AbstractExporter ...@@ -103,7 +103,7 @@ abstract class AbstractExporter
mkdir($this->_outputDir, 0777, true); mkdir($this->_outputDir, 0777, true);
} }
foreach ($this->_metadatas as $metadata) { foreach ($this->_metadata as $metadata) {
$output = $this->exportClassMetadata($metadata); $output = $this->exportClassMetadata($metadata);
$path = $this->_generateOutputPath($metadata); $path = $this->_generateOutputPath($metadata);
$dir = dirname($path); $dir = dirname($path);
...@@ -129,7 +129,7 @@ abstract class AbstractExporter ...@@ -129,7 +129,7 @@ abstract class AbstractExporter
* Set the directory to output the mapping files to * Set the directory to output the mapping files to
* *
* [php] * [php]
* $exporter = new YamlExporter($metadatas, __DIR__ . '/yaml'); * $exporter = new YamlExporter($metadata, __DIR__ . '/yaml');
* $exporter->setExtension('.yml'); * $exporter->setExtension('.yml');
* $exporter->export(); * $exporter->export();
* *
......
...@@ -4,7 +4,7 @@ namespace Doctrine\Tests\ORM\Functional; ...@@ -4,7 +4,7 @@ namespace Doctrine\Tests\ORM\Functional;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
use Doctrine\ORM\Tools\ClassMetadataReader; use Doctrine\ORM\Mapping\ClassMetadataInfo;
class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
{ {
...@@ -80,7 +80,6 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -80,7 +80,6 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']); $this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']);
} }
/** /**
* *
* @param string $className * @param string $className
...@@ -88,15 +87,12 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -88,15 +87,12 @@ class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
*/ */
protected function extractClassMetadata($className) protected function extractClassMetadata($className)
{ {
$cm = new ClassMetadataReader(); $driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm);
$cm->addMappingSource(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($this->_sm)); foreach ($driver->getAllClassNames() as $dbClassName) {
$metadatas = $cm->getMetadatas(); $class = new ClassMetadataInfo($dbClassName);
$driver->loadMetadataForClass($dbClassName, $class);
$output = false; if (strtolower($class->name) == strtolower($className)) {
return $class;
foreach ($metadatas AS $metadata) {
if (strtolower($metadata->name) == strtolower($className)) {
return $metadata;
} }
} }
......
...@@ -21,8 +21,15 @@ ...@@ -21,8 +21,15 @@
namespace Doctrine\Tests\ORM\Tools; namespace Doctrine\Tests\ORM\Tools;
use Doctrine\ORM\Tools\Export\ClassMetadataExporter, use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
Doctrine\ORM\Tools\ConvertDoctrine1Schema; use Doctrine\ORM\Tools\ConvertDoctrine1Schema;
use Doctrine\Tests\Mocks\MetadataDriverMock;
use Doctrine\Tests\Mocks\DatabasePlatformMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
use Doctrine\Common\EventManager;
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
require_once __DIR__ . '/../../TestInit.php'; require_once __DIR__ . '/../../TestInit.php';
...@@ -38,9 +45,23 @@ require_once __DIR__ . '/../../TestInit.php'; ...@@ -38,9 +45,23 @@ require_once __DIR__ . '/../../TestInit.php';
*/ */
class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
{ {
protected function _createEntityManager($metadataDriver)
{
$driverMock = new DriverMock();
$config = new \Doctrine\ORM\Configuration();
$config->setProxyDir(__DIR__ . '/../../Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$eventManager = new EventManager();
$conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
$mockDriver = new MetadataDriverMock();
$config->setMetadataDriverImpl($metadataDriver);
return EntityManagerMock::create($conn, $config, $eventManager);
}
public function testTest() public function testTest()
{ {
if (!class_exists('Symfony\Components\Yaml\Yaml', true)) { if ( ! class_exists('Symfony\Components\Yaml\Yaml', true)) {
$this->markTestSkipped('Please install Symfony YAML Component into the include path of your PHP installation.'); $this->markTestSkipped('Please install Symfony YAML Component into the include path of your PHP installation.');
} }
...@@ -48,28 +69,32 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase ...@@ -48,28 +69,32 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema'); $converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema');
$exporter = $cme->getExporter('yml', __DIR__ . '/convert'); $exporter = $cme->getExporter('yml', __DIR__ . '/convert');
$exporter->setMetadatas($converter->getMetadatas()); $exporter->setMetadata($converter->getMetadata());
$exporter->export(); $exporter->export();
$this->assertTrue(file_exists(__DIR__ . '/convert/User.dcm.yml')); $this->assertTrue(file_exists(__DIR__ . '/convert/User.dcm.yml'));
$this->assertTrue(file_exists(__DIR__ . '/convert/Profile.dcm.yml')); $this->assertTrue(file_exists(__DIR__ . '/convert/Profile.dcm.yml'));
$cme->addMappingSource(__DIR__ . '/convert'); $metadataDriver = new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/convert');
$metadatas = $cme->getMetadatas(); $em = $this->_createEntityManager($metadataDriver);
$cmf = new DisconnectedClassMetadataFactory($em);
$metadata = $cmf->getAllMetadata();
$profileClass = $metadata[0];
$userClass = $metadata[1];
$this->assertEquals(2, count($metadatas)); $this->assertEquals(2, count($metadata));
$this->assertEquals('Profile', $metadatas['Profile']->name); $this->assertEquals('Profile', $profileClass->name);
$this->assertEquals('User', $metadatas['User']->name); $this->assertEquals('User', $userClass->name);
$this->assertEquals(4, count($metadatas['Profile']->fieldMappings)); $this->assertEquals(4, count($profileClass->fieldMappings));
$this->assertEquals(5, count($metadatas['User']->fieldMappings)); $this->assertEquals(5, count($userClass->fieldMappings));
$this->assertEquals('text', $metadatas['User']->fieldMappings['clob']['type']); $this->assertEquals('text', $userClass->fieldMappings['clob']['type']);
$this->assertEquals('test_alias', $metadatas['User']->fieldMappings['theAlias']['columnName']); $this->assertEquals('test_alias', $userClass->fieldMappings['theAlias']['columnName']);
$this->assertEquals('theAlias', $metadatas['User']->fieldMappings['theAlias']['fieldName']); $this->assertEquals('theAlias', $userClass->fieldMappings['theAlias']['fieldName']);
$this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName); $this->assertEquals('Profile', $profileClass->associationMappings['User']->sourceEntityName);
$this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName); $this->assertEquals('User', $profileClass->associationMappings['User']->targetEntityName);
$this->assertEquals('username', $metadatas['User']->table['uniqueConstraints']['username']['columns'][0]); $this->assertEquals('username', $userClass->table['uniqueConstraints']['username']['columns'][0]);
unlink(__DIR__ . '/convert/User.dcm.yml'); unlink(__DIR__ . '/convert/User.dcm.yml');
unlink(__DIR__ . '/convert/Profile.dcm.yml'); unlink(__DIR__ . '/convert/Profile.dcm.yml');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment