diff --git a/lib/Doctrine/ORM/Mapping/Driver/AbstractDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AbstractDriver.php deleted file mode 100644 index c18eeca5f..000000000 --- a/lib/Doctrine/ORM/Mapping/Driver/AbstractDriver.php +++ /dev/null @@ -1,97 +0,0 @@ -. - */ - -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 - * @author Guilherme Blanco - * @author Jonathan H. Wage - * @author Roman Borschel - */ -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 diff --git a/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php index af1f8ff96..b0197894b 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php @@ -40,12 +40,73 @@ use Doctrine\ORM\Mapping\MappingException; * @author Jonathan H. Wage * @author Roman Borschel */ -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 $_middleFileExtension = 'dcm'; + 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 + */ + 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,13 +131,16 @@ abstract class AbstractFileDriver extends AbstractDriver implements Driver */ public function isTransient($className) { - try { - $fileName = $this->_findMappingFile($className); - - return false; - } catch (\Exception $e) { - return true; + $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; + } } + + return true; } /** @@ -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) { @@ -136,17 +198,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 diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index ebc222a73..f75982a0c 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -41,7 +41,7 @@ require __DIR__ . '/DoctrineAnnotations.php'; * @author Jonathan H. Wage * @author Roman Borschel */ -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; } diff --git a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php index 59abd48ea..8de3cbad3 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php @@ -42,11 +42,79 @@ use Doctrine\Common\DoctrineException, * @author Jonathan H. Wage * @author Roman Borschel */ -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(); } } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index 29fbfc0f7..04ccff231 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -41,7 +41,7 @@ class XmlDriver extends AbstractFileDriver /** * {@inheritdoc} */ - protected $_fileExtension = 'xml'; + protected $_fileExtension = '.dcm.xml'; /** * {@inheritdoc} @@ -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 @@ -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 diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index da1d142d9..3a4c1994c 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -49,7 +49,7 @@ class YamlDriver extends AbstractFileDriver /** * {@inheritdoc} */ - protected $_fileExtension = 'yml'; + protected $_fileExtension = '.dcm.yml'; /** * {@inheritdoc} diff --git a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php index 588fef281..03057412d 100644 --- a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php @@ -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') { - $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 = 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->addPaths((array) $source); + $driver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, $source); } else { $driver = new $class($source); } diff --git a/tests/Doctrine/Tests/ORM/Mapping/MappingDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/MappingDriverTest.php index 53d9733aa..a9539eb4c 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/MappingDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/MappingDriverTest.php @@ -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);