Permalink
Browse files

added support for global mapping files

  • Loading branch information...
1 parent 7252272 commit d38f1136ff3dff7d13b38db2e10630809719fcf5 @kriswallsmith kriswallsmith committed May 10, 2011
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver;
+
+use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver as BaseXmlDriver;
+
+/**
+ * XmlDriver that additionally looks for mapping information in a global file.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Kris Wallsmith <kris@symfony.com>
+ */
+class XmlDriver extends BaseXmlDriver
+{
+ protected $classCache;
+ protected $globalFile = 'mapping';
+ protected $fileExtension = '.mongodb.xml';
+
+ public function isTransient($className)
+ {
+ return !in_array($className, $this->getAllClassNames());
+ }
+
+ public function getAllClassNames()
+ {
+ if (null === $this->classCache) {
+ $this->initialize();
+ }
+
+ return array_merge(parent::getAllClassNames(), 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();
+ foreach ($this->paths as $path) {
+ if (file_exists($file = $path.'/'.$this->globalFile.$this->fileExtension)) {
+ $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
+ }
+ }
+ }
+}
@@ -0,0 +1,64 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver;
+
+use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver as BaseYamlDriver;
+
+/**
+ * YamlDriver that additionally looks for mapping information in a global file.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Kris Wallsmith <kris@symfony.com>
+ */
+class YamlDriver extends BaseYamlDriver
+{
+ protected $classCache;
+ protected $globalFile = 'mapping';
+ protected $fileExtension = '.mongodb.yml';
+
+ public function isTransient($className)
+ {
+ return !in_array($className, $this->getAllClassNames());
+ }
+
+ public function getAllClassNames()
+ {
+ if (null === $this->classCache) {
+ $this->initialize();
+ }
+
+ return array_merge(parent::getAllClassNames(), 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();
+ foreach ($this->paths as $path) {
+ if (file_exists($file = $path.'/'.$this->globalFile.$this->fileExtension)) {
+ $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
+ }
+ }
+ }
+}
@@ -35,8 +35,8 @@
<parameter key="doctrine.odm.mongodb.metadata.driver_chain.class">Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain</parameter>
<parameter key="doctrine.odm.mongodb.metadata.annotation.class">Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver</parameter>
<parameter key="doctrine.odm.mongodb.metadata.annotation_reader.class">Doctrine\Common\Annotations\AnnotationReader</parameter>
- <parameter key="doctrine.odm.mongodb.metadata.xml.class">Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver</parameter>
- <parameter key="doctrine.odm.mongodb.metadata.yml.class">Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver</parameter>
+ <parameter key="doctrine.odm.mongodb.metadata.xml.class">Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\XmlDriver</parameter>
+ <parameter key="doctrine.odm.mongodb.metadata.yml.class">Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\YamlDriver</parameter>
<!-- directories -->
<parameter key="doctrine.odm.mongodb.mapping_dirs" type="collection"></parameter>
@@ -44,8 +44,8 @@ public function testDependencyInjectionConfigurationDefaults()
$this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain', $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\Common\Annotations\AnnotationReader', $container->getParameter('doctrine.odm.mongodb.metadata.annotation_reader.class'));
- $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.xml.class'));
- $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.yml.class'));
+ $this->assertEquals('Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\XmlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.xml.class'));
+ $this->assertEquals('Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\YamlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.yml.class'));
$this->assertEquals('Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\UniqueValidator', $container->getParameter('doctrine_odm.mongodb.validator.unique.class'));

0 comments on commit d38f113

Please sign in to comment.