Permalink
Browse files

Merge branch '2.0'

Conflicts:
	Mapping/Driver/XmlDriver.php
	Mapping/Driver/YamlDriver.php
	Tests/ContainerTest.php
	Tests/Mapping/Driver/XmlDriverTest.php
	Tests/Mapping/Driver/YamlDriverTest.php
	Tests/bootstrap.php
  • Loading branch information...
2 parents 091f67d + 84fc74c commit e53d16831df935e9a3ed9a8f368a9a26b60851d6 @jmikola jmikola committed Jul 20, 2012
@@ -304,6 +304,33 @@ protected function loadDocumentManagerBundlesMappingInformation(array $documentM
$odmConfigDef->addMethodCall('setDocumentNamespaces', array($this->aliasMap));
}
+ protected function registerMappingDrivers($objectManager, ContainerBuilder $container)
+ {
+ parent::registerMappingDrivers($objectManager, $container);
+
+ /* In Symfony 2.0, the ORM bundle uses its own mapping drivers, while
+ * ODM utilizes those in Doctrine Common. To account for this, we must
+ * remove the setNamespacePrefixes() method call from any XML or YML
+ * drivers and pass its argument to the constructor.
+ */
+ foreach ($this->drivers as $driverType => $driverPaths) {
+ if ('xml' !== $driverType && 'yml' !== $driverType) {
+ continue;
+ }
+
+ $mappingService = $this->getObjectManagerElementName($objectManager['name'].'_'.$driverType.'_metadata_driver');
+ $mappingDriverDef = $container->getDefinition($mappingService);
+
+ foreach ($mappingDriverDef->getMethodCalls() as $call) {
+ if ($call[0] === 'setNamespacePrefixes') {
+ $mappingDriverDef->setArguments($call[1]);
+ break;
+ }
+ }
+ $mappingDriverDef->removeMethodCall('setNamespacePrefixes');
+ }
+ }
+
protected function getObjectManagerElementName($name)
{
return 'doctrine.odm.mongodb.' . $name;
@@ -14,8 +14,8 @@
namespace Doctrine\Bundle\MongoDBBundle\Mapping\Driver;
+use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver as BaseXmlDriver;
-use Doctrine\ODM\MongoDB\MongoDBException;
/**
* XmlDriver that additionally looks for mapping information in a global file.
@@ -25,133 +25,14 @@
*/
class XmlDriver extends BaseXmlDriver
{
- protected $prefixes = array();
- protected $globalBasename;
- protected $classCache;
- protected $fileExtension = '.mongodb.xml';
+ const DEFAULT_FILE_EXTENSION = '.mongodb.xml';
- public function __construct($prefixesByPath = array())
+ /**
+ * {@inheritDoc}
+ */
+ public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
- parent::__construct(array_keys($prefixesByPath));
-
- $this->setNamespacePrefixes($prefixesByPath);
- }
-
- public function setGlobalBasename($file)
- {
- $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 MongoDBException::fileMappingDriversRequireConfiguredDirectoryPath();
- }
-
- $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 MongoDBException::mappingNotFound($className, $filename);
- }
-
- throw MongoDBException::mappingNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
+ $locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);
+ parent::__construct($locator, $fileExtension);
}
}
@@ -14,8 +14,8 @@
namespace Doctrine\Bundle\MongoDBBundle\Mapping\Driver;
+use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver as BaseYamlDriver;
-use Doctrine\ODM\MongoDB\MongoDBException;
/**
* YamlDriver that additionally looks for mapping information in a global file.
@@ -25,133 +25,14 @@
*/
class YamlDriver extends BaseYamlDriver
{
- protected $prefixes = array();
- protected $globalBasename;
- protected $classCache;
- protected $fileExtension = '.mongodb.yml';
+ const DEFAULT_FILE_EXTENSION = '.mongodb.yml';
- public function __construct($prefixesByPath = array())
+ /**
+ * {@inheritDoc}
+ */
+ public function __construct($prefixes, $fileExtension = self::DEFAULT_FILE_EXTENSION)
{
- parent::__construct(array_keys($prefixesByPath));
-
- $this->setNamespacePrefixes($prefixesByPath);
- }
-
- public function setGlobalBasename($file)
- {
- $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 MongoDBException::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 MongoDBException::mappingNotFound($className, $filename);
- }
-
- throw MongoDBException::mappingNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
+ $locator = new SymfonyFileLocator((array) $prefixes, $fileExtension);
+ parent::__construct($locator, $fileExtension);
}
}
@@ -41,7 +41,7 @@
<parameter key="doctrine.odm.mongodb.cache.xcache.class">Doctrine\Common\Cache\XcacheCache</parameter>
<!-- metadata -->
- <parameter key="doctrine.odm.mongodb.metadata.driver_chain.class">Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain</parameter>
+ <parameter key="doctrine.odm.mongodb.metadata.driver_chain.class">Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain</parameter>
<parameter key="doctrine.odm.mongodb.metadata.annotation.class">Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver</parameter>
<parameter key="doctrine.odm.mongodb.metadata.xml.class">Doctrine\Bundle\MongoDBBundle\Mapping\Driver\XmlDriver</parameter>
<parameter key="doctrine.odm.mongodb.metadata.yml.class">Doctrine\Bundle\MongoDBBundle\Mapping\Driver\YamlDriver</parameter>
@@ -43,7 +43,7 @@ public function testDependencyInjectionConfigurationDefaults()
$this->assertEquals('11211', $container->getParameter('doctrine.odm.mongodb.cache.memcache_port'));
$this->assertEquals('Memcache', $container->getParameter('doctrine.odm.mongodb.cache.memcache_instance.class'));
$this->assertEquals('Doctrine\Common\Cache\XcacheCache', $container->getParameter('doctrine.odm.mongodb.cache.xcache.class'));
- $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain', $container->getParameter('doctrine.odm.mongodb.metadata.driver_chain.class'));
+ $this->assertEquals('Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain', $container->getParameter('doctrine.odm.mongodb.metadata.driver_chain.class'));
$this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver', $container->getParameter('doctrine.odm.mongodb.metadata.annotation.class'));
$this->assertEquals('Doctrine\Bundle\MongoDBBundle\Mapping\Driver\XmlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.xml.class'));
$this->assertEquals('Doctrine\Bundle\MongoDBBundle\Mapping\Driver\YamlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.yml.class'));
Oops, something went wrong.

0 comments on commit e53d168

Please sign in to comment.