| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* sfRouting class controls the generation and parsing of URLs.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage routing
|
|
|
16 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: sfRouting.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
18 |
*/
|
|
|
19 |
abstract class sfRouting
|
|
|
20 |
{
|
|
|
21 |
protected
|
|
|
22 |
$dispatcher = null,
|
|
|
23 |
$cache = null,
|
|
|
24 |
$defaultParameters = array(),
|
|
|
25 |
$options = array();
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Class constructor.
|
|
|
29 |
*
|
|
|
30 |
* @see initialize()
|
|
|
31 |
*/
|
|
|
32 |
public function __construct(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array())
|
|
|
33 |
{
|
|
|
34 |
$this->initialize($dispatcher, $cache, $options);
|
|
|
35 |
|
|
|
36 |
if (!isset($this->options['auto_shutdown']) || $this->options['auto_shutdown'])
|
|
|
37 |
{
|
|
|
38 |
register_shutdown_function(array($this, 'shutdown'));
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Returns the routing cache object.
|
|
|
44 |
*
|
|
|
45 |
* @return sfCache A sfCache instance or null
|
|
|
46 |
*/
|
|
|
47 |
public function getCache()
|
|
|
48 |
{
|
|
|
49 |
return $this->cache;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Initializes this sfRouting instance.
|
|
|
54 |
*
|
|
|
55 |
* Available options:
|
|
|
56 |
*
|
|
|
57 |
* * default_module: The default module name
|
|
|
58 |
* * default_action: The default action name
|
|
|
59 |
* * logging: Whether to log or not (false by default)
|
|
|
60 |
* * debug: Whether to cache or not (false by default)
|
|
|
61 |
* * context: An array of context variables to help URL matching and generation
|
|
|
62 |
*
|
|
|
63 |
* @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
|
|
|
64 |
* @param sfCache $cache An sfCache instance
|
|
|
65 |
* @param array $options An associative array of initialization options.
|
|
|
66 |
*/
|
|
|
67 |
public function initialize(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array())
|
|
|
68 |
{
|
|
|
69 |
$this->dispatcher = $dispatcher;
|
|
|
70 |
|
|
|
71 |
$options['debug'] = isset($options['debug']) ? (boolean) $options['debug'] : false;
|
|
|
72 |
|
|
|
73 |
// disable caching when in debug mode
|
|
|
74 |
$this->cache = $options['debug'] ? null : $cache;
|
|
|
75 |
|
|
|
76 |
$this->setDefaultParameter('module', isset($options['default_module']) ? $options['default_module'] : 'default');
|
|
|
77 |
$this->setDefaultParameter('action', isset($options['default_action']) ? $options['default_action'] : 'index');
|
|
|
78 |
|
|
|
79 |
if (!isset($options['logging']))
|
|
|
80 |
{
|
|
|
81 |
$options['logging'] = false;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if (!isset($options['context']))
|
|
|
85 |
{
|
|
|
86 |
$options['context'] = array();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$this->options = $options;
|
|
|
90 |
|
|
|
91 |
$this->dispatcher->connect('user.change_culture', array($this, 'listenToChangeCultureEvent'));
|
|
|
92 |
$this->dispatcher->connect('request.filter_parameters', array($this, 'filterParametersEvent'));
|
|
|
93 |
|
|
|
94 |
$this->loadConfiguration();
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Returns the options.
|
|
|
99 |
*
|
|
|
100 |
* @return array An array of options
|
|
|
101 |
*/
|
|
|
102 |
public function getOptions()
|
|
|
103 |
{
|
|
|
104 |
return $this->options;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Loads routing configuration.
|
|
|
109 |
*
|
|
|
110 |
* This methods notifies a routing.load_configuration event.
|
|
|
111 |
*/
|
|
|
112 |
public function loadConfiguration()
|
|
|
113 |
{
|
|
|
114 |
$this->dispatcher->notify(new sfEvent($this, 'routing.load_configuration'));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Gets the internal URI for the current request.
|
|
|
119 |
*
|
|
|
120 |
* @param bool $with_route_name Whether to give an internal URI with the route name (@route)
|
|
|
121 |
* or with the module/action pair
|
|
|
122 |
*
|
|
|
123 |
* @return string The current internal URI
|
|
|
124 |
*/
|
|
|
125 |
abstract public function getCurrentInternalUri($with_route_name = false);
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Gets the current compiled route array.
|
|
|
129 |
*
|
|
|
130 |
* @return array The route array
|
|
|
131 |
*/
|
|
|
132 |
abstract public function getRoutes();
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Sets the compiled route array.
|
|
|
136 |
*
|
|
|
137 |
* @param array $routes The route array
|
|
|
138 |
*
|
|
|
139 |
* @return array The route array
|
|
|
140 |
*/
|
|
|
141 |
abstract public function setRoutes($routes);
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Returns true if this instance has some routes.
|
|
|
145 |
*
|
|
|
146 |
* @return bool
|
|
|
147 |
*/
|
|
|
148 |
abstract public function hasRoutes();
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Clears all current routes.
|
|
|
152 |
*/
|
|
|
153 |
abstract public function clearRoutes();
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Generates a valid URLs for parameters.
|
|
|
157 |
*
|
|
|
158 |
* @param string $name The route name
|
|
|
159 |
* @param array $params The parameter values
|
|
|
160 |
* @param Boolean $absolute Whether to generate an absolute URL
|
|
|
161 |
*
|
|
|
162 |
* @return string The generated URL
|
|
|
163 |
*/
|
|
|
164 |
abstract public function generate($name, $params = array(), $absolute = false);
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Parses a URL to find a matching route and sets internal state.
|
|
|
168 |
*
|
|
|
169 |
* Returns false if no route match the URL.
|
|
|
170 |
*
|
|
|
171 |
* @param string $url URL to be parsed
|
|
|
172 |
*
|
|
|
173 |
* @return array|false An array of parameters or false if the route does not match
|
|
|
174 |
*/
|
|
|
175 |
abstract public function parse($url);
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Gets the default parameters for URL generation.
|
|
|
179 |
*
|
|
|
180 |
* @return array An array of default parameters
|
|
|
181 |
*/
|
|
|
182 |
public function getDefaultParameters()
|
|
|
183 |
{
|
|
|
184 |
return $this->defaultParameters;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Gets a default parameter.
|
|
|
189 |
*
|
|
|
190 |
* @param string $key The key
|
|
|
191 |
*
|
|
|
192 |
* @return string The value
|
|
|
193 |
*/
|
|
|
194 |
public function getDefaultParameter($key)
|
|
|
195 |
{
|
|
|
196 |
return isset($this->defaultParameters[$key]) ? $this->defaultParameters[$key] : null;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Sets a default parameter.
|
|
|
201 |
*
|
|
|
202 |
* @param string $key The key
|
|
|
203 |
* @param string $value The value
|
|
|
204 |
*/
|
|
|
205 |
public function setDefaultParameter($key, $value)
|
|
|
206 |
{
|
|
|
207 |
$this->defaultParameters[$key] = $value;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Sets the default parameters for URL generation.
|
|
|
212 |
*
|
|
|
213 |
* @param array $parameters An array of default parameters
|
|
|
214 |
*/
|
|
|
215 |
public function setDefaultParameters($parameters)
|
|
|
216 |
{
|
|
|
217 |
$this->defaultParameters = $parameters;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Listens to the user.change_culture event.
|
|
|
222 |
*
|
|
|
223 |
* @param sfEvent $event An sfEvent instance
|
|
|
224 |
*
|
|
|
225 |
*/
|
|
|
226 |
public function listenToChangeCultureEvent(sfEvent $event)
|
|
|
227 |
{
|
|
|
228 |
// change the culture in the routing default parameters
|
|
|
229 |
$this->setDefaultParameter('sf_culture', $event['culture']);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
/**
|
|
|
233 |
* Listens to the request.filter_parameters event.
|
|
|
234 |
*
|
|
|
235 |
* @param sfEvent $event An sfEvent instance
|
|
|
236 |
*
|
|
|
237 |
* @return array $parameters An array of parameters for the event
|
|
|
238 |
*/
|
|
|
239 |
public function filterParametersEvent(sfEvent $event, $parameters)
|
|
|
240 |
{
|
|
|
241 |
$context = $event->getParameters();
|
|
|
242 |
|
|
|
243 |
$this->options['context'] = $context;
|
|
|
244 |
|
|
|
245 |
if (false === $params = $this->parse($event['path_info']))
|
|
|
246 |
{
|
|
|
247 |
return $parameters;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
return array_merge($parameters, $params);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
protected function fixGeneratedUrl($url, $absolute = false)
|
|
|
254 |
{
|
|
|
255 |
if (isset($this->options['context']['prefix']))
|
|
|
256 |
{
|
|
|
257 |
if (0 === strpos($url, 'http'))
|
|
|
258 |
{
|
|
|
259 |
$url = preg_replace('#https?\://[^/]+#', '$0'.$this->options['context']['prefix'], $url);
|
|
|
260 |
}
|
|
|
261 |
else
|
|
|
262 |
{
|
|
|
263 |
$url = $this->options['context']['prefix'].$url;
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
if ($absolute && isset($this->options['context']['host']) && 0 !== strpos($url, 'http'))
|
|
|
268 |
{
|
|
|
269 |
$url = 'http'.(isset($this->options['context']['is_secure']) && $this->options['context']['is_secure'] ? 's' : '').'://'.$this->options['context']['host'].$url;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
return $url;
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
/**
|
|
|
276 |
* Execute the shutdown procedure.
|
|
|
277 |
*
|
|
|
278 |
* @return void
|
|
|
279 |
*/
|
|
|
280 |
public function shutdown()
|
|
|
281 |
{
|
|
|
282 |
}
|
|
|
283 |
}
|