Projekt

Obecné

Profil

Stáhnout (5.49 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2
/* SVN FILE: $Id: router.php 4409 2007-02-02 13:20:59Z phpnut $ */
3
/**
4
 * Parses the request URL into controller, action, and parameters.
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
9
 * Copyright 2005-2007, Cake Software Foundation, Inc.
10
 *								1785 E. Sahara Avenue, Suite 490-204
11
 *								Las Vegas, Nevada 89104
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright		Copyright 2005-2007, Cake Software Foundation, Inc.
18
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package			cake
20
 * @subpackage		cake.cake.libs
21
 * @since			CakePHP(tm) v 0.2.9
22
 * @version			$Revision: 4409 $
23
 * @modifiedby		$LastChangedBy: phpnut $
24
 * @lastmodified	$Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
25
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Included libraries.
29
 *
30
 */
31
	if (!class_exists('Object')) {
32
		 uses ('object');
33
	}
34
/**
35
 * Parses the request URL into controller, action, and parameters.
36
 *
37
 * @package		cake
38
 * @subpackage	cake.cake.libs
39
 */
40
class Router extends Object {
41
/**
42
 * Array of routes
43
 *
44
 * @var array
45
 * @access public
46
 */
47
	 var $routes = array();
48
/**
49
 * CAKE_ADMIN route
50
 *
51
 * @var array
52
 * @access private
53
 */
54
	 var $__admin = null;
55
/**
56
 * Constructor
57
 *
58
 * @access public
59
 */
60
	function __construct() {
61
		if (defined('CAKE_ADMIN')) {
62
			$admin = CAKE_ADMIN;
63
			if (!empty($admin)) {
64
				$this->__admin = array('/:' . $admin . '/:controller/:action/* (default)',
65
										'/^(?:\/(?:(' . $admin . ')(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:[\\/\\?](.*))?)?)?))[\/]*$/',
66
										array($admin, 'controller', 'action'), array());
67
			}
68
		}
69
	}
70
/**
71
 * Returns this object's routes array. Returns false if there are no routes available.
72
 *
73
 * @param string $route	An empty string, or a route string "/"
74
 * @param array $default NULL or an array describing the default route
75
 * @return array Array of routes
76
 */
77
	function connect($route, $default = null) {
78
		$parsed = $names = array();
79

    
80
		if (defined('CAKE_ADMIN') && $default == null) {
81
			if ($route == CAKE_ADMIN) {
82
				$this->routes[] = $this->__admin;
83
				$this->__admin = null;
84
			}
85
		}
86
		$r = null;
87
		if (($route == '') || ($route == '/')) {
88
			$regexp='/^[\/]*$/';
89
			$this->routes[] = array($route, $regexp, array(), $default);
90
		} else {
91
			$elements = array();
92

    
93
			foreach(explode('/', $route)as $element) {
94
				if (trim($element))
95
				$elements[] = $element;
96
			}
97

    
98
			if (!count($elements)) {
99
				return false;
100
			}
101

    
102
			foreach($elements as $element) {
103

    
104
				if (preg_match('/^:(.+)$/', $element, $r)) {
105
					$parsed[]='(?:\/([^\/]+))?';
106
					$names[] =$r[1];
107
				} elseif(preg_match('/^\*$/', $element, $r)) {
108
					$parsed[] = '(?:\/(.*))?';
109
				} else {
110
					$parsed[] = '/' . $element;
111
				}
112
			}
113

    
114
			$regexp='#^' . join('', $parsed) . '[\/]*$#';
115
			$this->routes[] = array($route, $regexp, $names, $default);
116
		}
117
		return $this->routes;
118
	}
119
/**
120
 * Parses given URL and returns an array of controllers, action and parameters
121
 * taken from that URL.
122
 *
123
 * @param string $url URL to be parsed
124
 * @return array
125
 * @access public
126
 */
127
	function parse($url) {
128
		if ($url && ('/' != $url[0])) {
129
			if (!defined('SERVER_IIS')) {
130
				$url = '/' . $url;
131
			}
132
		}
133
		$out = array('pass'=>array());
134
		$r = null;
135
		$default_route = array('/:controller/:action/* (default)',
136
								'/^(?:\/(?:([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:\\/([a-zA-Z0-9_\\-\\.\\;\\:]+)(?:[\\/\\?](.*))?)?))[\\/]*$/',
137
								array('controller', 'action'), array());
138

    
139
		if (defined('CAKE_ADMIN') && $this->__admin != null) {
140
			$this->routes[]=$this->__admin;
141
			$this->__admin =null;
142
		}
143
		$this->connect('/bare/:controller/:action/*', array('bare' => '1'));
144
		$this->connect('/ajax/:controller/:action/*', array('bare' => '1'));
145

    
146
		if (defined('WEBSERVICES') && WEBSERVICES == 'on') {
147
			$this->connect('/rest/:controller/:action/*', array('webservices' => 'Rest'));
148
			$this->connect('/rss/:controller/:action/*', array('webservices' => 'Rss'));
149
			$this->connect('/soap/:controller/:action/*', array('webservices' => 'Soap'));
150
			$this->connect('/xml/:controller/:action/*', array('webservices' => 'Xml'));
151
			$this->connect('/xmlrpc/:controller/:action/*', array('webservices' => 'XmlRpc'));
152
		}
153
		$this->routes[] = $default_route;
154

    
155
		if (strpos($url, '?') !== false) {
156
			$url = substr($url, 0, strpos($url, '?'));
157
		}
158

    
159
		foreach($this->routes as $route) {
160
			list($route, $regexp, $names, $defaults) = $route;
161

    
162
			if (preg_match($regexp, $url, $r)) {
163
				// remove the first element, which is the url
164
				array_shift ($r);
165
				// hack, pre-fill the default route names
166
				foreach($names as $name) {
167
					$out[$name] = null;
168
				}
169
				$ii=0;
170

    
171
				if (is_array($defaults)) {
172
					foreach($defaults as $name => $value) {
173
						if (preg_match('#[a-zA-Z_\-]#i', $name)) {
174
							$out[$name] = $value;
175
						} else {
176
							$out['pass'][] = $value;
177
						}
178
					}
179
				}
180
				
181
				foreach($r as $found) {
182
					// if $found is a named url element (i.e. ':action')
183
					if (isset($names[$ii])) {
184
						$out[$names[$ii]] = $found;
185
					} else {
186
						// unnamed elements go in as 'pass'
187
						$found = explode('/', $found);
188
						$pass = array();
189
						foreach($found as $key => $value) {
190
							if ($value == "0") {
191
								$pass[$key] = $value;
192
							} elseif ($value) {
193
								$pass[$key] = $value;
194
							}
195
						}
196
						$out['pass'] = am($out['pass'], $pass);
197
					}
198
					$ii++;
199
				}
200
				break;
201
			}
202
		}
203
		return $out;
204
	}
205
}
206
?>
(14-14/19)