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.
15
* @package Zend_Controller
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @version $Id: Route.php 11073 2008-08-26 16:29:59Z dasprid $
19
* @license http://framework.zend.com/license/new-bsd New BSD License
22
/** Zend_Controller_Router_Route_Abstract */
23
require_once 'Zend/Controller/Router/Route/Abstract.php';
28
* @package Zend_Controller
30
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
31
* @license http://framework.zend.com/license/new-bsd New BSD License
32
* @see http://manuals.rubyonrails.com/read/chapter/65
34
class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract
37
protected $_urlVariable = ':';
38
protected $_urlDelimiter = '/';
39
protected $_regexDelimiter = '#';
40
protected $_defaultRegex = null;
43
* Holds names of all route's pattern variable names. Array index holds a position in URL.
46
protected $_variables = array();
49
* Holds Route patterns for all URL 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.
51
* In case of a wildcard, it stores an asterisk (*)
54
protected $_parts = array();
57
* Holds user submitted default values for route's variables. Name and value pairs.
60
protected $_defaults = array();
63
* Holds user submitted regular expression patterns for route's variables' values.
64
* Name and value pairs.
67
protected $_requirements = array();
70
* Associative array filled on match() that holds matched path values
71
* for given variable names.
74
protected $_values = array();
77
* Associative array filled on match() that holds wildcard variable
81
protected $_wildcardData = array();
84
* Helper var that holds a count of route pattern's static parts
88
protected $_staticCount = 0;
90
public function getVersion() {
95
* Instantiates route based on passed Zend_Config structure
97
* @param Zend_Config $config Configuration object
99
public static function getInstance(Zend_Config $config)
101
$reqs = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
102
$defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
103
return new self($config->route, $defs, $reqs);
107
* Prepares the route for mapping by splitting (exploding) it
108
* to a corresponding atomic parts. These parts are assigned
109
* a position which is later used for matching and preparing values.
111
* @param string $route Map used to match with later submitted URL path
112
* @param array $defaults Defaults for map variables with keys as variable names
113
* @param array $reqs Regular expression requirements for variables (keys as variable names)
115
public function __construct($route, $defaults = array(), $reqs = array())
118
$route = trim($route, $this->_urlDelimiter);
119
$this->_defaults = (array) $defaults;
120
$this->_requirements = (array) $reqs;
124
foreach (explode($this->_urlDelimiter, $route) as $pos => $part) {
126
if (substr($part, 0, 1) == $this->_urlVariable) {
127
$name = substr($part, 1);
128
$this->_parts[$pos] = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
129
$this->_variables[$pos] = $name;
131
$this->_parts[$pos] = $part;
132
if ($part != '*') $this->_staticCount++;
142
* Matches a user submitted path with parts defined by a map. Assigns and
143
* returns an array of variables on a successful match.
145
* @param string $path Path used to match against this routing map
146
* @return array|false An array of assigned values or a false on a mismatch
148
public function match($path)
151
$pathStaticCount = 0;
154
$path = trim($path, $this->_urlDelimiter);
158
$path = explode($this->_urlDelimiter, $path);
160
foreach ($path as $pos => $pathPart) {
162
// Path is longer than a route, it's not a match
163
if (!array_key_exists($pos, $this->_parts)) {
167
// If it's a wildcard, get the rest of URL as wildcard data and stop matching
168
if ($this->_parts[$pos] == '*') {
169
$count = count($path);
170
for($i = $pos; $i < $count; $i+=2) {
171
$var = urldecode($path[$i]);
172
if (!isset($this->_wildcardData[$var]) && !isset($this->_defaults[$var]) && !isset($values[$var])) {
173
$this->_wildcardData[$var] = (isset($path[$i+1])) ? urldecode($path[$i+1]) : null;
179
$name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
180
$pathPart = urldecode($pathPart);
182
// If it's a static part, match directly
183
if ($name === null && $this->_parts[$pos] != $pathPart) {
187
// If it's a variable with requirement, match a regex. If not - everything matches
188
if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $pathPart)) {
192
// If it's a variable store it's value for later
193
if ($name !== null) {
194
$values[$name] = $pathPart;
203
// Check if all static mappings have been matched
204
if ($this->_staticCount != $pathStaticCount) {
208
$return = $values + $this->_wildcardData + $this->_defaults;
210
// Check if all map variables have been initialized
211
foreach ($this->_variables as $var) {
212
if (!array_key_exists($var, $return)) {
217
$this->_values = $values;
224
* Assembles user submitted parameters forming a URL path defined by this route
226
* @param array $data An array of variable and value pairs used as parameters
227
* @param boolean $reset Whether or not to set route defaults with those provided in $data
228
* @return string Route path with user submitted parameters
230
public function assemble($data = array(), $reset = false, $encode = false)
236
foreach ($this->_parts as $key => $part) {
238
$name = isset($this->_variables[$key]) ? $this->_variables[$key] : null;
241
if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) {
247
if (isset($data[$name]) && !$useDefault) {
248
$url[$key] = $data[$name];
250
} elseif (!$reset && !$useDefault && isset($this->_values[$name])) {
251
$url[$key] = $this->_values[$name];
252
} elseif (!$reset && !$useDefault && isset($this->_wildcardData[$name])) {
253
$url[$key] = $this->_wildcardData[$name];
254
} elseif (isset($this->_defaults[$name])) {
255
$url[$key] = $this->_defaults[$name];
257
require_once 'Zend/Controller/Router/Exception.php';
258
throw new Zend_Controller_Router_Exception($name . ' is not specified');
262
} elseif ($part != '*') {
265
if (!$reset) $data += $this->_wildcardData;
266
foreach ($data as $var => $value) {
267
if ($value !== null) {
269
$url[$key++] = $value;
279
foreach (array_reverse($url, true) as $key => $value) {
280
if ($flag || !isset($this->_variables[$key]) || $value !== $this->getDefault($this->_variables[$key])) {
281
if ($encode) $value = urlencode($value);
282
$return = $this->_urlDelimiter . $value . $return;
287
return trim($return, $this->_urlDelimiter);
292
* Return a single parameter of route's defaults
294
* @param string $name Array key of the parameter
295
* @return string Previously set default
297
public function getDefault($name) {
298
if (isset($this->_defaults[$name])) {
299
return $this->_defaults[$name];
305
* Return an array of defaults
307
* @return array Route defaults
309
public function getDefaults() {
310
return $this->_defaults;