web/lib/Zend/Controller/Router/Route/Chain.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Controller
       
    17  * @subpackage Router
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @version    $Id: Chain.php 23187 2010-10-20 18:42:37Z matthew $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /** Zend_Controller_Router_Route_Abstract */
       
    24 require_once 'Zend/Controller/Router/Route/Abstract.php';
       
    25 
       
    26 /**
       
    27  * Chain route is used for managing route chaining.
       
    28  *
       
    29  * @package    Zend_Controller
       
    30  * @subpackage Router
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Abstract
       
    35 {
       
    36     protected $_routes = array();
       
    37     protected $_separators = array();
       
    38 
       
    39     /**
       
    40      * Instantiates route based on passed Zend_Config structure
       
    41      *
       
    42      * @param Zend_Config $config Configuration object
       
    43      */
       
    44     public static function getInstance(Zend_Config $config)
       
    45     {
       
    46         $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
       
    47         return new self($config->route, $defs);
       
    48     }
       
    49 
       
    50     /**
       
    51      * Add a route to this chain
       
    52      *
       
    53      * @param  Zend_Controller_Router_Route_Abstract $route
       
    54      * @param  string                                $separator
       
    55      * @return Zend_Controller_Router_Route_Chain
       
    56      */
       
    57     public function chain(Zend_Controller_Router_Route_Abstract $route, $separator = '/')
       
    58     {
       
    59         $this->_routes[]     = $route;
       
    60         $this->_separators[] = $separator;
       
    61 
       
    62         return $this;
       
    63 
       
    64     }
       
    65 
       
    66     /**
       
    67      * Matches a user submitted path with a previously defined route.
       
    68      * Assigns and returns an array of defaults on a successful match.
       
    69      *
       
    70      * @param  Zend_Controller_Request_Http $request Request to get the path info from
       
    71      * @return array|false An array of assigned values or a false on a mismatch
       
    72      */
       
    73     public function match($request, $partial = null)
       
    74     {
       
    75         $path    = trim($request->getPathInfo(), '/');
       
    76         $subPath = $path;
       
    77         $values  = array();
       
    78 
       
    79         foreach ($this->_routes as $key => $route) {
       
    80             if ($key > 0 
       
    81                 && $matchedPath !== null 
       
    82                 && $subPath !== '' 
       
    83                 && $subPath !== false
       
    84             ) {
       
    85                 $separator = substr($subPath, 0, strlen($this->_separators[$key]));
       
    86 
       
    87                 if ($separator !== $this->_separators[$key]) {
       
    88                     return false;
       
    89                 }
       
    90 
       
    91                 $subPath = substr($subPath, strlen($separator));
       
    92             }
       
    93 
       
    94             // TODO: Should be an interface method. Hack for 1.0 BC
       
    95             if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
       
    96                 $match = $subPath;
       
    97             } else {
       
    98                 $request->setPathInfo($subPath);
       
    99                 $match = $request;
       
   100             }
       
   101 
       
   102             $res = $route->match($match, true);
       
   103             if ($res === false) {
       
   104                 return false;
       
   105             }
       
   106 
       
   107             $matchedPath = $route->getMatchedPath();
       
   108 
       
   109             if ($matchedPath !== null) {
       
   110                 $subPath     = substr($subPath, strlen($matchedPath));
       
   111                 $separator   = substr($subPath, 0, strlen($this->_separators[$key]));
       
   112             }
       
   113 
       
   114             $values = $res + $values;
       
   115         }
       
   116 
       
   117         $request->setPathInfo($path);
       
   118 
       
   119         if ($subPath !== '' && $subPath !== false) {
       
   120             return false;
       
   121         }
       
   122 
       
   123         return $values;
       
   124     }
       
   125 
       
   126     /**
       
   127      * Assembles a URL path defined by this route
       
   128      *
       
   129      * @param array $data An array of variable and value pairs used as parameters
       
   130      * @return string Route path with user submitted parameters
       
   131      */
       
   132     public function assemble($data = array(), $reset = false, $encode = false)
       
   133     {
       
   134         $value     = '';
       
   135         $numRoutes = count($this->_routes);
       
   136 
       
   137         foreach ($this->_routes as $key => $route) {
       
   138             if ($key > 0) {
       
   139                 $value .= $this->_separators[$key];
       
   140             }
       
   141 
       
   142             $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
       
   143 
       
   144             if (method_exists($route, 'getVariables')) {
       
   145                 $variables = $route->getVariables();
       
   146 
       
   147                 foreach ($variables as $variable) {
       
   148                     $data[$variable] = null;
       
   149                 }
       
   150             }
       
   151         }
       
   152 
       
   153         return $value;
       
   154     }
       
   155 
       
   156     /**
       
   157      * Set the request object for this and the child routes
       
   158      *
       
   159      * @param  Zend_Controller_Request_Abstract|null $request
       
   160      * @return void
       
   161      */
       
   162     public function setRequest(Zend_Controller_Request_Abstract $request = null)
       
   163     {
       
   164         $this->_request = $request;
       
   165 
       
   166         foreach ($this->_routes as $route) {
       
   167             if (method_exists($route, 'setRequest')) {
       
   168                 $route->setRequest($request);
       
   169             }
       
   170         }
       
   171     }
       
   172 
       
   173 }