Commit 115e4d01 authored by jwage's avatar jwage

[2.0] Refactoring the meta data drivers and refactoring yaml/xml and adding tests for yaml.

parent bdd2241f
<?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\Mapping\Driver;
use Doctrine\ORM\Mapping\MappingException;
/**
* SchemaMetadata mapping driver interface
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @version $Revision: 1393 $
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
abstract class AbstractFileDriver implements Driver
{
/**
* The FILE_PER_CLASS mode is an operating mode of the FileDriver where it loads
* the mapping files of individual classes on demand. This requires the user to
* adhere to the convention of 1 mapping file per class and the file names of
* the mapping files must correspond to the full class name, including namespace,
* with the namespace delimiters '\', replaced by dots '.'.
*
* Example:
* Class: My\Project\Model\User
* Mapping file: My.Project.Model.User.dcm.xml
*
* @var integer
*/
const FILE_PER_CLASS = 1;
/**
* The PRELOAD mode is an operating mode of the FileDriver where it loads
* all mapping files in advance. This is the default behavior. It does not
* require a naming convention or the convention of 1 class per mapping file.
*
* @var integer
*/
const PRELOAD = 2;
/**
* The paths where to look for mapping files.
*
* @var array
*/
protected $_paths;
/**
* The operating mode. Either FILE_PER_CLASS or PRELOAD.
*
* @var integer
*/
protected $_mode;
/**
* The file extension of mapping documents.
*
* @var string
*/
protected $_fileExtension;
/**
* Any preloaded elements.
*
* @var array
*/
protected $_elements = array();
/**
* Initializes a new FileDriver that looks in the given path(s) for mapping
* documents and operates in the specified operating mode.
*
* @param string|array $paths One or multiple paths where mapping documents can be found.
* @param integer $mode The operating mode. Either PRELOAD (default) or FILE_PER_CLASS.
*/
public function __construct($paths, $mode = self::PRELOAD)
{
$this->_paths = $paths;
$this->_mode = $mode;
}
/**
* Get the file extension used to look for mapping files under
*
* @return void
*/
public function getfileExtension()
{
return $this->_fileExtension;
}
/**
* Set the file extension used to look for mapping files under
*
* @param string $fileExtension The file extension to set
* @return void
*/
public function setFileExtension($fileExtension)
{
$this->_fileExtension = $fileExtension;
}
/**
* Get the element of schema meta data for the class from the mapping file.
* This will lazily load the mapping file if it is not loaded yet
*
* @return array $element The element of schema meta data
*/
public function getElement($className)
{
if (isset($this->_elements[$className])) {
$element = $this->_elements[$className];
unset($this->_elements[$className]);
return $element;
} else {
$result = $this->_loadMappingFile($this->_findMappingFile($className));
return $result[$className];
}
}
/**
* Gets any preloaded elements.
*
* @return array
*/
public function getPreloadedElements()
{
return $this->_elements;
}
/**
* Whether the class with the specified name should have its metadata loaded.
* This is only the case if it is either mapped as an Entity or a
* MappedSuperclass.
*
* @param string $className
* @return boolean
*/
public function isTransient($className)
{
$isTransient = true;
if ($this->_mode == self::FILE_PER_CLASS) {
// check whether file exists
foreach ((array)$this->_paths as $path) {
if (file_exists($path . DIRECTORY_SEPARATOR . str_replace('\\', '.', $className) . $this->_fileExtension)) {
$isTransient = false;
break;
}
}
} else {
$isTransient = isset($this->_elements[$className]);
}
return $isTransient;
}
/**
* Preloads all mapping information found in any documents within the
* configured paths and returns a list of class names that have been preloaded.
*
* @return array The list of class names that have been preloaded.
*/
public function preload()
{
if ($this->_mode != self::PRELOAD) {
return array();
}
foreach ((array)$this->_paths as $path) {
if (is_dir($path)) {
$files = glob($path . '/*' . $this->_fileExtension);
foreach ($files as $file) {
$this->_elements = array_merge($this->_elements, $this->_loadMappingFile($file));
}
} else if (is_file($path)) {
$this->_elements = array_merge($this->_elements, $this->_loadMappingFile($path));
}
}
return array_keys($this->_elements);
}
/**
* Finds the mapping file for the class with the given name by searching
* through the configured paths.
*
* @param $className
* @return string The (absolute) file name.
* @throws MappingException
*/
protected function _findMappingFile($className)
{
$fileName = null;
foreach ((array)$this->_paths as $path) {
$fileName = $path . DIRECTORY_SEPARATOR . str_replace('\\', '.', $className) . $this->_fileExtension;
if (file_exists($fileName)) {
break;
}
}
if ($fileName === null) {
throw MappingException::mappingFileNotFound($className);
}
return $fileName;
}
/**
* Loads a mapping file with the given name and returns a map
* from class/entity names to their corresponding elements.
*
* @param string $file The mapping file to load.
* @return array
*/
abstract protected function _loadMappingFile($file);
}
\ No newline at end of file
......@@ -38,7 +38,7 @@ require __DIR__ . '/DoctrineAnnotations.php';
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*/
class AnnotationDriver
class AnnotationDriver implements Driver
{
/**
* Loads the metadata for the specified class into the provided container.
......
<?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\Mapping\Driver;
use Doctrine\ORM\Mapping\ClassMetadata;
/**
* SchemaMetadata mapping driver interface
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @version $Revision: 1393 $
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
interface Driver
{
/**
* Loads the metadata for the specified class into the provided container.
*
* @param string $className
* @param ClassMetadata $metadata
*/
public function loadMetadataForClass($className, ClassMetadata $metadata);
/**
* Whether the class with the specified name should have its metadata loaded.
* This is only the case if it is either mapped as an Entity or a
* MappedSuperclass.
*
* @param string $className
* @return boolean
*/
public function isTransient($className);
}
\ No newline at end of file
......@@ -22,7 +22,6 @@
namespace Doctrine\ORM\Mapping\Driver;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
/**
* XmlDriver is a metadata driver that enables mapping through XML files.
......@@ -30,63 +29,9 @@ use Doctrine\ORM\Mapping\MappingException;
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*/
class XmlDriver
class XmlDriver extends AbstractFileDriver
{
/**
* The FILE_PER_CLASS mode is an operating mode of the XmlDriver where it loads
* the mapping files of individual classes on demand. This requires the user to
* adhere to the convention of 1 mapping file per class and the file names of
* the mapping files must correspond to the full class name, including namespace,
* with the namespace delimiters '\', replaced by dots '.'.
*
* Example:
* Class: My\Project\Model\User
* Mapping file: My.Project.Model.User.dcm.xml
*
* @var integer
*/
const FILE_PER_CLASS = 1;
/**
* The PRELOAD mode is an operating mode of the XmlDriver where it loads
* all mapping files in advance. This is the default behavior. It does not
* require a naming convention or the convention of 1 class per mapping file.
*
* @var integer
*/
const PRELOAD = 2;
/** The paths where to look for mapping files. */
private $_paths;
/** The operating mode. Either FILE_PER_CLASS or PRELOAD. */
private $_mode;
/** The file extension of mapping documents. */
private $_fileExtension = '.dcm.xml';
/** Any preloaded XML elements. */
private $_xmlElements = array();
/**
* Initializes a new XmlDriver that looks in the given path(s) for mapping
* documents and operates in the specified operating mode.
*
* @param string|array $paths One or multiple paths where mapping documents can be found.
* @param integer $mode The operating mode. Either PRELOAD (default) or FILE_PER_CLASS.
*/
public function __construct($paths, $mode = self::PRELOAD)
{
$this->_paths = $paths;
$this->_mode = $mode;
}
/**
* Gets any preloaded XML documents.
*
* @return array
*/
public function getPreloadedXmlElements()
{
return $this->_xmlElements;
}
protected $_fileExtension = '.dcm.xml';
/**
* Loads the metadata for the specified class into the provided container.
......@@ -96,13 +41,7 @@ class XmlDriver
*/
public function loadMetadataForClass($className, ClassMetadata $metadata)
{
if (isset($this->_xmlElements[$className])) {
$xmlRoot = $this->_xmlElements[$className];
unset($this->_xmlElements[$className]);
} else {
$result = $this->_loadMappingFile($this->_findMappingFile($className));
$xmlRoot = $result[$className];
}
$xmlRoot = $this->getElement($className);
if ($xmlRoot->getName() == 'entity') {
......@@ -134,7 +73,6 @@ class XmlDriver
}
}
// Evaluate <id ...> mappings
foreach ($xmlRoot->id as $idElement) {
$mapping = array(
......@@ -263,59 +201,6 @@ class XmlDriver
} else if ($xmlRoot->getName() == 'mapped-superclass') {
throw MappingException::notImplemented('Mapped superclasses are not yet supported.');
}
}
/**
* Whether the class with the specified name should have its metadata loaded.
* This is only the case if it is either mapped as an Entity or a
* MappedSuperclass.
*
* @param string $className
* @return boolean
*/
public function isTransient($className)
{
$isTransient = true;
if ($this->_mode == self::FILE_PER_CLASS) {
// check whether file exists
foreach ((array)$this->_paths as $path) {
if (file_exists($path . DIRECTORY_SEPARATOR . str_replace('\\', '.', $className) . $this->_fileExtension)) {
$isTransient = false;
break;
}
}
} else {
$isTransient = isset($this->_xmlElements[$className]);
}
return $isTransient;
}
/**
* Preloads all mapping information found in any documents within the
* configured paths and returns a list of class names that have been preloaded.
*
* @return array The list of class names that have been preloaded.
*/
public function preload()
{
if ($this->_mode != self::PRELOAD) {
return array();
}
foreach ((array)$this->_paths as $path) {
if (is_dir($path)) {
$files = glob($path . '/*' . $this->_fileExtension);
foreach ($files as $file) {
$this->_xmlElements = array_merge($this->_xmlElements, $this->_loadMappingFile($file));
}
} else if (is_file($path)) {
$this->_xmlElements = array_merge($this->_xmlElements, $this->_loadMappingFile($path));
}
}
return array_keys($this->_xmlElements);
}
/**
......@@ -325,7 +210,7 @@ class XmlDriver
* @param string $file The mapping file to load.
* @return array
*/
private function _loadMappingFile($file)
protected function _loadMappingFile($file)
{
$result = array();
$xmlElement = simplexml_load_file($file);
......@@ -345,31 +230,6 @@ class XmlDriver
return $result;
}
/**
* Finds the mapping file for the class with the given name by searching
* through the configured paths.
*
* @param $className
* @return string The (absolute) file name.
* @throws MappingException
*/
private function _findMappingFile($className)
{
$fileName = null;
foreach ((array)$this->_paths as $path) {
$fileName = $path . DIRECTORY_SEPARATOR . str_replace('\\', '.', $className) . $this->_fileExtension;
if (file_exists($fileName)) {
break;
}
}
if ($fileName === null) {
throw MappingException::mappingFileNotFound($className);
}
return $fileName;
}
/**
* Constructs a joinColumn mapping array based on the information
* found in the given SimpleXMLElement.
......@@ -423,5 +283,4 @@ class XmlDriver
return $cascades;
}
}
}
\ No newline at end of file
......@@ -21,7 +21,8 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\XmlDriverTest');
//$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\YamlDriverTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest');
return $suite;
}
......
......@@ -75,3 +75,10 @@ class ClassMetadataFactoryTestSubject extends \Doctrine\ORM\Mapping\ClassMetadat
return $this->_requestedClasses;
}
}
class TestEntity1
{
private $id;
private $name;
private $other;
}
\ No newline at end of file
......@@ -46,7 +46,6 @@ class XmlDriverTest extends \Doctrine\Tests\OrmTestCase
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
$this->assertTrue(isset($class->associationMappings['groups']));
$this->assertTrue($class->associationMappings['groups']->isOwningSide);
}
public function testPreloadMode()
......@@ -58,10 +57,10 @@ class XmlDriverTest extends \Doctrine\Tests\OrmTestCase
$classNames = $xmlDriver->preload();
$this->assertEquals($className, $classNames[0]);
$this->assertEquals(1, count($xmlDriver->getPreloadedXmlElements()));
$this->assertEquals(1, count($xmlDriver->getPreloadedElements()));
$xmlDriver->loadMetadataForClass($className, $class);
$this->assertEquals(0, count($xmlDriver->getPreloadedXmlElements()));
$this->assertEquals(0, count($xmlDriver->getPreloadedElements()));
}
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
require_once __DIR__ . '/yaml/User.php';
require_once __DIR__ . '/../../TestInit.php';
class YamlDriverTest extends \Doctrine\Tests\OrmTestCase
{
public function testYamlMapping()
{
$className = 'YamlMappingTest\User';
$yamlDriver = new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml');
$class = new ClassMetadata($className);
$this->assertFalse($yamlDriver->isTransient($className));
$yamlDriver->loadMetadataForClass($className, $class);
$this->assertEquals('cms_users', $class->getTableName());
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $class->getInheritanceType());
$this->assertEquals(2, count($class->fieldMappings));
$this->assertTrue(isset($class->fieldMappings['id']));
$this->assertTrue(isset($class->fieldMappings['name']));
$this->assertEquals('string', $class->fieldMappings['name']['type']);
$this->assertEquals(array('id'), $class->identifier);
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $class->getIdGeneratorType());
$this->assertEquals(3, count($class->associationMappings));
$this->assertEquals(1, count($class->inverseMappings));
$this->assertTrue($class->associationMappings['address'] instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
$this->assertTrue(isset($class->associationMappings['address']));
$this->assertTrue($class->associationMappings['address']->isOwningSide);
$this->assertTrue($class->associationMappings['phonenumbers'] instanceof \Doctrine\ORM\Mapping\OneToManyMapping);
$this->assertTrue(isset($class->associationMappings['phonenumbers']));
$this->assertFalse($class->associationMappings['phonenumbers']->isOwningSide);
$this->assertTrue($class->associationMappings['phonenumbers']->isInverseSide());
$this->assertTrue($class->associationMappings['phonenumbers']->isCascadeSave);
$this->assertTrue($class->associationMappings['groups'] instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
$this->assertTrue(isset($class->associationMappings['groups']));
$this->assertTrue($class->associationMappings['groups']->isOwningSide);
}
public function testPreloadMode()
{
$className = 'YamlMappingTest\User';
$yamlDriver = new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml');
$class = new ClassMetadata($className);
$classNames = $yamlDriver->preload();
$this->assertEquals($className, $classNames[0]);
$this->assertEquals(1, count($yamlDriver->getPreloadedElements()));
$yamlDriver->loadMetadataForClass($className, $class);
$this->assertEquals(0, count($yamlDriver->getPreloadedElements()));
}
}
\ No newline at end of file
......@@ -10,4 +10,4 @@ class User {
private $groups;
// ... rest of code omitted, irrelevant for the mapping test
}
}
\ No newline at end of file
......@@ -37,4 +37,4 @@
</entity>
</doctrine-mapping>
</doctrine-mapping>
\ No newline at end of file
<?php
namespace YamlMappingTest;
class User {
private $id;
private $name;
private $address;
private $phonenumbers;
private $groups;
// ... rest of code omitted, irrelevant for the mapping test
}
\ No newline at end of file
YamlMappingTest\User:
type: entity
table: cms_users
id:
id:
type: integer
generator:
strategy: auto
fields:
name:
type: string
length: 50
oneToOne:
address:
targetEntity: Address
joinColumn:
name: address_id
referencedColumnName: id
oneToMany:
phonenumbers:
targetEntity: Phonenumber
mappedBy: user
cascade: cascadeSave
manyToMany:
groups:
targetEntity: Group
joinTable:
name: cms_users_groups
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
group_id:
referencedColumnName: id
\ No newline at end of file
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