Permalink
Browse files

Merge pull request #18 from saem/master

Fix XML and YAML drivers
  • Loading branch information...
beberlei committed Jan 28, 2013
2 parents a13d59e + ef1b9d6 commit 408c17e865616d2f84eec5292ed5e8df13e4a6a0
Showing with 23 additions and 246 deletions.
  1. +9 −120 Mapping/Driver/XmlDriver.php
  2. +10 −121 Mapping/Driver/YamlDriver.php
  3. +1 −1 Resources/config/odm.xml
  4. +3 −4 composer.json
@@ -20,136 +20,25 @@
namespace Doctrine\Bundle\CouchDBBundle\Mapping\Driver;
-use Doctrine\ODM\CouchDB\Mapping\MappingException;
use Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver as BaseXmlDriver;
+use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
/**
* XmlDriver that additionally looks for mapping information in a global file.
*
* @author Fabien Potencier <fabien@symfony.com>
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class XmlDriver extends BaseXmlDriver
{
- protected $prefixes = array();
- protected $globalBasename;
- protected $classCache;
- protected $fileExtension = '.couchdb.xml';
+ const DEFAULT_FILE_EXTENSION = '.couchdb.xml';
- public function setGlobalBasename($file)
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
- $this->globalBasename = $file;
- }
-
- public function getGlobalBasename()
- {
- return $this->globalBasename;
- }
-
- public function setNamespacePrefixes($prefixes)
- {
- $this->prefixes = $prefixes;
- }
-
- public function getNamespacePrefixes()
- {
- return $this->prefixes;
- }
-
- public function isTransient($className)
- {
- return !in_array($className, $this->getAllClassNames());
- }
-
- public function getAllClassNames()
- {
- if (null === $this->classCache) {
- $this->initialize();
- }
-
- $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) {
- $fileName = $file->getBasename($this->fileExtension);
-
- if ($fileName == $file->getBasename() || $fileName == $this->globalBasename) {
- continue;
- }
-
- // NOTE: All files found here means classes are not transient!
- if (isset($this->prefixes[$path])) {
- $classes[] = $this->prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
- } else {
- $classes[] = str_replace('.', '\\', $fileName);
- }
- }
- }
- }
-
- return array_merge($classes, array_keys($this->classCache));
- }
-
- public function getElement($className)
- {
- if (null === $this->classCache) {
- $this->initialize();
- }
-
- if (!isset($this->classCache[$className])) {
- $this->classCache[$className] = parent::getElement($className);
- }
-
- return $this->classCache[$className];
- }
-
- protected function initialize()
- {
- $this->classCache = array();
- if (null !== $this->globalBasename) {
- foreach ($this->paths as $path) {
- if (file_exists($file = $path.'/'.$this->globalBasename.$this->fileExtension)) {
- $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
- }
- }
- }
- }
-
- protected function findMappingFile($className)
- {
- $defaultFileName = str_replace('\\', '.', $className) . $this->fileExtension;
- foreach ($this->paths as $path) {
- if (!isset($this->prefixes[$path])) {
- if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
- return $path . DIRECTORY_SEPARATOR . $defaultFileName;
- }
-
- continue;
- }
-
- $prefix = $this->prefixes[$path];
-
- if (0 !== strpos($className, $prefix.'\\')) {
- continue;
- }
-
- $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
- if (file_exists($filename)) {
- return $filename;
- }
-
- throw MappingException::mappingFileNotFound($className, $filename);
- }
-
- throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
+ $locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);
+ parent::__construct($locator, $fileExtension);
}
}
@@ -20,136 +20,25 @@
namespace Doctrine\Bundle\CouchDBBundle\Mapping\Driver;
-use Doctrine\ODM\CouchDB\Mapping\MappingException;
-use Doctrine\ODM\CouchDB\Mapping\Driver\YamlDriver as BaseYamlDriver;
+use Doctrine\ODM\CouchDB\Mapping\Driver\XmlDriver as BaseYamlDriver;
+use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
/**
* YamlDriver that additionally looks for mapping information in a global file.
*
* @author Fabien Potencier <fabien@symfony.com>
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class YamlDriver extends BaseYamlDriver
{
- protected $prefixes = array();
- protected $globalBasename;
- protected $classCache;
- protected $fileExtension = '.couchdb.yml';
+ const DEFAULT_FILE_EXTENSION = '.couchdb.yml';
- public function setGlobalBasename($file)
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
- $this->globalBasename = $file;
- }
-
- public function getGlobalBasename()
- {
- return $this->globalBasename;
- }
-
- public function setNamespacePrefixes($prefixes)
- {
- $this->prefixes = $prefixes;
- }
-
- public function getNamespacePrefixes()
- {
- return $this->prefixes;
- }
-
- public function isTransient($className)
- {
- return !in_array($className, $this->getAllClassNames());
- }
-
- public function getAllClassNames()
- {
- if (null === $this->classCache) {
- $this->initialize();
- }
-
- $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) {
- $fileName = $file->getBasename($this->fileExtension);
-
- if ($fileName == $file->getBasename() || $fileName == $this->globalBasename) {
- continue;
- }
-
- // NOTE: All files found here means classes are not transient!
- if (isset($this->prefixes[$path])) {
- $classes[] = $this->prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
- } else {
- $classes[] = str_replace('.', '\\', $fileName);
- }
- }
- }
- }
-
- return array_merge($classes, array_keys($this->classCache));
- }
-
- public function getElement($className)
- {
- if (null === $this->classCache) {
- $this->initialize();
- }
-
- if (!isset($this->classCache[$className])) {
- $this->classCache[$className] = parent::getElement($className);
- }
-
- return $this->classCache[$className];
- }
-
- protected function initialize()
- {
- $this->classCache = array();
- if (null !== $this->globalBasename) {
- foreach ($this->paths as $path) {
- if (file_exists($file = $path.'/'.$this->globalBasename.$this->fileExtension)) {
- $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
- }
- }
- }
- }
-
- protected function _findMappingFile($className)
- {
- $defaultFileName = str_replace('\\', '.', $className) . $this->fileExtension;
- foreach ($this->paths as $path) {
- if (!isset($this->prefixes[$path])) {
- if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
- return $path . DIRECTORY_SEPARATOR . $defaultFileName;
- }
-
- continue;
- }
-
- $prefix = $this->prefixes[$path];
-
- if (0 !== strpos($className, $prefix.'\\')) {
- continue;
- }
-
- $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
- if (file_exists($filename)) {
- return $filename;
- }
-
- throw MappingException::mappingFileNotFound($className, $filename);
- }
-
- throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
+ $locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);
+ parent::__construct($locator, $fileExtension);
}
}
View
@@ -26,7 +26,7 @@
<!-- form field factory guesser -->
<parameter key="form.type_guesser.doctrine_couchdb.class">Doctrine\Bundle\CouchDBBundle\Form\CouchDBTypeGuesser</parameter>
- <parameter key="doctrine_couchdb.odm.metadata.driver_chain.class">Doctrine\ODM\CouchDB\Mapping\Driver\DriverChain</parameter>
+ <parameter key="doctrine_couchdb.odm.metadata.driver_chain.class">Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain</parameter>
<parameter key="doctrine_couchdb.odm.metadata.annotation.class">Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver</parameter>
<parameter key="doctrine_couchdb.odm.metadata.annotation_reader.class">Doctrine\Common\Annotations\AnnotationReader</parameter>
<parameter key="doctrine_couchdb.odm.metadata.xml.class">Doctrine\Bundle\CouchDBBundle\Mapping\Driver\XmlDriver</parameter>
View
@@ -11,14 +11,13 @@
],
"require": {
"php": ">=5.3.2",
- "doctrine/couchdb": "*",
- "symfony/symfony": "2.1.*"
+ "doctrine/couchdb-odm": "*",
+ "symfony/doctrine-bridge": ">=2.0.0,<2.2.0-dev",
+ "symfony/framework-bundle": ">=2.0.0,<2.2.0-dev"
},
"require-dev": {
- "doctrine/couchdb-odm": "*"
},
"suggest": {
- "doctrine/couchdb-odm": "*"
},
"autoload": {
"psr-0": { "Doctrine\\Bundle\\CouchDBBundle": "" }

0 comments on commit 408c17e

Please sign in to comment.