| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
abstract class sfRouting |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$dispatcher = null, |
|---|
| 23 |
$cache = null, |
|---|
| 24 |
$defaultParameters = array(), |
|---|
| 25 |
$defaultModule = 'default', |
|---|
| 26 |
$defaultAction = 'index', |
|---|
| 27 |
$options = array(); |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
* Class constructor. |
|---|
| 31 |
* |
|---|
| 32 |
* @see initialize() |
|---|
| 33 |
*/ |
|---|
| 34 |
public function __construct(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array()) |
|---|
| 35 |
{ |
|---|
| 36 |
$this->initialize($dispatcher, $cache, $options); |
|---|
| 37 |
|
|---|
| 38 |
if (!isset($this->options['auto_shutdown']) || $this->options['auto_shutdown']) |
|---|
| 39 |
{ |
|---|
| 40 |
register_shutdown_function(array($this, 'shutdown')); |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
* Initializes this sfRouting instance. |
|---|
| 46 |
* |
|---|
| 47 |
* Available options: |
|---|
| 48 |
* |
|---|
| 49 |
* * default_module: The default module name |
|---|
| 50 |
* * default_action: The default action name |
|---|
| 51 |
* * logging: Whether to log or not (false by default) |
|---|
| 52 |
* * debug: Whether to cache or not (false by default) |
|---|
| 53 |
* |
|---|
| 54 |
* @param sfEventDispatcher $dispatcher An sfEventDispatcher instance |
|---|
| 55 |
* @param sfCache $cache An sfCache instance |
|---|
| 56 |
* @param array $options An associative array of initialization options. |
|---|
| 57 |
*/ |
|---|
| 58 |
public function initialize(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array()) |
|---|
| 59 |
{ |
|---|
| 60 |
$this->dispatcher = $dispatcher; |
|---|
| 61 |
|
|---|
| 62 |
$options['debug'] = isset($options['debug']) ? (boolean) $options['debug'] : false; |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
$this->cache = $options['debug'] ? null : $cache; |
|---|
| 66 |
|
|---|
| 67 |
if (isset($options['default_module'])) |
|---|
| 68 |
{ |
|---|
| 69 |
$this->defaultModule = $options['default_module']; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
if (isset($options['default_action'])) |
|---|
| 73 |
{ |
|---|
| 74 |
$this->defaultAction = $options['default_action']; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
if (!isset($options['logging'])) |
|---|
| 78 |
{ |
|---|
| 79 |
$options['logging'] = false; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
$this->options = $options; |
|---|
| 83 |
|
|---|
| 84 |
$this->dispatcher->connect('user.change_culture', array($this, 'listenToChangeCultureEvent')); |
|---|
| 85 |
$this->dispatcher->connect('request.filter_parameters', array($this, 'filterParametersEvent')); |
|---|
| 86 |
|
|---|
| 87 |
$this->loadConfiguration(); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* Loads routing configuration. |
|---|
| 92 |
* |
|---|
| 93 |
* This methods notifies a routing.load_configuration event. |
|---|
| 94 |
*/ |
|---|
| 95 |
public function loadConfiguration() |
|---|
| 96 |
{ |
|---|
| 97 |
$this->dispatcher->notify(new sfEvent($this, 'routing.load_configuration')); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* Gets the internal URI for the current request. |
|---|
| 102 |
* |
|---|
| 103 |
* @param bool $with_route_name Whether to give an internal URI with the route name (@route) |
|---|
| 104 |
* or with the module/action pair |
|---|
| 105 |
* |
|---|
| 106 |
* @return string The current internal URI |
|---|
| 107 |
*/ |
|---|
| 108 |
abstract public function getCurrentInternalUri($with_route_name = false); |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Gets the current compiled route array. |
|---|
| 112 |
* |
|---|
| 113 |
* @return array The route array |
|---|
| 114 |
*/ |
|---|
| 115 |
abstract public function getRoutes(); |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
* Sets the compiled route array. |
|---|
| 119 |
* |
|---|
| 120 |
* @param array $routes The route array |
|---|
| 121 |
* |
|---|
| 122 |
* @return array The route array |
|---|
| 123 |
*/ |
|---|
| 124 |
abstract public function setRoutes($routes); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
* Returns true if this instance has some routes. |
|---|
| 128 |
* |
|---|
| 129 |
* @return bool |
|---|
| 130 |
*/ |
|---|
| 131 |
abstract public function hasRoutes(); |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Clears all current routes. |
|---|
| 135 |
*/ |
|---|
| 136 |
abstract public function clearRoutes(); |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
* Generates a valid URLs for parameters. |
|---|
| 140 |
* |
|---|
| 141 |
* @param string $name The route name |
|---|
| 142 |
* @param array $params The parameter values |
|---|
| 143 |
* @param string $querydiv The divider between URI and query string |
|---|
| 144 |
* @param string $divider The divider between key/value pairs |
|---|
| 145 |
* @param string $equals The equal sign to use between key and value |
|---|
| 146 |
* |
|---|
| 147 |
* @return string The generated URL |
|---|
| 148 |
*/ |
|---|
| 149 |
abstract public function generate($name, $params = array(), $querydiv = '/', $divider = '/', $equals = '/'); |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
* Parses a URL to find a matching route and sets internal state. |
|---|
| 153 |
* |
|---|
| 154 |
* Throws a sfError404Exception if no route match the URL. |
|---|
| 155 |
* |
|---|
| 156 |
* @param string $url URL to be parsed |
|---|
| 157 |
* |
|---|
| 158 |
* @return array An array of parameters |
|---|
| 159 |
* |
|---|
| 160 |
* @throws sfError404Exception if the url is not parseable by the sfRouting object |
|---|
| 161 |
*/ |
|---|
| 162 |
abstract public function parse($url); |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
* Sets a default parameter. |
|---|
| 166 |
* |
|---|
| 167 |
* @param string $key The key |
|---|
| 168 |
* @param string $value The value |
|---|
| 169 |
*/ |
|---|
| 170 |
public function setDefaultParameter($key, $value) |
|---|
| 171 |
{ |
|---|
| 172 |
$this->defaultParameters[$key] = $value; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
* Sets the default parameters for URL generation. |
|---|
| 177 |
* |
|---|
| 178 |
* @param array $parameters An array of default parameters |
|---|
| 179 |
*/ |
|---|
| 180 |
public function setDefaultParameters($parameters) |
|---|
| 181 |
{ |
|---|
| 182 |
$this->defaultParameters = $parameters; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
protected function fixDefaults($arr) |
|---|
| 186 |
{ |
|---|
| 187 |
if (empty($arr['module'])) |
|---|
| 188 |
{ |
|---|
| 189 |
$arr['module'] = $this->defaultModule; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
if (empty($arr['action'])) |
|---|
| 193 |
{ |
|---|
| 194 |
$arr['action'] = $this->defaultAction; |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
return $arr; |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
protected function mergeArrays($arr1, $arr2) |
|---|
| 201 |
{ |
|---|
| 202 |
foreach ($arr2 as $key => $value) |
|---|
| 203 |
{ |
|---|
| 204 |
$arr1[$key] = $value; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
return $arr1; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
* Listens to the user.change_culture event. |
|---|
| 212 |
* |
|---|
| 213 |
* @param sfEvent An sfEvent instance |
|---|
| 214 |
* |
|---|
| 215 |
*/ |
|---|
| 216 |
public function listenToChangeCultureEvent(sfEvent $event) |
|---|
| 217 |
{ |
|---|
| 218 |
|
|---|
| 219 |
$this->setDefaultParameter('sf_culture', $event['culture']); |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
* Listens to the request.filter_parameters event. |
|---|
| 224 |
* |
|---|
| 225 |
* @param sfEvent $event An sfEvent instance |
|---|
| 226 |
* @param array $parameters An array of parameters for the event |
|---|
| 227 |
* |
|---|
| 228 |
*/ |
|---|
| 229 |
public function filterParametersEvent(sfEvent $event, $parameters) |
|---|
| 230 |
{ |
|---|
| 231 |
try |
|---|
| 232 |
{ |
|---|
| 233 |
return array_merge($parameters, $this->parse($event['path_info'])); |
|---|
| 234 |
} |
|---|
| 235 |
catch (sfError404Exception $e) |
|---|
| 236 |
{ |
|---|
| 237 |
return $parameters; |
|---|
| 238 |
} |
|---|
| 239 |
} |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
* Execute the shutdown procedure. |
|---|
| 243 |
* |
|---|
| 244 |
* @return void |
|---|
| 245 |
*/ |
|---|
| 246 |
public function shutdown() |
|---|
| 247 |
{ |
|---|
| 248 |
} |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|