Browse Source

ZF-6545: fix recursive modules plugin resource issue

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@15505 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 years ago
parent
commit
3e0a405cc4

+ 4 - 2
library/Zend/Application.php

@@ -119,7 +119,7 @@ class Zend_Application
         $options = array_change_key_case($options, CASE_LOWER);
 
         if (!empty($options['config'])) {
-            $options += $this->_loadConfig($options['config']);
+            $options = array_merge($options, $this->_loadConfig($options['config']));
         }
         
         $this->_options = $options;
@@ -263,7 +263,9 @@ class Zend_Application
             $class = 'Bootstrap';
         }
 
-        require_once $path;
+        if (!class_exists($class)) {
+            require_once $path;
+        }
         $this->_bootstrap = new $class($this);
         
         return $this;

+ 1 - 1
library/Zend/Application/Bootstrap/BootstrapAbstract.php

@@ -398,7 +398,7 @@ abstract class Zend_Application_Bootstrap_BootstrapAbstract
         ) {
             $this->_application = $application;
         } else {
-            throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor');
+            throw new Zend_Application_Bootstrap_Exception('Invalid application provided to bootstrap constructor (received "' . get_class($application) . '" instance)');
         }
         return $this;
     }

+ 5 - 0
library/Zend/Application/Module/Bootstrap.php

@@ -66,6 +66,11 @@ abstract class Zend_Application_Module_Bootstrap
         }
 
         $this->initResourceLoader();
+
+        // ZF-6545: prevent recursive registration of modules
+        if ($this->hasPluginResource('Modules')) {
+            $this->unregisterPluginResource('Modules');
+        }
     }
 
     /**

+ 1 - 1
tests/Zend/Application/ApplicationTest.php

@@ -295,7 +295,7 @@ class Zend_Application_ApplicationTest extends PHPUnit_Framework_TestCase
     public function testPassingArrayOptionsWithConfigKeyShouldLoadOptionsAndOverride()
     {
         $application = new Zend_Application('testing', array('foo' => 'baz', 'config' => dirname(__FILE__) . '/_files/appconfig.inc'));
-        $this->assertEquals('baz', $application->getOption('foo'));
+        $this->assertEquals('bar', $application->getOption('foo'));
     }
 
     /**

+ 32 - 4
tests/Zend/Application/Module/BootstrapTest.php

@@ -145,11 +145,11 @@ class Zend_Application_Module_BootstrapTest extends PHPUnit_Framework_TestCase
                     'baseUrl'             => '/foo',
                     'controllerDirectory' => dirname(__FILE__),
                 ),
-                'bootstrap' => array(
-                    'path'  => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
-                    'class' => 'ZfAppBootstrap',
-                )
             ),
+            'bootstrap' => array(
+                'path'  => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
+                'class' => 'ZfAppBootstrap',
+            )
         ));
         $appBootstrap = $this->application->getBootstrap();
         $appBootstrap->bootstrap('FrontController');
@@ -161,6 +161,34 @@ class Zend_Application_Module_BootstrapTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('/foo', $test->getBaseUrl());
         $this->assertEquals(dirname(__FILE__), $test->getControllerDirectory('default'));
     }
+
+    /**
+     * @group ZF-6545
+     */
+    public function testModuleBootstrapsShouldNotAcceptModuleResourceInOrderToPreventRecursion()
+    {
+        require_once dirname(__FILE__) . '/../_files/ZfModuleBootstrap.php';
+        $this->application->setOptions(array(
+            'resources' => array(
+                'modules' => array(),
+                'frontController' => array(
+                    'baseUrl'             => '/foo',
+                    'moduleDirectory'     => dirname(__FILE__) . '/../_files/modules',
+                ),
+            ),
+            'bootstrap' => array(
+                'path'  => dirname(__FILE__) . '/../_files/ZfAppBootstrap.php',
+                'class' => 'ZfAppBootstrap',
+            )
+        ));
+        $appBootstrap = $this->application->getBootstrap();
+        $appBootstrap->bootstrap('Modules');
+        $modules = $appBootstrap->getResource('Modules');
+        foreach ($modules as $bootstrap) {
+            $resources = $bootstrap->getPluginResourceNames();
+            $this->assertFalse($bootstrap->hasPluginResource('Modules'), var_export($resources, 1));
+        }
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Module_BootstrapTest::main') {

+ 1 - 1
tests/Zend/Application/Resource/ResourceAbstractTest.php

@@ -120,7 +120,7 @@ class Zend_Application_Resource_ResourceAbstractTest extends PHPUnit_Framework_T
         $options3  = array(
             'foo' => 'BAR',
         );
-        $expected = $options1 + $options2 + $options3;
+        $expected = array_merge_recursive($options1, $options2, $options3);
         $resource->setOptions($options1)
                  ->setOptions($options2)
                  ->setOptions($options3);

+ 1 - 1
tests/Zend/Application/_files/modules/bar/Bootstrap.php

@@ -1,5 +1,5 @@
 <?php
-class Bar_Bootstrap extends Zend_Application_Bootstrap_BootstrapAbstract
+class Bar_Bootstrap extends Zend_Application_Module_Bootstrap
 {
     public $bootstrapped = false;
 

+ 1 - 1
tests/Zend/Application/_files/modules/foo/Bootstrap.php

@@ -1,5 +1,5 @@
 <?php
-class Foo_Bootstrap extends Zend_Application_Bootstrap_BootstrapAbstract
+class Foo_Bootstrap extends Zend_Application_Module_Bootstrap
 {
     public $bootstrapped = false;