PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Lib/CroogoRouter.php

http://github.com/croogo/croogo
PHP | 221 lines | 124 code | 14 blank | 83 comment | 18 complexity | 70aa11855e6095dccdc6f853c44ed219 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. App::uses('ApiRoute', 'Croogo.Routing/Route');
  3. App::uses('Router', 'Routing');
  4. App::uses('StringConverter', 'Croogo.Utility');
  5. /**
  6. * CroogoRouter
  7. *
  8. * NOTE: Do not use this class as a substitute of Router class.
  9. * Use it only for CroogoRouter::connect()
  10. *
  11. * @package Croogo.Croogo.Lib
  12. * @version 1.0
  13. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. * @link http://www.croogo.org
  16. */
  17. class CroogoRouter {
  18. /**
  19. * If Translate plugin is active,
  20. * an extra Route will be created for locale-based URLs
  21. *
  22. * For example,
  23. * http://yoursite.com/blog/post-title, and
  24. * http://yoursite.com/eng/blog/post-title
  25. *
  26. * Returns this object's routes array. Returns false if there are no routes available.
  27. *
  28. * @param string $route An empty string, or a route string "/"
  29. * @param array $default NULL or an array describing the default route
  30. * @param array $params An array matching the named elements in the
  31. * route to regular expressions which that element
  32. * should match.
  33. * @return array Array of routes
  34. * @see Router::connect()
  35. * @throws RouterException
  36. */
  37. public static function connect($route, $default = array(), $params = array(), $options = array()) {
  38. return self::__connect($route, $default, $params, $options);
  39. }
  40. /**
  41. *
  42. * @see Router::connect()
  43. */
  44. private static function __connect($route, $default = array(), $params = array(), $options = array()) {
  45. $options = Hash::merge(array('promote' => false), $options);
  46. $localizedRoute = $route == '/' ? '' : $route;
  47. if (CakePlugin::loaded('Translate')) {
  48. Router::connect('/:locale' . $localizedRoute, $default, array_merge(array('locale' => '[a-z]{3}'), $params));
  49. if ($options['promote']) {
  50. Router::promote();
  51. }
  52. }
  53. $return = Router::connect($route, $default, $params);
  54. if ($options['promote']) {
  55. Router::promote();
  56. }
  57. return $return;
  58. }
  59. /**
  60. * Check wether request is a API call.
  61. *
  62. * @see CakeRequest::addDetector()
  63. * @param $request CakeRequest Request object
  64. * @return bool True when request contains the necessary route parameters
  65. */
  66. public static function isApiRequest(CakeRequest $request) {
  67. if (!$request) {
  68. return false;
  69. }
  70. if (empty($request['api']) || empty($request['prefix'])) {
  71. return false;
  72. }
  73. if ($request['api'] !== Configure::read('Croogo.Api.path')) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * Check wether request is from a whitelisted IP address
  80. *
  81. * @see CakeRequest::addDetector()
  82. * @param $request CakeRequest Request object
  83. * @return boolean True when request is from a whitelisted IP Address
  84. */
  85. public static function isWhitelistedRequest(CakeRequest $request) {
  86. if (!$request) {
  87. return false;
  88. }
  89. $clientIp = $request->clientIp();
  90. $whitelist = array_map(
  91. 'trim',
  92. (array)explode(',', Configure::read('Site.ipWhitelist'))
  93. );
  94. return in_array($clientIp, $whitelist);
  95. }
  96. /**
  97. * Creates REST resource routes for the given controller(s).
  98. *
  99. * @param string|array $controller string or array of controller names
  100. * @return array Array of mapped resources
  101. * @see Router::mapResources()
  102. */
  103. public static function mapResources($controller, $options = array()) {
  104. $options = array_merge(array(
  105. 'routeClass' => 'ApiRoute',
  106. ), $options);
  107. static $defaultRouteClass;
  108. if (empty($defaultRouteClass)) {
  109. $defaultRouteClass = Router::defaultRouteClass();
  110. }
  111. Router::defaultRouteClass('ApiRoute');
  112. $routes = Router::mapResources($controller, $options);
  113. Router::defaultRouteClass($defaultRouteClass);
  114. return $routes;
  115. }
  116. /**
  117. * If you want your non-routed controler actions (like /users/add) to support locale based urls,
  118. * this method must be called AFTER all the routes.
  119. *
  120. * @return void
  121. */
  122. public static function localize() {
  123. if (CakePlugin::loaded('Translate')) {
  124. Router::connect('/:locale/:plugin/:controller/:action/*', array(), array('locale' => '[a-z]{3}'));
  125. Router::connect('/:locale/:controller/:action/*', array(), array('locale' => '[a-z]{3}'));
  126. }
  127. }
  128. /**
  129. * Load plugin routes
  130. *
  131. * @return void
  132. */
  133. public static function plugins() {
  134. $pluginRoutes = Configure::read('Hook.routes');
  135. if (!$pluginRoutes || !is_array(Configure::read('Hook.routes'))) {
  136. return;
  137. }
  138. $plugins = Configure::read('Hook.routes');
  139. foreach ($plugins as $plugin) {
  140. $path = App::pluginPath($plugin) . 'Config' . DS . 'routes.php';
  141. if (file_exists($path)) {
  142. include $path;
  143. }
  144. }
  145. }
  146. /**
  147. * Routes for content types
  148. *
  149. * @param string $alias
  150. * @return void
  151. */
  152. public static function contentType($alias) {
  153. CroogoRouter::connect('/' . $alias, array(
  154. 'plugin' => 'nodes', 'controller' => 'nodes',
  155. 'action' => 'index', 'type' => $alias
  156. ));
  157. CroogoRouter::connect('/' . $alias . '/archives/*', array(
  158. 'plugin' => 'nodes', 'controller' => 'nodes',
  159. 'action' => 'index', 'type' => $alias
  160. ));
  161. CroogoRouter::connect('/' . $alias . '/:slug', array(
  162. 'plugin' => 'nodes', 'controller' => 'nodes',
  163. 'action' => 'view', 'type' => $alias
  164. ));
  165. CroogoRouter::connect('/' . $alias . '/term/:slug/*', array(
  166. 'plugin' => 'nodes', 'controller' => 'nodes',
  167. 'action' => 'term', 'type' => $alias
  168. ));
  169. }
  170. /**
  171. * Apply routes for content types with routes enabled
  172. *
  173. * @return void
  174. */
  175. public static function routableContentTypes() {
  176. try {
  177. $types = ClassRegistry::init('Taxonomy.Type')->find('all', array(
  178. 'cache' => array(
  179. 'name' => 'types',
  180. 'config' => 'croogo_types',
  181. ),
  182. ));
  183. foreach ($types as $type) {
  184. if (isset($type['Params']['routes']) && $type['Params']['routes']) {
  185. CroogoRouter::contentType($type['Type']['alias']);
  186. }
  187. }
  188. }
  189. catch (MissingConnectionException $e) {
  190. CakeLog::write('critical', __d('croogo', 'Unable to get routeable content types: %s', $e->getMessage()));
  191. }
  192. }
  193. /**
  194. * Setup Site.home_url
  195. *
  196. * @return void
  197. */
  198. public static function routes() {
  199. $homeUrl = Configure::read('Site.home_url');
  200. if ($homeUrl && strpos($homeUrl, ':') !== false) {
  201. $converter = new StringConverter();
  202. $url = $converter->linkStringToArray($homeUrl);
  203. CroogoRouter::connect('/', $url, array(), array('promote' => true));
  204. }
  205. CakePlugin::routes();
  206. }
  207. }