~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Controller/Router/Route.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * @package    Zend_Controller
 
16
 * @subpackage Router
 
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
 
20
 */
 
21
 
 
22
/** Zend_Controller_Router_Route_Abstract */
 
23
require_once 'Zend/Controller/Router/Route/Abstract.php';
 
24
 
 
25
/**
 
26
 * Route
 
27
 *
 
28
 * @package    Zend_Controller
 
29
 * @subpackage Router
 
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
 
33
 */
 
34
class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract
 
35
{
 
36
 
 
37
    protected $_urlVariable = ':';
 
38
    protected $_urlDelimiter = '/';
 
39
    protected $_regexDelimiter = '#';
 
40
    protected $_defaultRegex = null;
 
41
 
 
42
    /**
 
43
     * Holds names of all route's pattern variable names. Array index holds a position in URL.
 
44
     * @var array
 
45
     */
 
46
    protected $_variables = array();
 
47
 
 
48
    /**
 
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 (*)
 
52
     * @var array
 
53
     */
 
54
    protected $_parts = array();
 
55
 
 
56
    /**
 
57
     * Holds user submitted default values for route's variables. Name and value pairs.
 
58
     * @var array
 
59
     */
 
60
    protected $_defaults = array();
 
61
 
 
62
    /**
 
63
     * Holds user submitted regular expression patterns for route's variables' values.
 
64
     * Name and value pairs.
 
65
     * @var array
 
66
     */
 
67
    protected $_requirements = array();
 
68
 
 
69
    /**
 
70
     * Associative array filled on match() that holds matched path values
 
71
     * for given variable names.
 
72
     * @var array
 
73
     */
 
74
    protected $_values = array();
 
75
 
 
76
    /**
 
77
     * Associative array filled on match() that holds wildcard variable
 
78
     * names and values.
 
79
     * @var array
 
80
     */
 
81
    protected $_wildcardData = array();
 
82
 
 
83
    /**
 
84
     * Helper var that holds a count of route pattern's static parts
 
85
     * for validation
 
86
     * @var int
 
87
     */
 
88
    protected $_staticCount = 0;
 
89
 
 
90
    public function getVersion() {
 
91
        return 1;
 
92
    }
 
93
    
 
94
    /**
 
95
     * Instantiates route based on passed Zend_Config structure
 
96
     *
 
97
     * @param Zend_Config $config Configuration object
 
98
     */
 
99
    public static function getInstance(Zend_Config $config)
 
100
    {
 
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);
 
104
    }
 
105
 
 
106
    /**
 
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.
 
110
     *
 
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)
 
114
     */
 
115
    public function __construct($route, $defaults = array(), $reqs = array())
 
116
    {
 
117
 
 
118
        $route = trim($route, $this->_urlDelimiter);
 
119
        $this->_defaults = (array) $defaults;
 
120
        $this->_requirements = (array) $reqs;
 
121
 
 
122
        if ($route != '') {
 
123
 
 
124
            foreach (explode($this->_urlDelimiter, $route) as $pos => $part) {
 
125
 
 
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;
 
130
                } else {
 
131
                    $this->_parts[$pos] = $part;
 
132
                    if ($part != '*') $this->_staticCount++;
 
133
                }
 
134
 
 
135
            }
 
136
 
 
137
        }
 
138
 
 
139
    }
 
140
 
 
141
    /**
 
142
     * Matches a user submitted path with parts defined by a map. Assigns and
 
143
     * returns an array of variables on a successful match.
 
144
     *
 
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
 
147
     */
 
148
    public function match($path)
 
149
    {
 
150
 
 
151
        $pathStaticCount = 0;
 
152
        $values = array();
 
153
 
 
154
        $path = trim($path, $this->_urlDelimiter);
 
155
        
 
156
        if ($path != '') {
 
157
 
 
158
            $path = explode($this->_urlDelimiter, $path);
 
159
 
 
160
            foreach ($path as $pos => $pathPart) {
 
161
 
 
162
                // Path is longer than a route, it's not a match
 
163
                if (!array_key_exists($pos, $this->_parts)) {
 
164
                    return false;
 
165
                }
 
166
                
 
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;
 
174
                        }
 
175
                    }
 
176
                    break;
 
177
                }
 
178
 
 
179
                $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
 
180
                $pathPart = urldecode($pathPart);
 
181
 
 
182
                // If it's a static part, match directly
 
183
                if ($name === null && $this->_parts[$pos] != $pathPart) {
 
184
                    return false;
 
185
                }
 
186
 
 
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)) {
 
189
                    return false;
 
190
                }
 
191
 
 
192
                // If it's a variable store it's value for later
 
193
                if ($name !== null) {
 
194
                    $values[$name] = $pathPart;
 
195
                } else {
 
196
                    $pathStaticCount++;
 
197
                }
 
198
                
 
199
            }
 
200
 
 
201
        }
 
202
 
 
203
        // Check if all static mappings have been matched
 
204
        if ($this->_staticCount != $pathStaticCount) {
 
205
            return false;
 
206
        }
 
207
 
 
208
        $return = $values + $this->_wildcardData + $this->_defaults;
 
209
 
 
210
        // Check if all map variables have been initialized
 
211
        foreach ($this->_variables as $var) {
 
212
            if (!array_key_exists($var, $return)) {
 
213
                return false;
 
214
            }
 
215
        }
 
216
 
 
217
        $this->_values = $values;
 
218
        
 
219
        return $return;
 
220
 
 
221
    }
 
222
 
 
223
    /**
 
224
     * Assembles user submitted parameters forming a URL path defined by this route
 
225
     *
 
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
 
229
     */
 
230
    public function assemble($data = array(), $reset = false, $encode = false)
 
231
    {
 
232
 
 
233
        $url = array();
 
234
        $flag = false;
 
235
 
 
236
        foreach ($this->_parts as $key => $part) {
 
237
 
 
238
            $name = isset($this->_variables[$key]) ? $this->_variables[$key] : null;
 
239
 
 
240
            $useDefault = false;
 
241
            if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) {
 
242
                $useDefault = true;
 
243
            }
 
244
 
 
245
            if (isset($name)) {
 
246
 
 
247
                if (isset($data[$name]) && !$useDefault) {
 
248
                    $url[$key] = $data[$name];
 
249
                    unset($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];
 
256
                } else {
 
257
                    require_once 'Zend/Controller/Router/Exception.php';
 
258
                    throw new Zend_Controller_Router_Exception($name . ' is not specified');
 
259
                }
 
260
 
 
261
 
 
262
            } elseif ($part != '*') {
 
263
                $url[$key] = $part;
 
264
            } else {
 
265
                if (!$reset) $data += $this->_wildcardData;
 
266
                foreach ($data as $var => $value) {
 
267
                    if ($value !== null) {
 
268
                        $url[$key++] = $var;
 
269
                        $url[$key++] = $value;
 
270
                        $flag = true;
 
271
                    }
 
272
                }
 
273
            }
 
274
 
 
275
        }
 
276
 
 
277
        $return = '';
 
278
 
 
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;
 
283
                $flag = true;
 
284
            }
 
285
        }
 
286
 
 
287
        return trim($return, $this->_urlDelimiter);
 
288
 
 
289
    }
 
290
 
 
291
    /**
 
292
     * Return a single parameter of route's defaults
 
293
     *
 
294
     * @param string $name Array key of the parameter
 
295
     * @return string Previously set default
 
296
     */
 
297
    public function getDefault($name) {
 
298
        if (isset($this->_defaults[$name])) {
 
299
            return $this->_defaults[$name];
 
300
        }
 
301
        return null;
 
302
    }
 
303
 
 
304
    /**
 
305
     * Return an array of defaults
 
306
     *
 
307
     * @return array Route defaults
 
308
     */
 
309
    public function getDefaults() {
 
310
        return $this->_defaults;
 
311
    }
 
312
 
 
313
}