Router.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace Tools;
  3. /**
  4. * Will manager new connections to the server
  5. * and try to match the requests and the controllers
  6. **/
  7. class Router
  8. {
  9. /**
  10. * @var string $rootPath
  11. * Contains the application's root path (ex: http://myshop.com/) with trailing slash
  12. * Can be accessed read-only via $instance->rootPath
  13. **/
  14. private $rootPath;
  15. /**
  16. * @var string $rootPath
  17. * Contains the application's root path (ex: /srv/http/myshop/) with trailing slash
  18. * Can be accessed read-only via $instance->rootUrl
  19. **/
  20. private $rootUrl;
  21. /**
  22. * @var string $requestUrl
  23. * Contains request
  24. **/
  25. private $requestUrl;
  26. /**
  27. * @var array ( array ( uri => controller ) ) $routes
  28. **/
  29. private $routes;
  30. /**
  31. * @var \Tools\Context $context
  32. * /core/tools/Context.php
  33. * Contains website's informations
  34. **/
  35. private $context;
  36. /**
  37. * @var array RouteParams
  38. * Contains route parameter
  39. * ex: /product/:id will contains {
  40. * 0 => product
  41. * 1 => [ID]
  42. * ':id' => [ID]
  43. **/
  44. private $routeParams;
  45. /**
  46. * @var array $overridden
  47. * contains url and paths to rewrite
  48. **/
  49. private $overridden;
  50. /**
  51. * @var string $modulePath
  52. * Contains the module directory
  53. **/
  54. /**
  55. * @var string $moduleUrl
  56. * Contains the module Uri
  57. **/
  58. /**
  59. * @var string $themePath
  60. * Contains the theme directory
  61. **/
  62. /**
  63. * @var string $themeUrl
  64. * Contains the theme Uri
  65. **/
  66. /**
  67. * Create the router, initialize url and path
  68. **/
  69. public function __construct($server, $context)
  70. {
  71. $pos = strrpos($server["SCRIPT_NAME"], "/");
  72. $relativePath = (($pos === FALSE) ? "" : substr($server["SCRIPT_NAME"], 0, $pos));
  73. $this->rootPath = $server["DOCUMENT_ROOT"] . $relativePath . "/";
  74. $this->rootUrl = $server["REQUEST_SCHEME"] . "://" . $server["HTTP_HOST"] . $relativePath ."/";
  75. $this->requestUrl = substr($server["REQUEST_URI"], count($this->rootUrl) -1);
  76. $this->context = $context;
  77. $this->routes = array();
  78. $this->overridden = null;
  79. }
  80. /**
  81. * Called after database initialization
  82. * Check the site url and redirect user if the HOST does not match
  83. * If the site url is not defined in database, do not redirect
  84. **/
  85. public function init($server)
  86. {
  87. $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
  88. // @codeCoverageIgnoreStart
  89. // This code is tested under another process
  90. if ($siteUrl != $server["HTTP_HOST"] && $siteUrl !== null)
  91. {
  92. header("location: http://{$siteUrl}{$server['REQUEST_URI']}");
  93. die;
  94. }
  95. // @codeCoverageIgnoreEnd
  96. }
  97. /**
  98. * @return \Controller\AController controller
  99. * /core/controller/AController.php
  100. * Match request to a controller
  101. * return FALSE on failure (eg. 404)
  102. **/
  103. public function serveUrl()
  104. {
  105. //TODO trigger hook GET, POST
  106. $this->prepareUrl();
  107. $requestParams = explode("/", $this->requestUrl);
  108. foreach ($this->routes as $i)
  109. {
  110. $routeParams = explode("/", $i[0]);
  111. $p = $this->routeMatch($requestParams, $routeParams);
  112. if ($p === false)
  113. continue;
  114. $controller = $this->createController($i[1], $p);
  115. if ($controller)
  116. return $controller;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Create and return Controller for the given route
  122. * @param $className
  123. * @param array $routeParameters
  124. * @return \Tools\AController on succes, false otherwise
  125. **/
  126. private function createController($className, $params)
  127. {
  128. if (!class_exists($className))
  129. return false;
  130. $this->routeParams = $params;
  131. $result = null;
  132. try
  133. {
  134. $result = new $className($this->context, $params);
  135. if (!($result instanceof \Tools\AController))
  136. return false;
  137. }
  138. catch (\Exception\Error404 $e)
  139. {
  140. return false;
  141. }
  142. return $result;
  143. }
  144. /**
  145. * Check if the request match route
  146. * @param array $request User request
  147. * @param array $route Route to check
  148. * @return array on success, false on failure
  149. **/
  150. private function routeMatch($request, $route)
  151. {
  152. $i = count($request);
  153. $params = array();
  154. if ($i != count($route))
  155. return false;
  156. while ($i)
  157. {
  158. $i--;
  159. if ($route[$i] == '' && $request[$i] == '')
  160. continue;
  161. if ($route[$i] == '' || $request[$i] == '')
  162. return false;
  163. if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
  164. return false;
  165. if ($route[$i][0] == ':')
  166. $params[$route[$i]] = $request[$i];
  167. $params[$i -1] = $request[$i];
  168. }
  169. return array_reverse($params);
  170. }
  171. /**
  172. * Append local routes to router
  173. * Will load CMS pages, categories page, products page, cart pages, etc.
  174. **/
  175. private function prepareUrl()
  176. {
  177. $fetcher = new \Entity\Cms();
  178. $pages = $fetcher->selects(null, array("order"));
  179. foreach ($pages as $i)
  180. $this->doRouteAdd($i->shurl, $i->controller);
  181. }
  182. /**
  183. * Add a route to the internal route list
  184. * Internal procedure
  185. **/
  186. private function doRouteAdd($route, $controller)
  187. {
  188. $this->routes[] = array($route, $controller);
  189. }
  190. /**
  191. * @param string $route Uri to match the controller
  192. * Uri can be formatted as '/:param/static'.
  193. * expl. '/product/:id/'
  194. * @param string $controller Controller class name.
  195. * new $controller() MUST return a \Tool\AController instance
  196. *
  197. * Add a route and a Controller to the list
  198. * Can only be called from `routerSetup' hook
  199. **/
  200. public function routeAdd($route, $controller)
  201. {
  202. if (!$this->context->hookManager->isInHook("routerSetup"))
  203. throw new \Exception("You can only add routes from `routerSetup' hook");
  204. $this->doRouteAdd($route, $controller);
  205. }
  206. /**
  207. * Override url
  208. * This SHOULD not be called for security purpose
  209. * @param string $type the url type to override
  210. * @param string $value the new value
  211. * Will fail if called from a controller
  212. * @return boolean false on failure
  213. **/
  214. public function overrideUrl($type, $value)
  215. {
  216. if (!$this->context->isTestingEnvironment())
  217. return false;
  218. $this->overridden[$type] = $value;
  219. return true;
  220. }
  221. /**
  222. * Getter
  223. **/
  224. public function __get($key)
  225. {
  226. if (isset($this->overridden) && in_array($key, array("modulesPath")))
  227. return $this->overridden[$key];
  228. switch ($key)
  229. {
  230. case "rootPath": return $this->rootPath; break;
  231. case "rootUrl": return $this->rootUrl; break;
  232. case "modulesPath": return $this->rootPath."content/modules/"; break;
  233. case "modulesUrl": return $this->rootUrl."content/modules/"; break;
  234. case "themesPath": return $this->rootPath."content/theme/"; break;
  235. case "themesUrl": return $this->rootUrl."content/theme/"; break;
  236. }
  237. throw new \Exception("Cannot access attribute {$key}");
  238. }
  239. }