Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix XML and YAML drivers #18

Merged
merged 2 commits into from

4 participants

@saem

....

Previously files were not correctly searched for when using automapping,
additionally all paths weren't traversed before throwing and exception,
lastly the exception factory method being used didn't exist in the
dependency (which has been added as a separate commit).

saem added some commits
@saem saem Fix XML and YAML drivers to correctly include files and throw excepti…
…ons.

Previously files were not correctly searched for when using automapping,
additionally all paths weren't traversed before throwing and exception,
lastly the exception factory method being used didn't exist in the
dependency (which has been added as a separate commit).
a0749ee
@saem saem Updates to use latest Doctrine/common faclities ef1b9d6
@lsmith77
Collaborator

there is some infrastructure for this inside Common that could be used instead:
https://github.com/doctrine/DoctrinePHPCRBundle/tree/master/Mapping/Driver

@saem

I saw that too, I just wanted to get it working, and then look at making it better as I get more comfortable (baby steps and all). I was poking at the mongodb bundle when I noticed that they were inheriting from the base ODM which inherited from one in Common, IIRC -- which seems to be the same thing happening in the PHPCR.

Honestly, I'm new to Doctrine and Doctrine Bridge development, and wasn't quite sure how big a change it would be. From my brief look, I wasn't entirely sure I could just change up the service names (in the odm.xml), and then inherit as per the PHPCR/MongoDB bundles. This gives working code now.

For my next trick I'll work on the larger change you're alluding to.

@stof
Collaborator

@saem inheriting the code of Common should be done first in the CouchDB ODM by refactoring it. Once done, you will not need any logic anymore here

@saem

I'm working on that now. A lot of code got blown away in the drivers, but it's all ultimately used in the ClassMetaDataFactory, which in the CouchDB ODM's case is quite old. I'm trying to wrap my head around all the nuances (currently have a failing test in terms of mapped super classes), but everything else seems to work. Once I get around that, it should be good to go.

Fair warning, the commit will be large!

@stof
Collaborator

@saem the ClassMetaDataFactory should be refactored to use the code from Common too

@saem

This is a dependent pull request against couchdb-odm doctrine/couchdb-odm#47

Once that is patch should take care of any issues with bridging the bundle.

@beberlei beberlei merged commit 408c17e into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 25, 2012
  1. @saem

    Fix XML and YAML drivers to correctly include files and throw excepti…

    saem authored
    …ons.
    
    Previously files were not correctly searched for when using automapping,
    additionally all paths weren't traversed before throwing and exception,
    lastly the exception factory method being used didn't exist in the
    dependency (which has been added as a separate commit).
Commits on Sep 26, 2012
  1. @saem
This page is out of date. Refresh to see the latest.
View
129 Mapping/Driver/XmlDriver.php
@@ -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);
}
}
View
131 Mapping/Driver/YamlDriver.php
@@ -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
2  Resources/config/odm.xml
@@ -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
7 composer.json
@@ -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": "" }
Something went wrong with that request. Please try again.