1
0
mirror of synced 2025-01-22 08:11:40 +03:00

Reducing code duplication

SimplifiedXmlDriver and SimplifiedYamlDriver are still not valid after this commit
This commit is contained in:
Marco Pivetta 2012-01-17 12:32:10 +01:00
parent e6a2bae5d7
commit 34bb0c4943
7 changed files with 60 additions and 152 deletions

View File

@ -20,6 +20,7 @@
namespace Doctrine\ORM\Mapping\Driver; namespace Doctrine\ORM\Mapping\Driver;
use Doctrine\ORM\Mapping\MappingException; use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
/** /**
* Base driver for file-based metadata drivers. * Base driver for file-based metadata drivers.
@ -37,33 +38,8 @@ 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 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. * Append lookup paths to metadata driver.
* *
@ -71,7 +47,7 @@ abstract class AbstractFileDriver implements Driver
*/ */
public function addPaths(array $paths) 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() public function getPaths()
{ {
return $this->_paths; return $this->locator->getPaths();
}
/**
* 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;
} }
/** /**
@ -185,8 +68,9 @@ abstract class AbstractFileDriver implements Driver
* @return string The (absolute) file name. * @return string The (absolute) file name.
* @throws MappingException * @throws MappingException
*/ */
protected function _findMappingFile($className) protected function findMappingFile($className)
{ {
return $this->locator->findMappingFile($className);
$fileName = str_replace('\\', '.', $className) . $this->_fileExtension; $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
// Check whether file exists // Check whether file exists
@ -198,13 +82,4 @@ abstract class AbstractFileDriver implements Driver
throw MappingException::mappingFileNotFound($className, $fileName); 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);
} }

View File

@ -43,6 +43,8 @@ use Doctrine\Common\Cache\ArrayCache,
*/ */
class PHPDriver extends AbstractFileDriver class PHPDriver extends AbstractFileDriver
{ {
const DEFAULT_FILE_EXTENSION = '.php';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -52,16 +54,24 @@ class PHPDriver extends AbstractFileDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function loadMetadataForClass($className, ClassMetadata $metadata) public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{ {
$this->_metadata = $metadata; parent::__construct($locator, $fileExtension);
$this->_loadMappingFile($this->_findMappingFile($className));
} }
/** /**
* {@inheritdoc} * {@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; $metadata = $this->_metadata;
include $file; include $file;

View File

@ -30,13 +30,19 @@ use Doctrine\ORM\Mapping\MappingException;
*/ */
class SimplifiedXmlDriver extends XmlDriver class SimplifiedXmlDriver extends XmlDriver
{ {
const DEFAULT_FILE_EXTENSION = '.orm.xml';
protected $_prefixes = array(); protected $_prefixes = array();
protected $_globalBasename; protected $_globalBasename;
protected $_classCache; protected $_classCache;
protected $_fileExtension = '.orm.xml'; 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); $this->addNamespacePrefixes($prefixes);
} }
@ -73,7 +79,7 @@ class SimplifiedXmlDriver extends XmlDriver
} }
try { try {
$this->_findMappingFile($className); $this->findMappingFile($className);
return false; return false;
} catch (MappingException $e) { } catch (MappingException $e) {
@ -139,13 +145,13 @@ class SimplifiedXmlDriver extends XmlDriver
if (null !== $this->_globalBasename) { if (null !== $this->_globalBasename) {
foreach ($this->_paths as $path) { foreach ($this->_paths as $path) {
if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) { 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; $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
foreach ($this->_paths as $path) { foreach ($this->_paths as $path) {

View File

@ -30,13 +30,19 @@ use Doctrine\ORM\Mapping\MappingException;
*/ */
class SimplifiedYamlDriver extends YamlDriver class SimplifiedYamlDriver extends YamlDriver
{ {
const DEFAULT_FILE_EXTENSION = '.orm.xml';
protected $_prefixes = array(); protected $_prefixes = array();
protected $_globalBasename; protected $_globalBasename;
protected $_classCache; protected $_classCache;
protected $_fileExtension = '.orm.yml'; 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); $this->addNamespacePrefixes($prefixes);
} }
@ -78,7 +84,7 @@ class SimplifiedYamlDriver extends YamlDriver
} }
try { try {
$this->_findMappingFile($className); $this->findMappingFile($className);
return false; return false;
} catch (MappingException $e) { } catch (MappingException $e) {
@ -144,13 +150,13 @@ class SimplifiedYamlDriver extends YamlDriver
if (null !== $this->_globalBasename) { if (null !== $this->_globalBasename) {
foreach ($this->_paths as $path) { foreach ($this->_paths as $path) {
if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) { 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; $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
foreach ($this->_paths as $path) { foreach ($this->_paths as $path) {

View File

@ -36,10 +36,15 @@ use SimpleXMLElement,
*/ */
class XmlDriver extends AbstractFileDriver class XmlDriver extends AbstractFileDriver
{ {
const DEFAULT_FILE_EXTENSION = '.dcm.xml';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $_fileExtension = '.dcm.xml'; public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
parent::__construct($locator, $fileExtension);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -681,7 +686,7 @@ class XmlDriver extends AbstractFileDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _loadMappingFile($file) protected function loadMappingFile($file)
{ {
$result = array(); $result = array();
$xmlElement = simplexml_load_file($file); $xmlElement = simplexml_load_file($file);

View File

@ -20,7 +20,8 @@
namespace Doctrine\ORM\Mapping\Driver; namespace Doctrine\ORM\Mapping\Driver;
use Doctrine\Common\Persistence\Mapping\ClassMetadata, 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. * The YamlDriver reads the mapping metadata from yaml schema files.
@ -33,10 +34,15 @@ use Doctrine\Common\Persistence\Mapping\ClassMetadata,
*/ */
class YamlDriver extends AbstractFileDriver class YamlDriver extends AbstractFileDriver
{ {
const DEFAULT_FILE_EXTENSION = '.dcm.yml';
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected $_fileExtension = '.dcm.yml'; public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
parent::__construct($locator, $fileExtension);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -666,8 +672,8 @@ class YamlDriver extends AbstractFileDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function _loadMappingFile($file) protected function loadMappingFile($file)
{ {
return \Symfony\Component\Yaml\Yaml::parse($file); return Yaml::parse($file);
} }
} }

View File

@ -32,7 +32,7 @@ abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
)); ));
touch($filename = $this->dir.'/Foo'.$this->getFileExtension()); 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() public function testFindMappingFileInSubnamespace()
@ -42,7 +42,7 @@ abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
)); ));
touch($filename = $this->dir.'/Foo.Bar'.$this->getFileExtension()); 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() public function testFindMappingFileNamespacedFoundFileNotFound()