web/enmi/Zend/Controller/Router/Route/Module.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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: Module.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    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  * Module Route
       
    28  *
       
    29  * Default route for module functionality
       
    30  *
       
    31  * @package    Zend_Controller
       
    32  * @subpackage Router
       
    33  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    35  * @see        http://manuals.rubyonrails.com/read/chapter/65
       
    36  */
       
    37 class Zend_Controller_Router_Route_Module extends Zend_Controller_Router_Route_Abstract
       
    38 {
       
    39     /**
       
    40      * URI delimiter
       
    41      */
       
    42     const URI_DELIMITER = '/';
       
    43 
       
    44     /**
       
    45      * Default values for the route (ie. module, controller, action, params)
       
    46      * @var array
       
    47      */
       
    48     protected $_defaults;
       
    49 
       
    50     protected $_values      = array();
       
    51     protected $_moduleValid = false;
       
    52     protected $_keysSet     = false;
       
    53 
       
    54     /**#@+
       
    55      * Array keys to use for module, controller, and action. Should be taken out of request.
       
    56      * @var string
       
    57      */
       
    58     protected $_moduleKey     = 'module';
       
    59     protected $_controllerKey = 'controller';
       
    60     protected $_actionKey     = 'action';
       
    61     /**#@-*/
       
    62 
       
    63     /**
       
    64      * @var Zend_Controller_Dispatcher_Interface
       
    65      */
       
    66     protected $_dispatcher;
       
    67 
       
    68     /**
       
    69      * @var Zend_Controller_Request_Abstract
       
    70      */
       
    71     protected $_request;
       
    72 
       
    73     public function getVersion() {
       
    74         return 1;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Instantiates route based on passed Zend_Config structure
       
    79      */
       
    80     public static function getInstance(Zend_Config $config)
       
    81     {
       
    82         $frontController = Zend_Controller_Front::getInstance();
       
    83 
       
    84         $defs       = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
       
    85         $dispatcher = $frontController->getDispatcher();
       
    86         $request    = $frontController->getRequest();
       
    87 
       
    88         return new self($defs, $dispatcher, $request);
       
    89     }
       
    90 
       
    91     /**
       
    92      * Constructor
       
    93      *
       
    94      * @param array $defaults Defaults for map variables with keys as variable names
       
    95      * @param Zend_Controller_Dispatcher_Interface $dispatcher Dispatcher object
       
    96      * @param Zend_Controller_Request_Abstract $request Request object
       
    97      */
       
    98     public function __construct(array $defaults = array(),
       
    99                 Zend_Controller_Dispatcher_Interface $dispatcher = null,
       
   100                 Zend_Controller_Request_Abstract $request = null)
       
   101     {
       
   102         $this->_defaults = $defaults;
       
   103 
       
   104         if (isset($request)) {
       
   105             $this->_request = $request;
       
   106         }
       
   107 
       
   108         if (isset($dispatcher)) {
       
   109             $this->_dispatcher = $dispatcher;
       
   110         }
       
   111     }
       
   112 
       
   113     /**
       
   114      * Set request keys based on values in request object
       
   115      *
       
   116      * @return void
       
   117      */
       
   118     protected function _setRequestKeys()
       
   119     {
       
   120         if (null !== $this->_request) {
       
   121             $this->_moduleKey     = $this->_request->getModuleKey();
       
   122             $this->_controllerKey = $this->_request->getControllerKey();
       
   123             $this->_actionKey     = $this->_request->getActionKey();
       
   124         }
       
   125 
       
   126         if (null !== $this->_dispatcher) {
       
   127             $this->_defaults += array(
       
   128                 $this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
       
   129                 $this->_actionKey     => $this->_dispatcher->getDefaultAction(),
       
   130                 $this->_moduleKey     => $this->_dispatcher->getDefaultModule()
       
   131             );
       
   132         }
       
   133 
       
   134         $this->_keysSet = true;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Matches a user submitted path. Assigns and returns an array of variables
       
   139      * on a successful match.
       
   140      *
       
   141      * If a request object is registered, it uses its setModuleName(),
       
   142      * setControllerName(), and setActionName() accessors to set those values.
       
   143      * Always returns the values as an array.
       
   144      *
       
   145      * @param string $path Path used to match against this routing map
       
   146      * @return array An array of assigned values or a false on a mismatch
       
   147      */
       
   148     public function match($path, $partial = false)
       
   149     {
       
   150         $this->_setRequestKeys();
       
   151 
       
   152         $values = array();
       
   153         $params = array();
       
   154 
       
   155         if (!$partial) {
       
   156             $path = trim($path, self::URI_DELIMITER);
       
   157         } else {
       
   158             $matchedPath = $path;
       
   159         }
       
   160 
       
   161         if ($path != '') {
       
   162             $path = explode(self::URI_DELIMITER, $path);
       
   163 
       
   164             if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
       
   165                 $values[$this->_moduleKey] = array_shift($path);
       
   166                 $this->_moduleValid = true;
       
   167             }
       
   168 
       
   169             if (count($path) && !empty($path[0])) {
       
   170                 $values[$this->_controllerKey] = array_shift($path);
       
   171             }
       
   172 
       
   173             if (count($path) && !empty($path[0])) {
       
   174                 $values[$this->_actionKey] = array_shift($path);
       
   175             }
       
   176 
       
   177             if ($numSegs = count($path)) {
       
   178                 for ($i = 0; $i < $numSegs; $i = $i + 2) {
       
   179                     $key = urldecode($path[$i]);
       
   180                     $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
       
   181                     $params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
       
   182                 }
       
   183             }
       
   184         }
       
   185 
       
   186         if ($partial) {
       
   187             $this->setMatchedPath($matchedPath);
       
   188         }
       
   189 
       
   190         $this->_values = $values + $params;
       
   191 
       
   192         return $this->_values + $this->_defaults;
       
   193     }
       
   194 
       
   195     /**
       
   196      * Assembles user submitted parameters forming a URL path defined by this route
       
   197      *
       
   198      * @param array $data An array of variable and value pairs used as parameters
       
   199      * @param bool $reset Weither to reset the current params
       
   200      * @return string Route path with user submitted parameters
       
   201      */
       
   202     public function assemble($data = array(), $reset = false, $encode = true, $partial = false)
       
   203     {
       
   204         if (!$this->_keysSet) {
       
   205             $this->_setRequestKeys();
       
   206         }
       
   207 
       
   208         $params = (!$reset) ? $this->_values : array();
       
   209 
       
   210         foreach ($data as $key => $value) {
       
   211             if ($value !== null) {
       
   212                 $params[$key] = $value;
       
   213             } elseif (isset($params[$key])) {
       
   214                 unset($params[$key]);
       
   215             }
       
   216         }
       
   217 
       
   218         $params += $this->_defaults;
       
   219 
       
   220         $url = '';
       
   221 
       
   222         if ($this->_moduleValid || array_key_exists($this->_moduleKey, $data)) {
       
   223             if ($params[$this->_moduleKey] != $this->_defaults[$this->_moduleKey]) {
       
   224                 $module = $params[$this->_moduleKey];
       
   225             }
       
   226         }
       
   227         unset($params[$this->_moduleKey]);
       
   228 
       
   229         $controller = $params[$this->_controllerKey];
       
   230         unset($params[$this->_controllerKey]);
       
   231 
       
   232         $action = $params[$this->_actionKey];
       
   233         unset($params[$this->_actionKey]);
       
   234 
       
   235         foreach ($params as $key => $value) {
       
   236             $key = ($encode) ? urlencode($key) : $key;
       
   237             if (is_array($value)) {
       
   238                 foreach ($value as $arrayValue) {
       
   239                     $arrayValue = ($encode) ? urlencode($arrayValue) : $arrayValue;
       
   240                     $url .= '/' . $key;
       
   241                     $url .= '/' . $arrayValue;
       
   242                 }
       
   243             } else {
       
   244                 if ($encode) $value = urlencode($value);
       
   245                 $url .= '/' . $key;
       
   246                 $url .= '/' . $value;
       
   247             }
       
   248         }
       
   249 
       
   250         if (!empty($url) || $action !== $this->_defaults[$this->_actionKey]) {
       
   251             if ($encode) $action = urlencode($action);
       
   252             $url = '/' . $action . $url;
       
   253         }
       
   254 
       
   255         if (!empty($url) || $controller !== $this->_defaults[$this->_controllerKey]) {
       
   256             if ($encode) $controller = urlencode($controller);
       
   257             $url = '/' . $controller . $url;
       
   258         }
       
   259 
       
   260         if (isset($module)) {
       
   261             if ($encode) $module = urlencode($module);
       
   262             $url = '/' . $module . $url;
       
   263         }
       
   264 
       
   265         return ltrim($url, self::URI_DELIMITER);
       
   266     }
       
   267 
       
   268     /**
       
   269      * Return a single parameter of route's defaults
       
   270      *
       
   271      * @param string $name Array key of the parameter
       
   272      * @return string Previously set default
       
   273      */
       
   274     public function getDefault($name) {
       
   275         if (isset($this->_defaults[$name])) {
       
   276             return $this->_defaults[$name];
       
   277         }
       
   278     }
       
   279 
       
   280     /**
       
   281      * Return an array of defaults
       
   282      *
       
   283      * @return array Route defaults
       
   284      */
       
   285     public function getDefaults() {
       
   286         return $this->_defaults;
       
   287     }
       
   288 
       
   289 }