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;
* @author Jonathan H. Wage <jonwage@gmail.com>
* @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
*/
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
*/
protected $_middleFileExtension = 'dcm';
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.
......@@ -70,15 +131,18 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
*/
public function isTransient($className)
{
try {
$fileName = $this->_findMappingFile($className);
$fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
// Check whether file exists
foreach ((array) $this->_paths as $path) {
if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
return false;
} catch (\Exception $e) {
return true;
}
}
return true;
}
/**
* Gets the names of all mapped classes known to this driver.
*
......@@ -100,14 +164,12 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
);
foreach ($iterator as $file) {
$info = pathinfo($file->getPathName());
if ( ! isset($info['extension']) || $info['extension'] != $this->_fileExtension) {
if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
continue;
}
// 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
*/
protected function _findMappingFile($className)
{
$fileName = str_replace('\\', '.', $className) . '.' . $this->_getFileSuffix();
$fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
// Check whether file exists
foreach ((array) $this->_paths as $path) {
......@@ -137,17 +199,6 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver
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
* from class/entity names to their corresponding elements.
......
......@@ -41,7 +41,7 @@ require __DIR__ . '/DoctrineAnnotations.php';
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class AnnotationDriver extends AbstractDriver implements Driver
class AnnotationDriver implements Driver
{
/**
* The AnnotationReader.
......@@ -50,15 +50,75 @@ class AnnotationDriver extends AbstractDriver implements Driver
*/
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
* docblock annotations.
*
* @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;
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
);
foreach ($iterator as $file) {
$info = pathinfo($file->getPathName());
if ( ! isset($info['extension']) || $info['extension'] != $this->_fileExtension) {
if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
continue;
}
......
......@@ -42,11 +42,79 @@ use Doctrine\Common\DoctrineException,
* @author Jonathan H. Wage <jonwage@gmail.com>
* @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();
/**
* 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}
*/
......@@ -84,15 +152,12 @@ class PhpDriver extends AbstractDriver implements Driver
);
foreach ($iterator as $file) {
$info = pathinfo($file->getPathName());
if ( ! isset($info['extension']) || $info['extension'] != $this->_fileExtension) {
if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
continue;
}
$className = $info['filename'];
$classes[] = $className;
$this->_classPaths[$className] = $file->getPathName();
$classes[] = $fileName;
$this->_classPaths[$fileName] = $file->getPathName();
}
}
}
......
......@@ -41,7 +41,7 @@ class XmlDriver extends AbstractFileDriver
/**
* {@inheritdoc}
*/
protected $_fileExtension = 'xml';
protected $_fileExtension = '.dcm.xml';
/**
* {@inheritdoc}
......@@ -370,29 +370,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
* found in the given SimpleXMLElement.
......@@ -445,4 +422,27 @@ class XmlDriver extends AbstractFileDriver
}
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
/**
* {@inheritdoc}
*/
protected $_fileExtension = 'yml';
protected $_fileExtension = '.dcm.yml';
/**
* {@inheritdoc}
......
......@@ -111,20 +111,17 @@ class ClassMetadataExporter
$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)) {
throw DoctrineException::fileMappingDriversRequireDirectoryPath();
}
if ($class == 'Doctrine\ORM\Mapping\Driver\AnnotationDriver') {
$driver = new $class($source);
} else 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);
$driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, $source);
} else {
$driver = new $class($source);
}
......
......@@ -13,8 +13,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
public function testXmlMapping()
{
$className = 'Doctrine\Tests\ORM\Mapping\User';
$xmlDriver = new XmlDriver();
$xmlDriver->addPaths(array(__DIR__ . DIRECTORY_SEPARATOR . 'xml'));
$xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$class = new ClassMetadata($className);
......@@ -28,8 +27,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
public function testYamlMapping()
{
$className = 'Doctrine\Tests\ORM\Mapping\User';
$yamlDriver = new YamlDriver();
$yamlDriver->addPaths(array(__DIR__ . DIRECTORY_SEPARATOR . 'yaml'));
$yamlDriver = new YamlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'yaml');
$class = new ClassMetadata($className);
......@@ -43,8 +41,7 @@ class MappingDriverTest extends \Doctrine\Tests\OrmTestCase
public function testXmlGetAllClassNames()
{
$className = 'Doctrine\Tests\ORM\Mapping\User';
$xmlDriver = new XmlDriver();
$xmlDriver->addPaths(array(__DIR__ . DIRECTORY_SEPARATOR . 'xml'));
$xmlDriver = new XmlDriver(__DIR__ . DIRECTORY_SEPARATOR . 'xml');
$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