From 34bb0c4943fbcecf88c0b57a7ced0e39c069981e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Tue, 17 Jan 2012 12:32:10 +0100 Subject: [PATCH] Reducing code duplication SimplifiedXmlDriver and SimplifiedYamlDriver are still not valid after this commit --- .../ORM/Mapping/Driver/AbstractFileDriver.php | 139 +----------------- lib/Doctrine/ORM/Mapping/Driver/PHPDriver.php | 18 ++- .../Mapping/Driver/SimplifiedXmlDriver.php | 14 +- .../Mapping/Driver/SimplifiedYamlDriver.php | 14 +- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php | 9 +- .../ORM/Mapping/Driver/YamlDriver.php | 14 +- .../Mapping/Symfony/AbstractDriverTest.php | 4 +- 7 files changed, 60 insertions(+), 152 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php index 86ef9ce2a..9a4ee054c 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AbstractFileDriver.php @@ -20,6 +20,7 @@ namespace Doctrine\ORM\Mapping\Driver; use Doctrine\ORM\Mapping\MappingException; +use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; /** * Base driver for file-based metadata drivers. @@ -37,33 +38,8 @@ use Doctrine\ORM\Mapping\MappingException; * @author Jonathan H. Wage * @author Roman Borschel */ -abstract class AbstractFileDriver implements Driver +abstract class AbstractFileDriver extends FileDriver implements Driver { - /** - * The paths where to look for mapping files. - * - * @var array - */ - protected $_paths = array(); - - /** - * The file extension of mapping documents. - * - * @var string - */ - protected $_fileExtension; - - /** - * 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. * @@ -71,7 +47,7 @@ abstract class AbstractFileDriver implements Driver */ public function addPaths(array $paths) { - $this->_paths = array_unique(array_merge($this->_paths, $paths)); + $this->locator->addPaths($paths); } /** @@ -81,100 +57,7 @@ abstract class AbstractFileDriver implements Driver */ 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. - * 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) - { - $result = $this->_loadMappingFile($this->_findMappingFile($className)); - - if(!isset($result[$className])){ - throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->_fileExtension); - } - return $result[$className]; - } - - /** - * 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) - { - $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; - } - - /** - * Gets the names of all mapped classes known to this driver. - * - * @return array The names of all mapped classes known to this driver. - */ - public function getAllClassNames() - { - $classes = array(); - - if ($this->_paths) { - foreach ((array) $this->_paths as $path) { - if ( ! is_dir($path)) { - throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); - } - - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($path), - \RecursiveIteratorIterator::LEAVES_ONLY - ); - - foreach ($iterator as $file) { - if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { - continue; - } - - // NOTE: All files found here means classes are not transient! - $classes[] = str_replace('.', '\\', $fileName); - } - } - } - - return $classes; + return $this->locator->getPaths(); } /** @@ -185,8 +68,9 @@ abstract class AbstractFileDriver implements Driver * @return string The (absolute) file name. * @throws MappingException */ - protected function _findMappingFile($className) + protected function findMappingFile($className) { + return $this->locator->findMappingFile($className); $fileName = str_replace('\\', '.', $className) . $this->_fileExtension; // Check whether file exists @@ -198,13 +82,4 @@ abstract class AbstractFileDriver implements Driver throw MappingException::mappingFileNotFound($className, $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 diff --git a/lib/Doctrine/ORM/Mapping/Driver/PHPDriver.php b/lib/Doctrine/ORM/Mapping/Driver/PHPDriver.php index 1e3f7f686..4a4dbd047 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/PHPDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/PHPDriver.php @@ -43,6 +43,8 @@ use Doctrine\Common\Cache\ArrayCache, */ class PHPDriver extends AbstractFileDriver { + const DEFAULT_FILE_EXTENSION = '.php'; + /** * {@inheritdoc} */ @@ -52,16 +54,24 @@ class PHPDriver extends AbstractFileDriver /** * {@inheritdoc} */ - public function loadMetadataForClass($className, ClassMetadata $metadata) + public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) { - $this->_metadata = $metadata; - $this->_loadMappingFile($this->_findMappingFile($className)); + parent::__construct($locator, $fileExtension); } /** * {@inheritdoc} */ - protected function _loadMappingFile($file) + public function loadMetadataForClass($className, ClassMetadata $metadata) + { + $this->_metadata = $metadata; + $this->loadMappingFile($this->findMappingFile($className)); + } + + /** + * {@inheritdoc} + */ + protected function loadMappingFile($file) { $metadata = $this->_metadata; include $file; diff --git a/lib/Doctrine/ORM/Mapping/Driver/SimplifiedXmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/SimplifiedXmlDriver.php index c87282d75..325495d3a 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/SimplifiedXmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/SimplifiedXmlDriver.php @@ -30,13 +30,19 @@ use Doctrine\ORM\Mapping\MappingException; */ class SimplifiedXmlDriver extends XmlDriver { + const DEFAULT_FILE_EXTENSION = '.orm.xml'; + protected $_prefixes = array(); protected $_globalBasename; protected $_classCache; protected $_fileExtension = '.orm.xml'; - public function __construct($prefixes) + /** + * {@inheritdoc} + */ + public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION) { + parent::__construct(array(), $fileExtension); $this->addNamespacePrefixes($prefixes); } @@ -73,7 +79,7 @@ class SimplifiedXmlDriver extends XmlDriver } try { - $this->_findMappingFile($className); + $this->findMappingFile($className); return false; } catch (MappingException $e) { @@ -139,13 +145,13 @@ class SimplifiedXmlDriver extends XmlDriver if (null !== $this->_globalBasename) { foreach ($this->_paths as $path) { if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) { - $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file)); + $this->_classCache = array_merge($this->_classCache, $this->loadMappingFile($file)); } } } } - protected function _findMappingFile($className) + protected function findMappingFile($className) { $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension; foreach ($this->_paths as $path) { diff --git a/lib/Doctrine/ORM/Mapping/Driver/SimplifiedYamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/SimplifiedYamlDriver.php index 31c4eb85e..a6e55ae84 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/SimplifiedYamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/SimplifiedYamlDriver.php @@ -30,13 +30,19 @@ use Doctrine\ORM\Mapping\MappingException; */ class SimplifiedYamlDriver extends YamlDriver { + const DEFAULT_FILE_EXTENSION = '.orm.xml'; + protected $_prefixes = array(); protected $_globalBasename; protected $_classCache; protected $_fileExtension = '.orm.yml'; - public function __construct($prefixes) + /** + * {@inheritdoc} + */ + public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION) { + parent::__construct(array(), $fileExtension); $this->addNamespacePrefixes($prefixes); } @@ -78,7 +84,7 @@ class SimplifiedYamlDriver extends YamlDriver } try { - $this->_findMappingFile($className); + $this->findMappingFile($className); return false; } catch (MappingException $e) { @@ -144,13 +150,13 @@ class SimplifiedYamlDriver extends YamlDriver if (null !== $this->_globalBasename) { foreach ($this->_paths as $path) { if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) { - $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file)); + $this->_classCache = array_merge($this->_classCache, $this->loadMappingFile($file)); } } } } - protected function _findMappingFile($className) + protected function findMappingFile($className) { $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension; foreach ($this->_paths as $path) { diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index 46b46dc5f..3ab7a08f2 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -36,10 +36,15 @@ use SimpleXMLElement, */ class XmlDriver extends AbstractFileDriver { + const DEFAULT_FILE_EXTENSION = '.dcm.xml'; + /** * {@inheritdoc} */ - protected $_fileExtension = '.dcm.xml'; + public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) + { + parent::__construct($locator, $fileExtension); + } /** * {@inheritdoc} @@ -681,7 +686,7 @@ class XmlDriver extends AbstractFileDriver /** * {@inheritdoc} */ - protected function _loadMappingFile($file) + protected function loadMappingFile($file) { $result = array(); $xmlElement = simplexml_load_file($file); diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index fb31c5416..d74505150 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -20,7 +20,8 @@ namespace Doctrine\ORM\Mapping\Driver; use Doctrine\Common\Persistence\Mapping\ClassMetadata, - Doctrine\ORM\Mapping\MappingException; + Doctrine\ORM\Mapping\MappingException, + Symfony\Component\Yaml\Yaml; /** * The YamlDriver reads the mapping metadata from yaml schema files. @@ -33,10 +34,15 @@ use Doctrine\Common\Persistence\Mapping\ClassMetadata, */ class YamlDriver extends AbstractFileDriver { + const DEFAULT_FILE_EXTENSION = '.dcm.yml'; + /** * {@inheritdoc} */ - protected $_fileExtension = '.dcm.yml'; + public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) + { + parent::__construct($locator, $fileExtension); + } /** * {@inheritdoc} @@ -666,8 +672,8 @@ class YamlDriver extends AbstractFileDriver /** * {@inheritdoc} */ - protected function _loadMappingFile($file) + protected function loadMappingFile($file) { - return \Symfony\Component\Yaml\Yaml::parse($file); + return Yaml::parse($file); } } diff --git a/tests/Doctrine/Tests/ORM/Mapping/Symfony/AbstractDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/Symfony/AbstractDriverTest.php index d16db4fbb..18e0d5534 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/Symfony/AbstractDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/Symfony/AbstractDriverTest.php @@ -32,7 +32,7 @@ abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase )); touch($filename = $this->dir.'/Foo'.$this->getFileExtension()); - $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo'))); + $this->assertEquals($filename, $this->invoke($driver, 'findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo'))); } public function testFindMappingFileInSubnamespace() @@ -42,7 +42,7 @@ abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase )); touch($filename = $this->dir.'/Foo.Bar'.$this->getFileExtension()); - $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo\Bar'))); + $this->assertEquals($filename, $this->invoke($driver, 'findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo\Bar'))); } public function testFindMappingFileNamespacedFoundFileNotFound()