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

[ZF-6758] Merged patch for chainNameSeparators into trunk

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16644 44c647ce-9c0f-0410-b52a-842ac1e357ba
dasprid 16 лет назад
Родитель
Сommit
e2bc325c71

+ 38 - 2
documentation/manual/en/module_specs/Zend_Controller-Router-Route-Chain.xml

@@ -49,7 +49,7 @@ $chainedRoute = $hostnameRoute->chain($pathRoute);
 ]]></programlisting>
 ]]></programlisting>
 
 
     <para>
     <para>
-        When chaining routes together, their default separator is a slash
+        When chaining routes together, their separator is a slash
         by default. There may be cases when you want to have a different
         by default. There may be cases when you want to have a different
         separator:
         separator:
     </para>
     </para>
@@ -77,7 +77,7 @@ echo $chainedRoute->assemble();
             parent- nor the child-route will be added directly to the router but
             parent- nor the child-route will be added directly to the router but
             only the resulting chained route. The name of the chained route in
             only the resulting chained route. The name of the chained route in
             the router will be the parent route name and the child route name
             the router will be the parent route name and the child route name
-            concatenated with a dash (-). A simple config in XML would look
+            concatenated with a dash (-) by default. A simple config in XML would look
             like this:
             like this:
         </para>
         </para>
 
 
@@ -177,6 +177,42 @@ echo $chainedRoute->assemble();
     </www-imprint>
     </www-imprint>
 </routes>
 </routes>
 ]]></programlisting>
 ]]></programlisting>
+        <para>
+            When you configure chain routes with <classname>Zend_Config</classname> and
+            want the chain name separator to be different from a dash, you
+            need to specify this separator separately:
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$config = new Zend_Config(array(
+    'chainName' => array(
+        'type'   => 'Zend_Controller_Router_Route_Static',
+        'route'  => 'foo',
+        'chains' => array(
+            'subRouteName' => array(
+                'type'     => 'Zend_Controller_Router_Route_Static',
+                'route'    => 'bar',
+                'defaults' => array(
+                    'module'      => 'module',
+                     'controller' => 'controller',
+                     'action'     => 'action'
+                )
+            )
+        )
+    )
+));
+
+// Set separator before adding config
+$router->setChainNameSeparator('_separator_')
+
+// Add config
+$outer->addConfig($config);
+
+// The name of our route now is: chainName_separator_subRouteName
+echo $this->_router->assemble(array(), 'chainName_separator_subRouteName');
+
+// The proof: it echoes /foo/bar
+]]></programlisting>
     </sect4>
     </sect4>
 </sect3>
 </sect3>
 <!--
 <!--

+ 8 - 4
library/Zend/Application/Resource/Router.php

@@ -27,7 +27,6 @@
  * @category   Zend
  * @category   Zend
  * @package    Zend_Application
  * @package    Zend_Application
  * @subpackage Resource
  * @subpackage Resource
- * @author     Dolf Schimmel
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
  */
@@ -59,14 +58,19 @@ class Zend_Application_Resource_Router
         if (null === $this->_router) {
         if (null === $this->_router) {
             $bootstrap = $this->getBootstrap();
             $bootstrap = $this->getBootstrap();
             $bootstrap->bootstrap('FrontController');
             $bootstrap->bootstrap('FrontController');
-            $front = $bootstrap->getContainer()->frontcontroller;
+            $this->_router = $bootstrap->getContainer()->frontcontroller->getRouter();
 
 
             $options = $this->getOptions();
             $options = $this->getOptions();
-            if(!isset($options['routes'])) {
+            if (!isset($options['routes'])) {
                 $options['routes'] = array();
                 $options['routes'] = array();
             }
             }
+            
+
+            if (isset($options['chainNameSeparator'])) {
+                $this->_router->setChainNameSeparator($options['chainNameSeparator']);
+            }
+            
 
 
-            $this->_router = $front->getRouter();
             $this->_router->addConfig(new Zend_Config($options['routes']));
             $this->_router->addConfig(new Zend_Config($options['routes']));
         }
         }
         return $this->_router;
         return $this->_router;

+ 29 - 1
library/Zend/Controller/Router/Rewrite.php

@@ -67,6 +67,13 @@ class Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Abstract
     protected $_globalParams = array();
     protected $_globalParams = array();
     
     
     /**
     /**
+     * Separator to use with chain names
+     * 
+     * @var string
+     */
+    protected $_chainNameSeparator = '-';
+    
+    /**
      * Add default routes which are used to mimic basic router behaviour
      * Add default routes which are used to mimic basic router behaviour
      * 
      * 
      * @return Zend_Controller_Router_Rewrite
      * @return Zend_Controller_Router_Rewrite
@@ -236,7 +243,7 @@ class Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Abstract
                 $chainRoute = $route->chain($childRoute);
                 $chainRoute = $route->chain($childRoute);
             }
             }
             
             
-            $chainName = $name . '-' . $childRouteName;
+            $chainName = $name . $this->_chainNameSeparator . $childRouteName;
             
             
             if (isset($childRouteInfo->chains)) {
             if (isset($childRouteInfo->chains)) {
                 $this->_addChainRoutesFromConfig($chainName, $chainRoute, $childRouteInfo->chains);
                 $this->_addChainRoutesFromConfig($chainName, $chainRoute, $childRouteInfo->chains);
@@ -453,4 +460,25 @@ class Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Abstract
     
     
         return $this;
         return $this;
     }
     }
+    
+    /**
+     * Set the separator to use with chain names
+     * 
+     * @param  string $separator The separator to use
+     * @return Zend_Controller_Router_Rewrite
+     */
+    public function setChainNameSeparator($separator) {
+    	$this->_chainNameSeparator = $separator;
+    	
+    	return $this;
+    }
+    
+    /**
+     * Get the separator to use for chain names
+     * 
+     * @return string
+     */
+    public function getChainNameSeparator() {
+        return $this->_chainNameSeparator;
+    }
 }
 }

+ 8 - 2
tests/Zend/Application/Resource/RouterTest.php

@@ -61,9 +61,7 @@ class Zend_Application_Resource_RouterTest extends PHPUnit_Framework_TestCase
 
 
         Zend_Loader_Autoloader::resetInstance();
         Zend_Loader_Autoloader::resetInstance();
         $this->autoloader = Zend_Loader_Autoloader::getInstance();
         $this->autoloader = Zend_Loader_Autoloader::getInstance();
-
         $this->application = new Zend_Application('testing');
         $this->application = new Zend_Application('testing');
-
         $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
         $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
 
 
         Zend_Controller_Front::getInstance()->resetInstance();
         Zend_Controller_Front::getInstance()->resetInstance();
@@ -100,6 +98,14 @@ class Zend_Application_Resource_RouterTest extends PHPUnit_Framework_TestCase
         $test = $resource->init();
         $test = $resource->init();
         $this->assertTrue($test instanceof Zend_Controller_Router_Rewrite);
         $this->assertTrue($test instanceof Zend_Controller_Router_Rewrite);
     }
     }
+    
+    public function testChainNameSeparatorIsParsedOnToRouter()
+    {
+        $resource = new Zend_Application_Resource_Router(array('chainNameSeparator' => '_unitTestSep_'));
+        $resource->setBootstrap($this->bootstrap);
+        $router = $resource->init();
+        $this->assertEquals('_unitTestSep_', $router->getChainNameSeparator());
+    }
 
 
     public function testOptionsPassedToResourceAreUsedToCreateRoutes()
     public function testOptionsPassedToResourceAreUsedToCreateRoutes()
     {
     {

+ 29 - 0
tests/Zend/Controller/Router/RewriteTest.php

@@ -652,6 +652,35 @@ class Zend_Controller_Router_RewriteTest extends PHPUnit_Framework_TestCase
         
         
         $this->assertEquals('/en/articles/1', $url);
         $this->assertEquals('/en/articles/1', $url);
     }
     }
+    
+    public function testChainNameSeparatorIsSetCorrectly() {
+        $separators = array('_','unitTestSeparator','-');
+        $results = array();
+
+        foreach($separators as $separator) {
+            $this->_router->setChainNameSeparator($separator);
+            $results[] = $this->_router->getChainNameSeparator();	
+        }
+
+        $this->assertEquals($separators, $results);
+    }
+    
+    public function testChainNameSeparatorisUsedCorrectly() {
+        $config = new Zend_Config(array('chains' => array(
+            'type'=>'Zend_Controller_Router_Route_Static',
+            'route'=>'foo',
+            'chains'=> array('bar'=>
+                array('type'=>'Zend_Controller_Router_Route_Static',
+                    'route'=>'bar',
+                    'defaults'=>array(
+                    'module'=>'module',
+                    'controller'=>'controller',
+                    'action'=>'action'))))));
+        $this->_router->setChainNameSeparator('_separator_')
+                      ->addConfig($config);
+        $url = $this->_router->assemble(array(),'chains_separator_bar');
+        $this->assertEquals('/foo/bar',$url);
+    }
 }
 }