Commit 443a2056 authored by guilhermeblanco's avatar guilhermeblanco

[2.0][DDC-281] Fixes for Mapping drivers

parent 14ec40e1
<?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;
/**
* Base driver for metadata drivers.
*
* A file driver operates in a mode 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 '.'.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
abstract class AbstractDriver
{
/**
* The paths where to look for mapping files.
*
* @var array
*/
protected $_paths = array();
/**
* The file extension of mapping documents.
*
* @var string
*/
protected $_fileExtension = 'php';
/**
* Append lookup paths to metadata driver.
*
* @param array $paths
*/
public function addPaths(array $paths)
{
$this->_paths = array_unique(array_merge($this->_paths, $paths));
}
/**
* Retrieve the defined metadata lookup paths.
*
* @return array
*/
public function getPaths()
{
return $this->_paths;
}
/**
* 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;
}
}
\ No newline at end of file
...@@ -40,12 +40,73 @@ use Doctrine\ORM\Mapping\MappingException; ...@@ -40,12 +40,73 @@ use Doctrine\ORM\Mapping\MappingException;
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
abstract class AbstractFileDriver extends AbstractDriver implements Driver abstract class AbstractFileDriver implements Driver
{ {
/** /**
* @var string Middle part file extension. * The paths where to look for mapping files.
*
* @var array
*/
protected $_paths = array();
/**
* The file extension of mapping documents.
*
* @var string
*/
protected $_fileExtension = '.php';
/**
* 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.
*/
public function __construct($paths)
{
$this->addPaths((array) $paths);
}
/**
* Append lookup paths to metadata driver.
*
* @param array $paths
*/ */
protected $_middleFileExtension = 'dcm'; public function addPaths(array $paths)
{
$this->_paths = array_unique(array_merge($this->_paths, $paths));
}
/**
* Retrieve the defined metadata lookup paths.
*
* @return array
*/
public function getPaths()
{
return $this->_paths;
}
/**
* 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. * Get the element of schema meta data for the class from the mapping file.
...@@ -70,13 +131,16 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver ...@@ -70,13 +131,16 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
*/ */
public function isTransient($className) public function isTransient($className)
{ {
try { $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
$fileName = $this->_findMappingFile($className);
// Check whether file exists
return false; foreach ((array) $this->_paths as $path) {
} catch (\Exception $e) { if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
return true; return false;
}
} }
return true;
} }
/** /**
...@@ -100,14 +164,12 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver ...@@ -100,14 +164,12 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
); );
foreach ($iterator as $file) { foreach ($iterator as $file) {
$info = pathinfo($file->getPathName()); if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
if ( ! isset($info['extension']) || $info['extension'] != $this->_fileExtension) {
continue; continue;
} }
// NOTE: All files found here means classes are not transient! // NOTE: All files found here means classes are not transient!
$classes[] = str_replace('.', '\\', $file->getBasename('.' . $this->_getFileSuffix())); $classes[] = str_replace('.', '\\', $fileName);
} }
} }
} }
...@@ -125,7 +187,7 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver ...@@ -125,7 +187,7 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
*/ */
protected function _findMappingFile($className) protected function _findMappingFile($className)
{ {
$fileName = str_replace('\\', '.', $className) . '.' . $this->_getFileSuffix(); $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
// Check whether file exists // Check whether file exists
foreach ((array) $this->_paths as $path) { foreach ((array) $this->_paths as $path) {
...@@ -136,17 +198,6 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver ...@@ -136,17 +198,6 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
throw MappingException::mappingFileNotFound($className); throw MappingException::mappingFileNotFound($className);
} }
/**
* Retrieves the mapping file name suffix.
*
* @return string File name suffix.
*/
protected function _getFileSuffix()
{
return ($this->_middleFileExtension != '' ? $this->_middleFileExtension . '.' : '')
. $this->_fileExtension;
}
/** /**
* Loads a mapping file with the given name and returns a map * Loads a mapping file with the given name and returns a map
......
...@@ -41,7 +41,7 @@ require __DIR__ . '/DoctrineAnnotations.php'; ...@@ -41,7 +41,7 @@ require __DIR__ . '/DoctrineAnnotations.php';
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class AnnotationDriver extends AbstractDriver implements Driver class AnnotationDriver implements Driver
{ {
/** /**
* The AnnotationReader. * The AnnotationReader.
...@@ -50,15 +50,75 @@ class AnnotationDriver extends AbstractDriver implements Driver ...@@ -50,15 +50,75 @@ class AnnotationDriver extends AbstractDriver implements Driver
*/ */
private $_reader; private $_reader;
/**
* The paths where to look for mapping files.
*
* @var array
*/
protected $_paths = array();
/**
* The file extension of mapping documents.
*
* @var string
*/
protected $_fileExtension = '.php';
/** /**
* Initializes a new AnnotationDriver that uses the given AnnotationReader for reading * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
* docblock annotations. * docblock annotations.
* *
* @param $reader The AnnotationReader to use. * @param $reader The AnnotationReader to use.
* @param string|array $paths One or multiple paths where mapping classes can be found.
*/ */
public function __construct(AnnotationReader $reader) public function __construct(AnnotationReader $reader, $paths = null)
{ {
$this->_reader = $reader; $this->_reader = $reader;
if ($paths) {
$this->addPaths((array) $paths);
}
}
/**
* Append lookup paths to metadata driver.
*
* @param array $paths
*/
public function addPaths(array $paths)
{
$this->_paths = array_unique(array_merge($this->_paths, $paths));
}
/**
* Retrieve the defined metadata lookup paths.
*
* @return array
*/
public function getPaths()
{
return $this->_paths;
}
/**
* 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;
} }
/** /**
...@@ -359,9 +419,7 @@ class AnnotationDriver extends AbstractDriver implements Driver ...@@ -359,9 +419,7 @@ class AnnotationDriver extends AbstractDriver implements Driver
); );
foreach ($iterator as $file) { foreach ($iterator as $file) {
$info = pathinfo($file->getPathName()); if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
if ( ! isset($info['extension']) || $info['extension'] != $this->_fileExtension) {
continue; continue;
} }
......
...@@ -42,11 +42,79 @@ use Doctrine\Common\DoctrineException, ...@@ -42,11 +42,79 @@ use Doctrine\Common\DoctrineException,
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class PhpDriver extends AbstractDriver implements Driver class PhpDriver implements Driver
{ {
/** The array of class names found and the path to the file */ /**
* @var array The array of class names found and the path to the file
*/
private $_classPaths = array(); private $_classPaths = array();
/**
* The paths where to look for mapping files.
*
* @var array
*/
protected $_paths = array();
/**
* The file extension of mapping documents.
*
* @var string
*/
protected $_fileExtension = '.php';
/**
* Initializes a new PhpDriver 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.
*/
public function __construct($paths)
{
$this->addPaths((array) $paths);
}
/**
* Append lookup paths to metadata driver.
*
* @param array $paths
*/
public function addPaths(array $paths)
{
$this->_paths = array_unique(array_merge($this->_paths, $paths));
}
/**
* Retrieve the defined metadata lookup paths.
*
* @return array
*/
public function getPaths()
{
return $this->_paths;
}
/**
* 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;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -84,15 +152,12 @@ class PhpDriver extends AbstractDriver implements Driver ...@@ -84,15 +152,12 @@ class PhpDriver extends AbstractDriver implements Driver
); );
foreach ($iterator as $file) { foreach ($iterator as $file) {
$info = pathinfo($file->getPathName()); if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
if ( ! isset($info['extension']) || $info['extension'] != $this->_fileExtension) {
continue; continue;
} }
$className = $info['filename']; $classes[] = $fileName;
$classes[] = $className; $this->_classPaths[$fileName] = $file->getPathName();
$this->_classPaths[$className] = $file->getPathName();
} }
} }
} }
......
...@@ -41,7 +41,7 @@ class XmlDriver extends AbstractFileDriver ...@@ -41,7 +41,7 @@ class XmlDriver extends AbstractFileDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $_fileExtension = 'xml'; protected $_fileExtension = '.dcm.xml';
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -369,29 +369,6 @@ class XmlDriver extends AbstractFileDriver ...@@ -369,29 +369,6 @@ class XmlDriver extends AbstractFileDriver
} }
} }
} }
/**
* {@inheritdoc}
*/
protected function _loadMappingFile($file)
{
$result = array();
$xmlElement = simplexml_load_file($file);
if (isset($xmlElement->entity)) {
foreach ($xmlElement->entity as $entityElement) {
$entityName = (string)$entityElement['name'];
$result[$entityName] = $entityElement;
}
} else if (isset($xmlElement->{'mapped-superclass'})) {
foreach ($xmlElement->{'mapped-superclass'} as $mapperSuperClass) {
$className = (string)$mappedSuperClass['name'];
$result[$className] = $mappedSuperClass;
}
}
return $result;
}
/** /**
* Constructs a joinColumn mapping array based on the information * Constructs a joinColumn mapping array based on the information
...@@ -445,4 +422,27 @@ class XmlDriver extends AbstractFileDriver ...@@ -445,4 +422,27 @@ class XmlDriver extends AbstractFileDriver
} }
return $cascades; return $cascades;
} }
/**
* {@inheritdoc}
*/
protected function _loadMappingFile($file)
{
$result = array();
$xmlElement = simplexml_load_file($file);
if (isset($xmlElement->entity)) {
foreach ($xmlElement->entity as $entityElement) {
$entityName = (string)$entityElement['name'];
$result[$entityName] = $entityElement;
}
} else if (isset($xmlElement->{'mapped-superclass'})) {
foreach ($xmlElement->{'mapped-superclass'} as $mapperSuperClass) {
$className = (string)$mappedSuperClass['name'];
$result[$className] = $mappedSuperClass;
}
}
return $result;
}
} }
\ No newline at end of file
...@@ -49,7 +49,7 @@ class YamlDriver extends AbstractFileDriver ...@@ -49,7 +49,7 @@ class YamlDriver extends AbstractFileDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $_fileExtension = 'yml'; protected $_fileExtension = '.dcm.yml';
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -111,20 +111,17 @@ class ClassMetadataExporter ...@@ -111,20 +111,17 @@ class ClassMetadataExporter
$class = $this->_mappingDrivers[$type]; $class = $this->_mappingDrivers[$type];
if (is_subclass_of($class, 'Doctrine\ORM\Mapping\Driver\AbstractDriver')) { if (is_subclass_of($class, 'Doctrine\ORM\Mapping\Driver\AbstractFileDriver')) {
if (is_null($source)) { if (is_null($source)) {
throw DoctrineException::fileMappingDriversRequireDirectoryPath(); throw DoctrineException::fileMappingDriversRequireDirectoryPath();
} }
if ($class == 'Doctrine\ORM\Mapping\Driver\AnnotationDriver') { $driver = new $class($source);
$reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache); } else if ($class == 'Doctrine\ORM\Mapping\Driver\AnnotationDriver') {
$reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader); $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
} else {
$driver = new $class();
}
$driver->addPaths((array) $source); $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, $source);
} else { } else {
$driver = new $class($source); $driver = new $class($source);
} }
......
...@@ -13,8 +13,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase ...@@ -13,8 +13,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
public function testXmlMapping() public function testXmlMapping()
{ {
$className = 'Doctrine\Tests\ORM\Mapping\User'; $className = 'Doctrine\Tests\ORM\Mapping\User';
$xmlDriver = new XmlDriver(); $xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$xmlDriver->addPaths(array(__DIR__ . DIRECTORY_SEPARATOR . 'xml'));
$class = new ClassMetadata($className); $class = new ClassMetadata($className);
...@@ -28,8 +27,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase ...@@ -28,8 +27,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
public function testYamlMapping() public function testYamlMapping()
{ {
$className = 'Doctrine\Tests\ORM\Mapping\User'; $className = 'Doctrine\Tests\ORM\Mapping\User';
$yamlDriver = new YamlDriver(); $yamlDriver = new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml');
$yamlDriver->addPaths(array(__DIR__ . DIRECTORY_SEPARATOR . 'yaml'));
$class = new ClassMetadata($className); $class = new ClassMetadata($className);
...@@ -43,8 +41,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase ...@@ -43,8 +41,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
public function testXmlGetAllClassNames() public function testXmlGetAllClassNames()
{ {
$className = 'Doctrine\Tests\ORM\Mapping\User'; $className = 'Doctrine\Tests\ORM\Mapping\User';
$xmlDriver = new XmlDriver(); $xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$xmlDriver->addPaths(array(__DIR__ . DIRECTORY_SEPARATOR . 'xml'));
$class = new ClassMetadata($className); $class = new ClassMetadata($className);
......
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