PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Lib/CroogoRouter.php

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