web/lib/Zend/Controller/Router/Route/Chain.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Controller
    16  * @package    Zend_Controller
    17  * @subpackage Router
    17  * @subpackage Router
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @version    $Id: Chain.php 23187 2010-10-20 18:42:37Z matthew $
    19  * @version    $Id: Chain.php 25249 2013-02-06 09:54:24Z frosch $
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    21  */
    21  */
    22 
    22 
    23 /** Zend_Controller_Router_Route_Abstract */
    23 /** Zend_Controller_Router_Route_Abstract */
    24 require_once 'Zend/Controller/Router/Route/Abstract.php';
    24 require_once 'Zend/Controller/Router/Route/Abstract.php';
    26 /**
    26 /**
    27  * Chain route is used for managing route chaining.
    27  * Chain route is used for managing route chaining.
    28  *
    28  *
    29  * @package    Zend_Controller
    29  * @package    Zend_Controller
    30  * @subpackage Router
    30  * @subpackage Router
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    31  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  */
    33  */
    34 class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Abstract
    34 class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Abstract
    35 {
    35 {
    36     protected $_routes = array();
    36     protected $_routes = array();
    37     protected $_separators = array();
    37     protected $_separators = array();
    38 
    38 
    39     /**
    39     /**
    40      * Instantiates route based on passed Zend_Config structure
    40      * Instantiates route based on passed Zend_Config structure
    41      *
    41      *
    42      * @param Zend_Config $config Configuration object
    42      * @param  Zend_Config $config Configuration object
       
    43      * @return Zend_Controller_Router_Route_Chain
    43      */
    44      */
    44     public static function getInstance(Zend_Config $config)
    45     public static function getInstance(Zend_Config $config)
    45     {
    46     {
    46         $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
    47         $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
    47         return new self($config->route, $defs);
    48         return new self($config->route, $defs);
    52      *
    53      *
    53      * @param  Zend_Controller_Router_Route_Abstract $route
    54      * @param  Zend_Controller_Router_Route_Abstract $route
    54      * @param  string                                $separator
    55      * @param  string                                $separator
    55      * @return Zend_Controller_Router_Route_Chain
    56      * @return Zend_Controller_Router_Route_Chain
    56      */
    57      */
    57     public function chain(Zend_Controller_Router_Route_Abstract $route, $separator = '/')
    58     public function chain(Zend_Controller_Router_Route_Abstract $route, $separator = self::URI_DELIMITER)
    58     {
    59     {
    59         $this->_routes[]     = $route;
    60         $this->_routes[]     = $route;
    60         $this->_separators[] = $separator;
    61         $this->_separators[] = $separator;
    61 
    62 
    62         return $this;
    63         return $this;
    66     /**
    67     /**
    67      * Matches a user submitted path with a previously defined route.
    68      * Matches a user submitted path with a previously defined route.
    68      * Assigns and returns an array of defaults on a successful match.
    69      * Assigns and returns an array of defaults on a successful match.
    69      *
    70      *
    70      * @param  Zend_Controller_Request_Http $request Request to get the path info from
    71      * @param  Zend_Controller_Request_Http $request Request to get the path info from
       
    72      * @param  null                         $partial
    71      * @return array|false An array of assigned values or a false on a mismatch
    73      * @return array|false An array of assigned values or a false on a mismatch
    72      */
    74      */
    73     public function match($request, $partial = null)
    75     public function match($request, $partial = null)
    74     {
    76     {
    75         $path    = trim($request->getPathInfo(), '/');
    77         $path        = trim($request->getPathInfo(), self::URI_DELIMITER);
    76         $subPath = $path;
    78         $subPath     = $path;
    77         $values  = array();
    79         $values      = array();
       
    80         $numRoutes   = count($this->_routes);
       
    81         $matchedPath = null;
    78 
    82 
    79         foreach ($this->_routes as $key => $route) {
    83         foreach ($this->_routes as $key => $route) {
    80             if ($key > 0 
    84             if ($key > 0
    81                 && $matchedPath !== null 
    85                 && $matchedPath !== null
    82                 && $subPath !== '' 
    86                 && $subPath !== ''
    83                 && $subPath !== false
    87                 && $subPath !== false
    84             ) {
    88             ) {
    85                 $separator = substr($subPath, 0, strlen($this->_separators[$key]));
    89                 $separator = substr($subPath, 0, strlen($this->_separators[$key]));
    86 
    90 
    87                 if ($separator !== $this->_separators[$key]) {
    91                 if ($separator !== $this->_separators[$key]) {
    97             } else {
   101             } else {
    98                 $request->setPathInfo($subPath);
   102                 $request->setPathInfo($subPath);
    99                 $match = $request;
   103                 $match = $request;
   100             }
   104             }
   101 
   105 
   102             $res = $route->match($match, true);
   106             $res = $route->match($match, true, ($key == $numRoutes - 1));
   103             if ($res === false) {
   107             if ($res === false) {
   104                 return false;
   108                 return false;
   105             }
   109             }
   106 
   110 
   107             $matchedPath = $route->getMatchedPath();
   111             $matchedPath = $route->getMatchedPath();
   124     }
   128     }
   125 
   129 
   126     /**
   130     /**
   127      * Assembles a URL path defined by this route
   131      * Assembles a URL path defined by this route
   128      *
   132      *
   129      * @param array $data An array of variable and value pairs used as parameters
   133      * @param  array $data An array of variable and value pairs used as parameters
       
   134      * @param  bool  $reset
       
   135      * @param  bool  $encode
   130      * @return string Route path with user submitted parameters
   136      * @return string Route path with user submitted parameters
   131      */
   137      */
   132     public function assemble($data = array(), $reset = false, $encode = false)
   138     public function assemble($data = array(), $reset = false, $encode = false)
   133     {
   139     {
   134         $value     = '';
   140         $value     = '';
   167             if (method_exists($route, 'setRequest')) {
   173             if (method_exists($route, 'setRequest')) {
   168                 $route->setRequest($request);
   174                 $route->setRequest($request);
   169             }
   175             }
   170         }
   176         }
   171     }
   177     }
   172 
   178     
       
   179     /**
       
   180      * Return a single parameter of route's defaults
       
   181      *
       
   182      * @param  string $name Array key of the parameter
       
   183      * @return string Previously set default
       
   184      */
       
   185     public function getDefault($name)
       
   186     {
       
   187         $default = null;
       
   188         foreach ($this->_routes as $route) {
       
   189             if (method_exists($route, 'getDefault')) {
       
   190                 $current = $route->getDefault($name);
       
   191                 if (null !== $current) {
       
   192                     $default = $current;
       
   193                 }
       
   194             }
       
   195         }
       
   196 
       
   197         return $default;
       
   198     }
       
   199 
       
   200     /**
       
   201      * Return an array of defaults
       
   202      *
       
   203      * @return array Route defaults
       
   204      */
       
   205     public function getDefaults()
       
   206     {
       
   207         $defaults = array();
       
   208         foreach ($this->_routes as $route) {
       
   209             if (method_exists($route, 'getDefaults')) {
       
   210                 $defaults = array_merge($defaults, $route->getDefaults());
       
   211             }
       
   212         }
       
   213 
       
   214         return $defaults;
       
   215     }
   173 }
   216 }