web/lib/Zend/Controller/Router/Route/Hostname.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: Hostname.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';
    26 /**
    26 /**
    27  * Hostname Route
    27  * Hostname Route
    28  *
    28  *
    29  * @package    Zend_Controller
    29  * @package    Zend_Controller
    30  * @subpackage Router
    30  * @subpackage Router
    31  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    31  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  * @see        http://manuals.rubyonrails.com/read/chapter/65
    33  * @see        http://manuals.rubyonrails.com/read/chapter/65
    34  */
    34  */
    35 class Zend_Controller_Router_Route_Hostname extends Zend_Controller_Router_Route_Abstract
    35 class Zend_Controller_Router_Route_Hostname extends Zend_Controller_Router_Route_Abstract
    36 {
    36 {
    37 
    37 
    38     protected $_hostVariable   = ':';
    38     /**
       
    39      * Host variable
       
    40      *
       
    41      * @var string
       
    42      */
       
    43     protected $_hostVariable = ':';
       
    44 
       
    45     /**
       
    46      * Regex delimiter
       
    47      *
       
    48      * @var string
       
    49      */
    39     protected $_regexDelimiter = '#';
    50     protected $_regexDelimiter = '#';
    40     protected $_defaultRegex   = null;
    51 
       
    52     /**
       
    53      * Default regex string
       
    54      *
       
    55      * @var string|null
       
    56      */
       
    57     protected $_defaultRegex = null;
    41 
    58 
    42     /**
    59     /**
    43      * Holds names of all route's pattern variable names. Array index holds a position in host.
    60      * Holds names of all route's pattern variable names. Array index holds a position in host.
       
    61      *
    44      * @var array
    62      * @var array
    45      */
    63      */
    46     protected $_variables = array();
    64     protected $_variables = array();
    47 
    65 
    48     /**
    66     /**
    49      * Holds Route patterns for all host parts. In case of a variable it stores it's regex
    67      * Holds Route patterns for all host parts. In case of a variable it stores it's regex
    50      * requirement or null. In case of a static part, it holds only it's direct value.
    68      * requirement or null. In case of a static part, it holds only it's direct value.
       
    69      *
    51      * @var array
    70      * @var array
    52      */
    71      */
    53     protected $_parts = array();
    72     protected $_parts = array();
    54 
    73 
    55     /**
    74     /**
    56      * Holds user submitted default values for route's variables. Name and value pairs.
    75      * Holds user submitted default values for route's variables. Name and value pairs.
       
    76      *
    57      * @var array
    77      * @var array
    58      */
    78      */
    59     protected $_defaults = array();
    79     protected $_defaults = array();
    60 
    80 
    61     /**
    81     /**
    62      * Holds user submitted regular expression patterns for route's variables' values.
    82      * Holds user submitted regular expression patterns for route's variables' values.
    63      * Name and value pairs.
    83      * Name and value pairs.
       
    84      *
    64      * @var array
    85      * @var array
    65      */
    86      */
    66     protected $_requirements = array();
    87     protected $_requirements = array();
    67 
    88 
    68     /**
    89     /**
    69      * Default scheme
    90      * Default scheme
       
    91      *
    70      * @var string
    92      * @var string
    71      */
    93      */
    72     protected $_scheme = null;
    94     protected $_scheme = null;
    73 
    95 
    74     /**
    96     /**
    75      * Associative array filled on match() that holds matched path values
    97      * Associative array filled on match() that holds matched path values
    76      * for given variable names.
    98      * for given variable names.
       
    99      *
    77      * @var array
   100      * @var array
    78      */
   101      */
    79     protected $_values = array();
   102     protected $_values = array();
    80 
   103 
    81     /**
   104     /**
    86     protected $_request;
   109     protected $_request;
    87 
   110 
    88     /**
   111     /**
    89      * Helper var that holds a count of route pattern's static parts
   112      * Helper var that holds a count of route pattern's static parts
    90      * for validation
   113      * for validation
       
   114      *
    91      * @var int
   115      * @var int
    92      */
   116      */
    93     private $_staticCount = 0;
   117     private $_staticCount = 0;
    94 
   118 
    95     /**
   119     /**
    96      * Set the request object
   120      * Set the request object
    97      *
   121      *
    98      * @param  Zend_Controller_Request_Abstract|null $request
   122      * @param  Zend_Controller_Request_Abstract|null $request
    99      * @return void
       
   100      */
   123      */
   101     public function setRequest(Zend_Controller_Request_Abstract $request = null)
   124     public function setRequest(Zend_Controller_Request_Abstract $request = null)
   102     {
   125     {
   103         $this->_request = $request;
   126         $this->_request = $request;
   104     }
   127     }
   120 
   143 
   121     /**
   144     /**
   122      * Instantiates route based on passed Zend_Config structure
   145      * Instantiates route based on passed Zend_Config structure
   123      *
   146      *
   124      * @param Zend_Config $config Configuration object
   147      * @param Zend_Config $config Configuration object
       
   148      * @return Zend_Controller_Router_Route_Hostname
   125      */
   149      */
   126     public static function getInstance(Zend_Config $config)
   150     public static function getInstance(Zend_Config $config)
   127     {
   151     {
   128         $reqs   = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
   152         $reqs   = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
   129         $defs   = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
   153         $defs   = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
   130         $scheme = (isset($config->scheme)) ? $config->scheme : null;
   154         $scheme = (isset($config->scheme)) ? $config->scheme : null;
       
   155 
   131         return new self($config->route, $defs, $reqs, $scheme);
   156         return new self($config->route, $defs, $reqs, $scheme);
   132     }
   157     }
   133 
   158 
   134     /**
   159     /**
   135      * Prepares the route for mapping by splitting (exploding) it
   160      * Prepares the route for mapping by splitting (exploding) it
   136      * to a corresponding atomic parts. These parts are assigned
   161      * to a corresponding atomic parts. These parts are assigned
   137      * a position which is later used for matching and preparing values.
   162      * a position which is later used for matching and preparing values.
   138      *
   163      *
   139      * @param string $route Map used to match with later submitted hostname
   164      * @param string $route    Map used to match with later submitted hostname
   140      * @param array  $defaults Defaults for map variables with keys as variable names
   165      * @param array  $defaults Defaults for map variables with keys as variable names
   141      * @param array  $reqs Regular expression requirements for variables (keys as variable names)
   166      * @param array  $reqs     Regular expression requirements for variables (keys as variable names)
   142      * @param string $scheme
   167      * @param string $scheme
   143      */
   168      */
   144     public function __construct($route, $defaults = array(), $reqs = array(), $scheme = null)
   169     public function __construct($route, $defaults = array(), $reqs = array(), $scheme = null)
   145     {
   170     {
   146         $route               = trim($route, '.');
   171         $route               = trim($route, '.');
   149         $this->_scheme       = $scheme;
   174         $this->_scheme       = $scheme;
   150 
   175 
   151         if ($route != '') {
   176         if ($route != '') {
   152             foreach (explode('.', $route) as $pos => $part) {
   177             foreach (explode('.', $route) as $pos => $part) {
   153                 if (substr($part, 0, 1) == $this->_hostVariable) {
   178                 if (substr($part, 0, 1) == $this->_hostVariable) {
   154                     $name = substr($part, 1);
   179                     $name                   = substr($part, 1);
   155                     $this->_parts[$pos] = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
   180                     $this->_parts[$pos]     = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
   156                     $this->_variables[$pos] = $name;
   181                     $this->_variables[$pos] = $name;
   157                 } else {
   182                 } else {
   158                     $this->_parts[$pos] = $part;
   183                     $this->_parts[$pos] = $part;
   159                     $this->_staticCount++;
   184                     $this->_staticCount++;
   160                 }
   185                 }
   185         if (preg_match('#:\d+$#', $host, $result) === 1) {
   210         if (preg_match('#:\d+$#', $host, $result) === 1) {
   186             $host = substr($host, 0, -strlen($result[0]));
   211             $host = substr($host, 0, -strlen($result[0]));
   187         }
   212         }
   188 
   213 
   189         $hostStaticCount = 0;
   214         $hostStaticCount = 0;
   190         $values = array();
   215         $values          = array();
   191 
   216 
   192         $host = trim($host, '.');
   217         $host = trim($host, '.');
   193 
   218 
   194         if ($host != '') {
   219         if ($host != '') {
   195             $host = explode('.', $host);
   220             $host = explode('.', $host);
   198                 // Host is longer than a route, it's not a match
   223                 // Host is longer than a route, it's not a match
   199                 if (!array_key_exists($pos, $this->_parts)) {
   224                 if (!array_key_exists($pos, $this->_parts)) {
   200                     return false;
   225                     return false;
   201                 }
   226                 }
   202 
   227 
   203                 $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
   228                 $name     = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
   204                 $hostPart = urldecode($hostPart);
   229                 $hostPart = urldecode($hostPart);
   205 
   230 
   206                 // If it's a static part, match directly
   231                 // If it's a static part, match directly
   207                 if ($name === null && $this->_parts[$pos] != $hostPart) {
   232                 if ($name === null && $this->_parts[$pos] != $hostPart) {
   208                     return false;
   233                     return false;
   209                 }
   234                 }
   210 
   235 
   211                 // If it's a variable with requirement, match a regex. If not - everything matches
   236                 // If it's a variable with requirement, match a regex. If not - everything matches
   212                 if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $hostPart)) {
   237                 if ($this->_parts[$pos] !== null
       
   238                     && !preg_match(
       
   239                         $this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu',
       
   240                         $hostPart
       
   241                     )
       
   242                 ) {
   213                     return false;
   243                     return false;
   214                 }
   244                 }
   215 
   245 
   216                 // If it's a variable store it's value for later
   246                 // If it's a variable store it's value for later
   217                 if ($name !== null) {
   247                 if ($name !== null) {
   237         }
   267         }
   238 
   268 
   239         $this->_values = $values;
   269         $this->_values = $values;
   240 
   270 
   241         return $return;
   271         return $return;
   242 
       
   243     }
   272     }
   244 
   273 
   245     /**
   274     /**
   246      * Assembles user submitted parameters forming a hostname defined by this route
   275      * Assembles user submitted parameters forming a hostname defined by this route
   247      *
   276      *
   248      * @param  array $data An array of variable and value pairs used as parameters
   277      * @param  array   $data  An array of variable and value pairs used as parameters
   249      * @param  boolean $reset Whether or not to set route defaults with those provided in $data
   278      * @param  boolean $reset Whether or not to set route defaults with those provided in $data
       
   279      * @param  boolean $encode
       
   280      * @param  boolean $partial
       
   281      * @throws Zend_Controller_Router_Exception
   250      * @return string Route path with user submitted parameters
   282      * @return string Route path with user submitted parameters
   251      */
   283      */
   252     public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
   284     public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
   253     {
   285     {
   254         $host = array();
   286         $host = array();
   280         }
   312         }
   281 
   313 
   282         $return = '';
   314         $return = '';
   283 
   315 
   284         foreach (array_reverse($host, true) as $key => $value) {
   316         foreach (array_reverse($host, true) as $key => $value) {
   285             if ($flag || !isset($this->_variables[$key]) || $value !== $this->getDefault($this->_variables[$key]) || $partial) {
   317             if ($flag || !isset($this->_variables[$key]) || $value !== $this->getDefault($this->_variables[$key])
   286                 if ($encode) $value = urlencode($value);
   318                 || $partial
       
   319             ) {
       
   320                 if ($encode) {
       
   321                     $value = urlencode($value);
       
   322                 }
   287                 $return = '.' . $value . $return;
   323                 $return = '.' . $value . $return;
   288                 $flag = true;
   324                 $flag   = true;
   289             }
   325             }
   290         }
   326         }
   291 
   327 
   292         $url = trim($return, '.');
   328         $url = trim($return, '.');
   293 
   329 
   300             } else {
   336             } else {
   301                 $scheme = 'http';
   337                 $scheme = 'http';
   302             }
   338             }
   303         }
   339         }
   304 
   340 
   305         $url      = $scheme . '://' . $url;
   341         $url = $scheme . '://' . $url;
   306 
   342 
   307         return $url;
   343         return $url;
   308     }
   344     }
   309 
   345 
   310     /**
   346     /**
   311      * Return a single parameter of route's defaults
   347      * Return a single parameter of route's defaults
   312      *
   348      *
   313      * @param string $name Array key of the parameter
   349      * @param string $name Array key of the parameter
   314      * @return string Previously set default
   350      * @return string Previously set default
   315      */
   351      */
   316     public function getDefault($name) {
   352     public function getDefault($name)
       
   353     {
   317         if (isset($this->_defaults[$name])) {
   354         if (isset($this->_defaults[$name])) {
   318             return $this->_defaults[$name];
   355             return $this->_defaults[$name];
   319         }
   356         }
       
   357 
   320         return null;
   358         return null;
   321     }
   359     }
   322 
   360 
   323     /**
   361     /**
   324      * Return an array of defaults
   362      * Return an array of defaults
   325      *
   363      *
   326      * @return array Route defaults
   364      * @return array Route defaults
   327      */
   365      */
   328     public function getDefaults() {
   366     public function getDefaults()
       
   367     {
   329         return $this->_defaults;
   368         return $this->_defaults;
   330     }
   369     }
   331 
   370 
   332     /**
   371     /**
   333      * Get all variables which are used by the route
   372      * Get all variables which are used by the route