sfRouting.class.php 6.2 KB

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