Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update CouchDB Bundle to latest Symfony gadgets and standalone testing

  • Loading branch information...
commit 66ebdb3a6fafd781304de9393f52af429f0b4268 1 parent 7c3393d
Benjamin Eberlei authored November 26, 2011
2  .gitignore
... ...
@@ -0,0 +1,2 @@
  1
+composer.lock
  2
+vendor
46  DoctrineCouchDBBundle.php
@@ -18,6 +18,7 @@
18 18
 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
19 19
 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 20
 use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
  21
+use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
21 22
 
22 23
 class DoctrineCouchDBBundle extends Bundle
23 24
 {
@@ -26,6 +27,7 @@ public function build(ContainerBuilder $container)
26 27
         parent::build($container);
27 28
 
28 29
         $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  30
+        $container->addCompilerPass(new DoctrineValidationPass('couchdb'));
29 31
     }
30 32
 
31 33
     public function boot()
@@ -33,5 +35,49 @@ public function boot()
33 35
         // force Doctrine annotations to be loaded
34 36
         // should be removed when a better solution is found in Doctrine
35 37
         class_exists('Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver');
  38
+        
  39
+        // Register an autoloader for proxies to avoid issues when unserializing them
  40
+        // when the ORM is used.
  41
+        if ($this->container->hasParameter('doctrine_couchdb.odm.proxy_namespace')) {
  42
+            $namespace = $this->container->getParameter('doctrine_couchdb.odm.proxy_namespace');
  43
+            $dir = $this->container->getParameter('doctrine_couchdb.odm.proxy_dir');
  44
+            $container = $this->container;
  45
+
  46
+            spl_autoload_register(function($class) use ($namespace, $dir, $container) {
  47
+                if (0 === strpos($class, $namespace)) {
  48
+                    $className = substr($class, strlen($namespace) +1);
  49
+                    $file = $dir.DIRECTORY_SEPARATOR.$className.'.php';
  50
+
  51
+                    if (!is_file($file) && $container->getParameter('kernel.debug')) {
  52
+                        $originalClassName = substr($className, 0, -5);
  53
+                        $registry = $container->get('doctrine_couchdb');
  54
+
  55
+                        // Tries to auto-generate the proxy file
  56
+                        foreach ($registry->getObjectManagers() as $manager) {
  57
+
  58
+                            if ($manager->getConfiguration()->getAutoGenerateProxyClasses()) {
  59
+                                $classes = $manager->getMetadataFactory()->getAllMetadata();
  60
+
  61
+                                foreach ($classes as $class) {
  62
+                                    $name = str_replace('\\', '', $class->name);
  63
+
  64
+                                    if ($name == $originalClassName) {
  65
+                                        $manager->getProxyFactory()->generateProxyClasses(array($class));
  66
+                                    }
  67
+                                }
  68
+                            }
  69
+                        }
  70
+
  71
+                        clearstatcache($file);
  72
+
  73
+                        if (!is_file($file)) {
  74
+                            throw new \RuntimeException(sprintf('The proxy file "%s" does not exist. If you still have objects serialized in the session, you need to clear the session manually.', $file));
  75
+                        }
  76
+                    }
  77
+
  78
+                    require $file;
  79
+                }
  80
+            });
  81
+        }
36 82
     }
37 83
 }
11  Tests/BundleTest.php
@@ -16,16 +16,23 @@
16 16
 
17 17
 class BundleTest extends TestCase
18 18
 {
19  
-    public function testRegisterEventListener()
  19
+    public function testRegisterCompilerPasses()
20 20
     {
21 21
         $bundle = new DoctrineCouchDBBundle();
22 22
         $builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder');
23  
-        $builder->expects($this->once())
  23
+        
  24
+        $builder->expects($this->at(0))
24 25
                 ->method('addCompilerPass')
25 26
                 ->with(
26 27
                     $this->isInstanceOf('Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass'),
27 28
                     $this->equalTo(PassConfig::TYPE_BEFORE_OPTIMIZATION)
28 29
                 );
  30
+        $builder->expects($this->at(1))
  31
+                ->method('addCompilerPass')
  32
+                ->with(
  33
+                    $this->isInstanceOf('Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass'),
  34
+                    $this->equalTo(PassConfig::TYPE_BEFORE_OPTIMIZATION)
  35
+                );
29 36
 
30 37
         $bundle->build($builder);
31 38
     }
2  Tests/DependencyInjection/ConfigurationTest.php
@@ -28,7 +28,7 @@ public function setUp()
28 28
     public function testEmptyConfig()
29 29
     {
30 30
         $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidConfigurationException',
31  
-            'The child node "client" at path "doctrine_couchdb" must be configured.');
  31
+            'The child node "client" at path "doctrine_couch_db" must be configured.');
32 32
         $config = $this->processor->processConfiguration($this->config, array());
33 33
     }
34 34
 
19  Tests/bootstrap.php
... ...
@@ -0,0 +1,19 @@
  1
+<?php
  2
+
  3
+if (!@include __DIR__ . '/../vendor/.composer/autoload.php') {
  4
+    die("You must set up the project dependencies, run the following commands:
  5
+wget http://getcomposer.org/composer.phar
  6
+php composer.phar install
  7
+");
  8
+}
  9
+
  10
+spl_autoload_register(function($class) {
  11
+    if (0 === strpos($class, 'Doctrine\\Bundle\\CouchDBBundle')) {
  12
+        $path = __DIR__.'/../'.implode('/', array_slice(explode('\\', $class), 3)).'.php';
  13
+        if (!stream_resolve_include_path($path)) {
  14
+            return false;
  15
+        }
  16
+        require_once $path;
  17
+        return true;
  18
+    }
  19
+});
13  composer.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
     "name": "doctrine/couchdb-odm-bundle",
3  
-    "type": "library",
  3
+    "type": "symfony-bundle",
4 4
     "description": "Symfony2 Doctrine CouchDB Bundle",
5 5
     "keywords": ["persistence", "couchdb", "symfony"],
6 6
     "homepage": "http://www.doctrine-project.org",
@@ -11,7 +11,12 @@
11 11
     ],
12 12
     "require": {
13 13
         "php": ">=5.3.2",
14  
-        "doctrine/couchdb-odm": ">=2.2.0",
15  
-        "symfony/framework-bundle": ">=2.1.0"
16  
-    }
  14
+        "doctrine/couchdb-odm": ">=2.0",
  15
+        "symfony/symfony": "2.1-dev",
  16
+        "symfony/doctrine-bridge": "2.1-dev"
  17
+    },
  18
+    "autoload": {
  19
+        "psr-0": { "Doctrine\\Bundle\\DoctrineCouchDBBundle": "" }
  20
+    },
  21
+    "target-dir": "Doctrine/Bundle/DoctrineCouchDBBundle"
17 22
 }
35  phpunit.xml.dist
... ...
@@ -0,0 +1,35 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+
  3
+<phpunit backupGlobals="false"
  4
+         backupStaticAttributes="false"
  5
+         colors="true"
  6
+         convertErrorsToExceptions="true"
  7
+         convertNoticesToExceptions="true"
  8
+         convertWarningsToExceptions="true"
  9
+         processIsolation="false"
  10
+         stopOnFailure="false"
  11
+         syntaxCheck="false"
  12
+         bootstrap="Tests/bootstrap.php"
  13
+>
  14
+    <testsuites>
  15
+        <testsuite name="DoctrineCouchDBBundle for the Symfony Framework">
  16
+            <directory>./Tests</directory>
  17
+        </testsuite>
  18
+    </testsuites>
  19
+
  20
+    <groups>
  21
+        <exclude>
  22
+            <group>benchmark</group>
  23
+        </exclude>
  24
+    </groups>
  25
+
  26
+    <filter>
  27
+        <whitelist>
  28
+            <directory>.</directory>
  29
+            <exclude>
  30
+                <directory>./Resources</directory>
  31
+                <directory>./Tests</directory>
  32
+            </exclude>
  33
+        </whitelist>
  34
+    </filter>
  35
+</phpunit>

0 notes on commit 66ebdb3

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