web/lib/Zend/Controller/Router/Route/Static.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: Static.php 23210 2010-10-21 16:10:55Z 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  * StaticRoute is used for managing static URIs.
       
    28  *
       
    29  * It's a lot faster compared to the standard Route implementation.
       
    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  */
       
    36 class Zend_Controller_Router_Route_Static extends Zend_Controller_Router_Route_Abstract
       
    37 {
       
    38 
       
    39     protected $_route = null;
       
    40     protected $_defaults = array();
       
    41 
       
    42     public function getVersion() {
       
    43         return 1;
       
    44     }
       
    45 
       
    46     /**
       
    47      * Instantiates route based on passed Zend_Config structure
       
    48      *
       
    49      * @param Zend_Config $config Configuration object
       
    50      */
       
    51     public static function getInstance(Zend_Config $config)
       
    52     {
       
    53         $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
       
    54         return new self($config->route, $defs);
       
    55     }
       
    56 
       
    57     /**
       
    58      * Prepares the route for mapping.
       
    59      *
       
    60      * @param string $route Map used to match with later submitted URL path
       
    61      * @param array $defaults Defaults for map variables with keys as variable names
       
    62      */
       
    63     public function __construct($route, $defaults = array())
       
    64     {
       
    65         $this->_route = trim($route, '/');
       
    66         $this->_defaults = (array) $defaults;
       
    67     }
       
    68 
       
    69     /**
       
    70      * Matches a user submitted path with a previously defined route.
       
    71      * Assigns and returns an array of defaults on a successful match.
       
    72      *
       
    73      * @param string $path Path used to match against this routing map
       
    74      * @return array|false An array of assigned values or a false on a mismatch
       
    75      */
       
    76     public function match($path, $partial = false)
       
    77     {
       
    78         if ($partial) {
       
    79             if ((empty($path) && empty($this->_route))
       
    80                 || (substr($path, 0, strlen($this->_route)) === $this->_route)
       
    81             ) {
       
    82                 $this->setMatchedPath($this->_route);
       
    83                 return $this->_defaults;
       
    84             }
       
    85         } else {
       
    86             if (trim($path, '/') == $this->_route) {
       
    87                 return $this->_defaults;
       
    88             }
       
    89         }
       
    90 
       
    91         return false;
       
    92     }
       
    93 
       
    94     /**
       
    95      * Assembles a URL path defined by this route
       
    96      *
       
    97      * @param array $data An array of variable and value pairs used as parameters
       
    98      * @return string Route path with user submitted parameters
       
    99      */
       
   100     public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
       
   101     {
       
   102         return $this->_route;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Return a single parameter of route's defaults
       
   107      *
       
   108      * @param string $name Array key of the parameter
       
   109      * @return string Previously set default
       
   110      */
       
   111     public function getDefault($name) {
       
   112         if (isset($this->_defaults[$name])) {
       
   113             return $this->_defaults[$name];
       
   114         }
       
   115         return null;
       
   116     }
       
   117 
       
   118     /**
       
   119      * Return an array of defaults
       
   120      *
       
   121      * @return array Route defaults
       
   122      */
       
   123     public function getDefaults() {
       
   124         return $this->_defaults;
       
   125     }
       
   126 
       
   127 }