File indexing completed on 2024-06-23 05:55:09

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Controller
0017  * @subpackage Router
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_Controller_Router_Route_Abstract */
0024 // require_once 'Zend/Controller/Router/Route/Abstract.php';
0025 
0026 /**
0027  * Chain route is used for managing route chaining.
0028  *
0029  * @package    Zend_Controller
0030  * @subpackage Router
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Abstract
0035 {
0036 
0037     /**
0038      * Routes
0039      *
0040      * @var array
0041      */
0042     protected $_routes = array();
0043 
0044     /**
0045      * Separators
0046      *
0047      * @var array
0048      */
0049     protected $_separators = array();
0050 
0051     /**
0052      * Instantiates route based on passed Zend_Config structure
0053      *
0054      * @param  Zend_Config $config Configuration object
0055      * @return Zend_Controller_Router_Route_Chain
0056      */
0057     public static function getInstance(Zend_Config $config)
0058     {
0059         $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
0060 
0061         return new self($config->route, $defs);
0062     }
0063 
0064     /**
0065      * Add a route to this chain
0066      *
0067      * @param  Zend_Controller_Router_Route_Abstract $route
0068      * @param  string                                $separator
0069      * @return Zend_Controller_Router_Route_Chain
0070      */
0071     public function chain(Zend_Controller_Router_Route_Abstract $route, $separator = self::URI_DELIMITER)
0072     {
0073         $this->_routes[]     = $route;
0074         $this->_separators[] = $separator;
0075 
0076         return $this;
0077     }
0078 
0079     /**
0080      * Matches a user submitted path with a previously defined route.
0081      * Assigns and returns an array of defaults on a successful match.
0082      *
0083      * @param  Zend_Controller_Request_Http $request Request to get the path info from
0084      * @param  null                         $partial
0085      * @return array|false An array of assigned values or a false on a mismatch
0086      */
0087     public function match($request, $partial = null)
0088     {
0089         $rawPath     = $request->getPathInfo();
0090         $path        = trim($request->getPathInfo(), self::URI_DELIMITER);
0091         $subPath     = $path;
0092         $values      = array();
0093         $matchedPath = null;
0094 
0095         foreach ($this->_routes as $key => $route) {
0096             if ($key > 0
0097                 && $matchedPath !== null
0098                 && $subPath !== ''
0099                 && $subPath !== false
0100             ) {
0101                 $separator = substr($subPath, 0, strlen($this->_separators[$key]));
0102 
0103                 if ($separator !== $this->_separators[$key]) {
0104                     $request->setPathInfo($rawPath);
0105                     return false;
0106                 }
0107 
0108                 $subPath = substr($subPath, strlen($separator));
0109             }
0110             // TODO: Should be an interface method. Hack for 1.0 BC
0111             if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
0112                 $match = $subPath;
0113             } else {
0114                 $request->setPathInfo($subPath);
0115                 $match = $request;
0116             }
0117 
0118             $res = $route->match($match, true);
0119 
0120             if ($res === false) {
0121                 $request->setPathInfo($rawPath);
0122                 return false;
0123             }
0124 
0125             $matchedPath = $route->getMatchedPath();
0126 
0127             if ($matchedPath !== null) {
0128                 $subPath   = substr($subPath, strlen($matchedPath));
0129             }
0130 
0131             $values = $res + $values;
0132         }
0133 
0134         $request->setPathInfo($path);
0135 
0136         if ($subPath !== '' && $subPath !== false) {
0137             return false;
0138         }
0139 
0140         return $values;
0141     }
0142 
0143     /**
0144      * Assembles a URL path defined by this route
0145      *
0146      * @param  array $data An array of variable and value pairs used as parameters
0147      * @param  bool  $reset
0148      * @param  bool  $encode
0149      * @return string Route path with user submitted parameters
0150      */
0151     public function assemble($data = array(), $reset = false, $encode = false)
0152     {
0153         $value     = '';
0154         $numRoutes = count($this->_routes);
0155 
0156         foreach ($this->_routes as $key => $route) {
0157             if ($key > 0) {
0158                 $value .= $this->_separators[$key];
0159             }
0160 
0161             $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
0162 
0163             if (method_exists($route, 'getVariables')) {
0164                 $variables = $route->getVariables();
0165 
0166                 foreach ($variables as $variable) {
0167                     $data[$variable] = null;
0168                 }
0169             }
0170         }
0171 
0172         return $value;
0173     }
0174 
0175     /**
0176      * Set the request object for this and the child routes
0177      *
0178      * @param  Zend_Controller_Request_Abstract|null $request
0179      * @return void
0180      */
0181     public function setRequest(Zend_Controller_Request_Abstract $request = null)
0182     {
0183         $this->_request = $request;
0184 
0185         foreach ($this->_routes as $route) {
0186             if (method_exists($route, 'setRequest')) {
0187                 $route->setRequest($request);
0188             }
0189         }
0190     }
0191 
0192     /**
0193      * Return a single parameter of route's defaults
0194      *
0195      * @param  string $name Array key of the parameter
0196      * @return string Previously set default
0197      */
0198     public function getDefault($name)
0199     {
0200         $default = null;
0201         foreach ($this->_routes as $route) {
0202             if (method_exists($route, 'getDefault')) {
0203                 $current = $route->getDefault($name);
0204                 if (null !== $current) {
0205                     $default = $current;
0206                 }
0207             }
0208         }
0209 
0210         return $default;
0211     }
0212 
0213     /**
0214      * Return an array of defaults
0215      *
0216      * @return array Route defaults
0217      */
0218     public function getDefaults()
0219     {
0220         $defaults = array();
0221         foreach ($this->_routes as $route) {
0222             if (method_exists($route, 'getDefaults')) {
0223                 $defaults = array_merge($defaults, $route->getDefaults());
0224             }
0225         }
0226 
0227         return $defaults;
0228     }
0229 }