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

Debug toolbar