Explorar el Código

ZF-6803, ZF-7158: allow default module execution by modules resource

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17729 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew hace 16 años
padre
commit
0e17bbdff9

+ 25 - 8
library/Zend/Application/Resource/Modules.php

@@ -62,24 +62,41 @@ class Zend_Application_Resource_Modules extends Zend_Application_Resource_Resour
 
         $modules = $front->getControllerDirectory();
         $default = $front->getDefaultModule();
-        foreach (array_keys($modules) as $module) {
-            if ($module === $default) {
-                continue;
-            }
-
+        $curBootstrapClass = get_class($bootstrap);
+        foreach ($modules as $module => $moduleDirectory) {
             $bootstrapClass = $this->_formatModuleName($module) . '_Bootstrap';
             if (!class_exists($bootstrapClass, false)) {
-                $bootstrapPath  = $front->getModuleDirectory($module) . '/Bootstrap.php';
+                $bootstrapPath  = dirname($moduleDirectory) . '/Bootstrap.php';
                 if (file_exists($bootstrapPath)) {
+                    $eMsgTpl = 'Bootstrap file found for module "%s" but bootstrap class "%s" not found';
                     include_once $bootstrapPath;
-                    if (!class_exists($bootstrapClass, false)) {
-                        throw new Zend_Application_Resource_Exception('Bootstrap file found for module "' . $module . '" but bootstrap class "' . $bootstrapClass . '" not found');
+                    if (($default != $module)
+                        && !class_exists($bootstrapClass, false)
+                    ) {
+                        throw new Zend_Application_Resource_Exception(sprintf(
+                            $eMsgTpl, $module, $bootstrapClass
+                        ));
+                    } elseif ($default == $module) {
+                        if (!class_exists($bootstrapClass, false)) {
+                            $bootstrapClass = 'Bootstrap';
+                            if (!class_exists($bootstrapClass, false)) {
+                                throw new Zend_Application_Resource_Exception(sprintf(
+                                    $eMsgTpl, $module, $bootstrapClass
+                                ));
+                            }
+                        }
                     }
                 } else {
                     continue;
                 }
             }
 
+            if ($bootstrapClass == $curBootstrapClass) {
+                // If the found bootstrap class matches the one calling this 
+                // resource, don't re-execute.
+                continue;
+            }
+
             $moduleBootstrap = new $bootstrapClass($bootstrap);
             $moduleBootstrap->bootstrap();
             $this->_bootstraps[$module] = $moduleBootstrap;

+ 20 - 6
tests/Zend/Application/Resource/ModulesTest.php

@@ -101,7 +101,11 @@ class Zend_Application_Resource_ModulesTest extends PHPUnit_Framework_TestCase
         $this->assertFalse(isset($this->bootstrap->bar));
     }
 
-    public function testInitializationNeverTriggersDefaultModuleBootstrap()
+    /**
+     * @group ZF-6803
+     * @group ZF-7158
+     */
+    public function testInitializationTriggersDefaultModuleBootstrapWhenDiffersFromApplicationBootstrap()
     {
         require_once 'Zend/Application/Resource/Modules.php';
 
@@ -111,7 +115,7 @@ class Zend_Application_Resource_ModulesTest extends PHPUnit_Framework_TestCase
         $resource = new Zend_Application_Resource_Modules(array());
         $resource->setBootstrap($this->bootstrap);
         $resource->init();
-        $this->assertFalse(isset($this->bootstrap->default));
+        $this->assertTrue(isset($this->bootstrap->default));
     }
 
     public function testInitializationShouldTriggerModuleBootstrapsWhenTheyExist()
@@ -124,10 +128,14 @@ class Zend_Application_Resource_ModulesTest extends PHPUnit_Framework_TestCase
         $resource = new Zend_Application_Resource_Modules(array());
         $resource->setBootstrap($this->bootstrap);
         $resource->init();
-        $this->assertTrue($this->bootstrap->foo);
-        $this->assertTrue($this->bootstrap->bar);
+        $this->assertTrue($this->bootstrap->foo, 'foo failed');
+        $this->assertTrue($this->bootstrap->bar, 'bar failed');
     }
 
+    /**
+     * @group ZF-6803
+     * @group ZF-7158
+     */
     public function testInitializationShouldSkipModulesWithoutBootstraps()
     {
         require_once 'Zend/Application/Resource/Modules.php';
@@ -139,12 +147,17 @@ class Zend_Application_Resource_ModulesTest extends PHPUnit_Framework_TestCase
         $resource->setBootstrap($this->bootstrap);
         $resource->init();
         $bootstraps = $resource->getExecutedBootstraps();
-        $this->assertEquals(3, count((array)$bootstraps));
+        $this->assertEquals(4, count((array)$bootstraps));
         $this->assertArrayHasKey('bar',     (array)$bootstraps);
         $this->assertArrayHasKey('foo-bar', (array)$bootstraps);
         $this->assertArrayHasKey('foo',     (array)$bootstraps);
+        $this->assertArrayHasKey('default', (array)$bootstraps);
     }
 
+    /**
+     * @group ZF-6803
+     * @group ZF-7158
+     */
     public function testShouldReturnExecutedBootstrapsWhenComplete()
     {
         require_once 'Zend/Application/Resource/Modules.php';
@@ -155,10 +168,11 @@ class Zend_Application_Resource_ModulesTest extends PHPUnit_Framework_TestCase
         $resource = new Zend_Application_Resource_Modules(array());
         $resource->setBootstrap($this->bootstrap);
         $bootstraps = $resource->init();
-        $this->assertEquals(3, count((array)$bootstraps));
+        $this->assertEquals(4, count((array)$bootstraps));
         $this->assertArrayHasKey('bar',     (array)$bootstraps);
         $this->assertArrayHasKey('foo-bar', (array)$bootstraps);
         $this->assertArrayHasKey('foo',     (array)$bootstraps);
+        $this->assertArrayHasKey('default', (array)$bootstraps);
     }
 }