sfRouting.class.php 7.3 KB

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