Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added support for global mapping files

  • Loading branch information...
commit d38f1136ff3dff7d13b38db2e10630809719fcf5 1 parent 7252272
Kris Wallsmith authored
64  Mapping/Driver/XmlDriver.php
... ...
@@ -0,0 +1,64 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * This file is part of the Symfony package.
  5
+ *
  6
+ * (c) Fabien Potencier <fabien@symfony.com>
  7
+ *
  8
+ * For the full copyright and license information, please view the LICENSE
  9
+ * file that was distributed with this source code.
  10
+ */
  11
+
  12
+namespace Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver;
  13
+
  14
+use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver as BaseXmlDriver;
  15
+
  16
+/**
  17
+ * XmlDriver that additionally looks for mapping information in a global file.
  18
+ *
  19
+ * @author Fabien Potencier <fabien@symfony.com>
  20
+ * @author Kris Wallsmith <kris@symfony.com>
  21
+ */
  22
+class XmlDriver extends BaseXmlDriver
  23
+{
  24
+    protected $classCache;
  25
+    protected $globalFile = 'mapping';
  26
+    protected $fileExtension = '.mongodb.xml';
  27
+
  28
+    public function isTransient($className)
  29
+    {
  30
+        return !in_array($className, $this->getAllClassNames());
  31
+    }
  32
+
  33
+    public function getAllClassNames()
  34
+    {
  35
+        if (null === $this->classCache) {
  36
+            $this->initialize();
  37
+        }
  38
+
  39
+        return array_merge(parent::getAllClassNames(), array_keys($this->classCache));
  40
+    }
  41
+
  42
+    public function getElement($className)
  43
+    {
  44
+        if (null === $this->classCache) {
  45
+            $this->initialize();
  46
+        }
  47
+
  48
+        if (!isset($this->classCache[$className])) {
  49
+            $this->classCache[$className] = parent::getElement($className);
  50
+        }
  51
+
  52
+        return $this->classCache[$className];
  53
+    }
  54
+
  55
+    protected function initialize()
  56
+    {
  57
+        $this->classCache = array();
  58
+        foreach ($this->paths as $path) {
  59
+            if (file_exists($file = $path.'/'.$this->globalFile.$this->fileExtension)) {
  60
+                $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
  61
+            }
  62
+        }
  63
+    }
  64
+}
64  Mapping/Driver/YamlDriver.php
... ...
@@ -0,0 +1,64 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * This file is part of the Symfony package.
  5
+ *
  6
+ * (c) Fabien Potencier <fabien@symfony.com>
  7
+ *
  8
+ * For the full copyright and license information, please view the LICENSE
  9
+ * file that was distributed with this source code.
  10
+ */
  11
+
  12
+namespace Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver;
  13
+
  14
+use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver as BaseYamlDriver;
  15
+
  16
+/**
  17
+ * YamlDriver that additionally looks for mapping information in a global file.
  18
+ *
  19
+ * @author Fabien Potencier <fabien@symfony.com>
  20
+ * @author Kris Wallsmith <kris@symfony.com>
  21
+ */
  22
+class YamlDriver extends BaseYamlDriver
  23
+{
  24
+    protected $classCache;
  25
+    protected $globalFile = 'mapping';
  26
+    protected $fileExtension = '.mongodb.yml';
  27
+
  28
+    public function isTransient($className)
  29
+    {
  30
+        return !in_array($className, $this->getAllClassNames());
  31
+    }
  32
+
  33
+    public function getAllClassNames()
  34
+    {
  35
+        if (null === $this->classCache) {
  36
+            $this->initialize();
  37
+        }
  38
+
  39
+        return array_merge(parent::getAllClassNames(), array_keys($this->classCache));
  40
+    }
  41
+
  42
+    public function getElement($className)
  43
+    {
  44
+        if (null === $this->classCache) {
  45
+            $this->initialize();
  46
+        }
  47
+
  48
+        if (!isset($this->classCache[$className])) {
  49
+            $this->classCache[$className] = parent::getElement($className);
  50
+        }
  51
+
  52
+        return $this->classCache[$className];
  53
+    }
  54
+
  55
+    protected function initialize()
  56
+    {
  57
+        $this->classCache = array();
  58
+        foreach ($this->paths as $path) {
  59
+            if (file_exists($file = $path.'/'.$this->globalFile.$this->fileExtension)) {
  60
+                $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
  61
+            }
  62
+        }
  63
+    }
  64
+}
4  Resources/config/mongodb.xml
@@ -35,8 +35,8 @@
35 35
         <parameter key="doctrine.odm.mongodb.metadata.driver_chain.class">Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain</parameter>
36 36
         <parameter key="doctrine.odm.mongodb.metadata.annotation.class">Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver</parameter>
37 37
         <parameter key="doctrine.odm.mongodb.metadata.annotation_reader.class">Doctrine\Common\Annotations\AnnotationReader</parameter>
38  
-        <parameter key="doctrine.odm.mongodb.metadata.xml.class">Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver</parameter>
39  
-        <parameter key="doctrine.odm.mongodb.metadata.yml.class">Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver</parameter>
  38
+        <parameter key="doctrine.odm.mongodb.metadata.xml.class">Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\XmlDriver</parameter>
  39
+        <parameter key="doctrine.odm.mongodb.metadata.yml.class">Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\YamlDriver</parameter>
40 40
 
41 41
         <!-- directories -->
42 42
         <parameter key="doctrine.odm.mongodb.mapping_dirs" type="collection"></parameter>
4  Tests/DependencyInjection/AbstractMongoDBExtensionTest.php
@@ -44,8 +44,8 @@ public function testDependencyInjectionConfigurationDefaults()
44 44
         $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain', $container->getParameter('doctrine.odm.mongodb.metadata.driver_chain.class'));
45 45
         $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver', $container->getParameter('doctrine.odm.mongodb.metadata.annotation.class'));
46 46
         $this->assertEquals('Doctrine\Common\Annotations\AnnotationReader', $container->getParameter('doctrine.odm.mongodb.metadata.annotation_reader.class'));
47  
-        $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.xml.class'));
48  
-        $this->assertEquals('Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.yml.class'));
  47
+        $this->assertEquals('Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\XmlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.xml.class'));
  48
+        $this->assertEquals('Symfony\Bundle\DoctrineMongoDBBundle\Mapping\Driver\YamlDriver', $container->getParameter('doctrine.odm.mongodb.metadata.yml.class'));
49 49
 
50 50
         $this->assertEquals('Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\UniqueValidator', $container->getParameter('doctrine_odm.mongodb.validator.unique.class'));
51 51
 

0 notes on commit d38f113

Please sign in to comment.
Something went wrong with that request. Please try again.