2
0
Просмотр исходного кода

ZF-9724: Allow changing dispatcher class via application configuration file

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24798 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 лет назад
Родитель
Сommit
7bea8ca2a2

+ 21 - 0
documentation/manual/en/module_specs/Zend_Application-AvailableResources-Frontcontroller.xml

@@ -98,6 +98,27 @@
                 boolean; by default, this is disabled.
             </para>
         </listitem>
+        
+        <listitem>
+            <para>
+                <emphasis><property>dispatcher</property></emphasis>: allows overriding the default 
+                dispatcher.  Has two subkeys, <property>class</property> (the classname of new dispatcher)
+                and <property>params</property>, an array of parameters to pass to the dispatcher constructor.
+            </para>
+            
+            <example id="zend.application.available-resources.frontcontroller.configExample">
+                <title>Overriding the dispatcher</title>
+
+                <programlisting language="ini"><![CDATA[
+        [production]
+        resources.frontController.dispatcher.class = "My_Custom_Dispatcher"
+        resources.frontController.dispatcher.params.foo = "bar"
+        resources.frontController.dispatcher.params.baz = "grok"
+        ]]></programlisting>
+            </example>
+            
+        </listitem>
+        
     </itemizedlist>
 
     <para>

+ 16 - 0
library/Zend/Application/Resource/Frontcontroller.php

@@ -135,6 +135,22 @@ class Zend_Application_Resource_Frontcontroller extends Zend_Application_Resourc
                     }
                     break;
 
+                case 'dispatcher':
+                    if(!isset($value['class'])) {
+                        require_once 'Zend/Application/Exception.php';
+                        throw new Zend_Application_Exception('You must specify both ');
+                    }
+                    if (!isset($value['params'])) {
+                        $value['params'] = array();
+                    }
+                    
+                    $dispatchClass = $value['class'];
+                    if(!class_exists($dispatchClass)) {
+                        require_once 'Zend/Application/Exception.php';
+                        throw new Zend_Application_Exception('Dispatcher class not found!');
+                    }
+                    $front->setDispatcher(new $dispatchClass((array)$value['params']));
+                    break;
                 default:
                     $front->setParam($key, $value);
                     break;

+ 23 - 0
tests/Zend/Application/Resource/FrontcontrollerTest.php

@@ -388,8 +388,31 @@ class Zend_Application_Resource_FrontcontrollerTest extends PHPUnit_Framework_Te
         $front = $resource->getFrontController();
         $this->assertTrue($front->returnResponse());
     }
+    
+    /**
+     * @group ZF-9724
+     */
+    public function testShouldSetDispatcherFromConfiguration()
+    {
+        require_once 'Zend/Application/Resource/Frontcontroller.php';
+        $resource = new Zend_Application_Resource_Frontcontroller(array(
+            'dispatcher' => array(
+                'class' => 'ZF9724_Dispatcher',
+                'params' => array(
+                    'bar' => 'baz'
+                )
+            )
+        ));
+        $resource->init();
+        $front = $resource->getFrontController();
+        $this->assertEquals('ZF9724_Dispatcher', get_class($front->getDispatcher()));
+        $this->assertEquals('baz', $front->getDispatcher()->getParam('bar'));
+    }
 }
 
+require_once 'Zend/Controller/Dispatcher/Standard.php';
+class ZF9724_Dispatcher extends Zend_Controller_Dispatcher_Standard {}
+
 if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_FrontcontrollerTest::main') {
     Zend_Application_Resource_FrontcontrollerTest::main();
 }