web/lib/Zend/Controller/Router/Route/Module.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    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-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @version    $Id: Module.php 24593 2012-01-05 20:35:02Z matthew $
    19  * @version    $Id$
    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';
    28  *
    28  *
    29  * Default route for module functionality
    29  * Default route for module functionality
    30  *
    30  *
    31  * @package    Zend_Controller
    31  * @package    Zend_Controller
    32  * @subpackage Router
    32  * @subpackage Router
    33  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    33  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    35  * @see        http://manuals.rubyonrails.com/read/chapter/65
    35  * @see        http://manuals.rubyonrails.com/read/chapter/65
    36  */
    36  */
    37 class Zend_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Abstract
    37 class Zend_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Abstract
    38 {
    38 {
       
    39 
    39     /**
    40     /**
    40      * Default values for the route (ie. module, controller, action, params)
    41      * Default values for the route (ie. module, controller, action, params)
       
    42      *
    41      * @var array
    43      * @var array
    42      */
    44      */
    43     protected $_defaults;
    45     protected $_defaults;
    44 
    46 
    45     protected $_values      = array();
    47     /**
       
    48      * Default values for the route (ie. module, controller, action, params)
       
    49      *
       
    50      * @var array
       
    51      */
       
    52     protected $_values = array();
       
    53 
       
    54     /**
       
    55      * @var boolean
       
    56      */
    46     protected $_moduleValid = false;
    57     protected $_moduleValid = false;
    47     protected $_keysSet     = false;
    58 
       
    59     /**
       
    60      * @var boolean
       
    61      */
       
    62     protected $_keysSet = false;
    48 
    63 
    49     /**#@+
    64     /**#@+
    50      * Array keys to use for module, controller, and action. Should be taken out of request.
    65      * Array keys to use for module, controller, and action. Should be taken out of request.
       
    66      *
    51      * @var string
    67      * @var string
    52      */
    68      */
    53     protected $_moduleKey     = 'module';
    69     protected $_moduleKey     = 'module';
    54     protected $_controllerKey = 'controller';
    70     protected $_controllerKey = 'controller';
    55     protected $_actionKey     = 'action';
    71     protected $_actionKey     = 'action';
    63     /**
    79     /**
    64      * @var Zend_Controller_Request_Abstract
    80      * @var Zend_Controller_Request_Abstract
    65      */
    81      */
    66     protected $_request;
    82     protected $_request;
    67 
    83 
    68     public function getVersion() {
    84     /**
       
    85      * Get the version of the route
       
    86      *
       
    87      * @return int
       
    88      */
       
    89     public function getVersion()
       
    90     {
    69         return 1;
    91         return 1;
    70     }
    92     }
    71 
    93 
    72     /**
    94     /**
    73      * Instantiates route based on passed Zend_Config structure
    95      * Instantiates route based on passed Zend_Config structure
       
    96      *
       
    97      * @param Zend_Config $config
       
    98      * @return Zend_Controller_Router_Route_Module
    74      */
    99      */
    75     public static function getInstance(Zend_Config $config)
   100     public static function getInstance(Zend_Config $config)
    76     {
   101     {
    77         $frontController = Zend_Controller_Front::getInstance();
   102         $frontController = Zend_Controller_Front::getInstance();
    78 
   103 
    84     }
   109     }
    85 
   110 
    86     /**
   111     /**
    87      * Constructor
   112      * Constructor
    88      *
   113      *
    89      * @param array $defaults Defaults for map variables with keys as variable names
   114      * @param array                                $defaults   Defaults for map variables with keys as variable names
    90      * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
   115      * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
    91      * @param Zend_Controller_Request_Abstract $request Request object
   116      * @param Zend_Controller_Request_Abstract     $request    Request object
    92      */
   117      */
    93     public function __construct(array $defaults = array(),
   118     public function __construct(
    94                 Zend_Controller_Dispatcher_Interface $dispatcher = null,
   119         array $defaults = array(),
    95                 Zend_Controller_Request_Abstract $request = null)
   120         Zend_Controller_Dispatcher_Interface $dispatcher = null,
       
   121         Zend_Controller_Request_Abstract $request = null
       
   122     )
    96     {
   123     {
    97         $this->_defaults = $defaults;
   124         $this->_defaults = $defaults;
    98 
   125 
    99         if (isset($request)) {
   126         if (isset($request)) {
   100             $this->_request = $request;
   127             $this->_request = $request;
   135      *
   162      *
   136      * If a request object is registered, it uses its setModuleName(),
   163      * If a request object is registered, it uses its setModuleName(),
   137      * setControllerName(), and setActionName() accessors to set those values.
   164      * setControllerName(), and setActionName() accessors to set those values.
   138      * Always returns the values as an array.
   165      * Always returns the values as an array.
   139      *
   166      *
   140      * @param string $path Path used to match against this routing map
   167      * @param string  $path Path used to match against this routing map
       
   168      * @param boolean $partial
   141      * @return array An array of assigned values or a false on a mismatch
   169      * @return array An array of assigned values or a false on a mismatch
   142      */
   170      */
   143     public function match($path, $partial = false)
   171     public function match($path, $partial = false)
   144     {
   172     {
   145         $this->_setRequestKeys();
   173         $this->_setRequestKeys();
   156         if ($path != '') {
   184         if ($path != '') {
   157             $path = explode(self::URI_DELIMITER, $path);
   185             $path = explode(self::URI_DELIMITER, $path);
   158 
   186 
   159             if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
   187             if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
   160                 $values[$this->_moduleKey] = array_shift($path);
   188                 $values[$this->_moduleKey] = array_shift($path);
   161                 $this->_moduleValid = true;
   189                 $this->_moduleValid        = true;
   162             }
   190             }
   163 
   191 
   164             if (count($path) && !empty($path[0])) {
   192             if (count($path) && !empty($path[0])) {
   165                 $values[$this->_controllerKey] = array_shift($path);
   193                 $values[$this->_controllerKey] = array_shift($path);
   166             }
   194             }
   169                 $values[$this->_actionKey] = array_shift($path);
   197                 $values[$this->_actionKey] = array_shift($path);
   170             }
   198             }
   171 
   199 
   172             if ($numSegs = count($path)) {
   200             if ($numSegs = count($path)) {
   173                 for ($i = 0; $i < $numSegs; $i = $i + 2) {
   201                 for ($i = 0; $i < $numSegs; $i = $i + 2) {
   174                     $key = urldecode($path[$i]);
   202                     $key          = urldecode($path[$i]);
   175                     $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
   203                     $val          = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
   176                     $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
   204                     $params[$key] = (isset($params[$key]) ? (array_merge((array)$params[$key], array($val))) : $val);
   177                 }
   205                 }
   178             }
   206             }
   179         }
   207         }
   180 
   208 
   181         if ($partial) {
   209         if ($partial) {
   188     }
   216     }
   189 
   217 
   190     /**
   218     /**
   191      * Assembles user submitted parameters forming a URL path defined by this route
   219      * Assembles user submitted parameters forming a URL path defined by this route
   192      *
   220      *
   193      * @param array $data An array of variable and value pairs used as parameters
   221      * @param array   $data  An array of variable and value pairs used as parameters
   194      * @param bool $reset Weither to reset the current params
   222      * @param boolean $reset Weither to reset the current params
       
   223      * @param boolean $encode
       
   224      * @param boolean $partial
   195      * @return string Route path with user submitted parameters
   225      * @return string Route path with user submitted parameters
   196      */
   226      */
   197     public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
   227     public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
   198     {
   228     {
   199         if (!$this->_keysSet) {
   229         if (!$this->_keysSet) {
   234                     $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
   264                     $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
   235                     $url .= self::URI_DELIMITER . $key;
   265                     $url .= self::URI_DELIMITER . $key;
   236                     $url .= self::URI_DELIMITER . $arrayValue;
   266                     $url .= self::URI_DELIMITER . $arrayValue;
   237                 }
   267                 }
   238             } else {
   268             } else {
   239                 if ($encode) $value = urlencode($value);
   269                 if ($encode) {
       
   270                     $value = urlencode($value);
       
   271                 }
   240                 $url .= self::URI_DELIMITER . $key;
   272                 $url .= self::URI_DELIMITER . $key;
   241                 $url .= self::URI_DELIMITER . $value;
   273                 $url .= self::URI_DELIMITER . $value;
   242             }
   274             }
   243         }
   275         }
   244 
   276 
   245         if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
   277         if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
   246             if ($encode) $action = urlencode($action);
   278             if ($encode) {
       
   279                 $action = urlencode($action);
       
   280             }
   247             $url = self::URI_DELIMITER . $action . $url;
   281             $url = self::URI_DELIMITER . $action . $url;
   248         }
   282         }
   249 
   283 
   250         if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
   284         if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
   251             if ($encode) $controller = urlencode($controller);
   285             if ($encode) {
       
   286                 $controller = urlencode($controller);
       
   287             }
   252             $url = self::URI_DELIMITER . $controller . $url;
   288             $url = self::URI_DELIMITER . $controller . $url;
   253         }
   289         }
   254 
   290 
   255         if (isset($module)) {
   291         if (isset($module)) {
   256             if ($encode) $module = urlencode($module);
   292             if ($encode) {
       
   293                 $module = urlencode($module);
       
   294             }
   257             $url = self::URI_DELIMITER . $module . $url;
   295             $url = self::URI_DELIMITER . $module . $url;
   258         }
   296         }
   259 
   297 
   260         return ltrim($url, self::URI_DELIMITER);
   298         return ltrim($url, self::URI_DELIMITER);
   261     }
   299     }
   264      * Return a single parameter of route's defaults
   302      * Return a single parameter of route's defaults
   265      *
   303      *
   266      * @param string $name Array key of the parameter
   304      * @param string $name Array key of the parameter
   267      * @return string Previously set default
   305      * @return string Previously set default
   268      */
   306      */
   269     public function getDefault($name) {
   307     public function getDefault($name)
       
   308     {
   270         if (isset($this->_defaults[$name])) {
   309         if (isset($this->_defaults[$name])) {
   271             return $this->_defaults[$name];
   310             return $this->_defaults[$name];
   272         }
   311         }
   273     }
   312     }
   274 
   313 
   275     /**
   314     /**
   276      * Return an array of defaults
   315      * Return an array of defaults
   277      *
   316      *
   278      * @return array Route defaults
   317      * @return array Route defaults
   279      */
   318      */
   280     public function getDefaults() {
   319     public function getDefaults()
       
   320     {
   281         return $this->_defaults;
   321         return $this->_defaults;
   282     }
   322     }
   283 
       
   284 }
   323 }