Router.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 string $modulePath
  38. * Contains the module directory
  39. **/
  40. /**
  41. * @var string $moduleUrl
  42. * Contains the module Uri
  43. **/
  44. /**
  45. * @var string $themePath
  46. * Contains the theme directory
  47. **/
  48. /**
  49. * @var string $themeUrl
  50. * Contains the theme Uri
  51. **/
  52. /**
  53. * Create the router, initialize url and path
  54. **/
  55. public function __construct($context)
  56. {
  57. $pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
  58. $relativePath = (($pos === FALSE) ? "" : substr($_SERVER["SCRIPT_NAME"], 0, $pos));
  59. $this->rootPath = $_SERVER["DOCUMENT_ROOT"] . $relativePath . "/";
  60. $this->rootUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $relativePath ."/";
  61. $this->requestUrl = substr($_SERVER["REQUEST_URI"], count($this->rootUrl) -1);
  62. $this->context = $context;
  63. $this->routes = array();
  64. }
  65. /**
  66. * Called after database initialization
  67. * Check the site url and redirect user if the HOST does not match
  68. * If the site url is not defined in database, do not redirect
  69. **/
  70. public function init()
  71. {
  72. $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
  73. if ($siteUrl != $_SERVER["HTTP_HOST"] && $siteUrl !== null)
  74. {
  75. header("location: http://{$siteUrl}{$_SERVER['REQUEST_URI']}");
  76. die;
  77. }
  78. }
  79. /**
  80. * @return \Controller\AController controller
  81. * /core/controller/AController.php
  82. * Match request to a controller
  83. * return FALSE on failure (eg. 404)
  84. **/
  85. public function serveUrl()
  86. {
  87. $this->prepareUrl();
  88. $requestParams = explode("/", $this->requestUrl);
  89. foreach ($this->routes as $i)
  90. {
  91. $routeParams = explode("/", $i[0]);
  92. }
  93. //TODO@2
  94. }
  95. /**
  96. * Append local routes to router
  97. * Will load CMS pages, categories page, products page, cart pages, etc.
  98. **/
  99. private function prepareUrl()
  100. {
  101. $this->doRouteAdd("/:item", "\Controller\Product");
  102. $this->doRouteAdd("/:category/:item", "\Controller\Product");
  103. }
  104. /**
  105. * Add a route to the internal route list
  106. * Internal procedure
  107. **/
  108. private function doRouteAdd($route, $controller)
  109. {
  110. $this->routes[] = array($route, $controller);
  111. }
  112. /**
  113. * @param string $route Uri to match the controller
  114. * Uri can be formatted as '/:param/static'.
  115. * expl. '/product/:id/'
  116. * @param string $controller Controller class name.
  117. * new $controller() MUST return a \Tool\AController instance
  118. *
  119. * Add a route and a Controller to the list
  120. * Can only be called from `routerSetup' hook
  121. **/
  122. public function routeAdd($route, $controller)
  123. {
  124. if (!$this->context->hookManager->isInHook("routerSetup"))
  125. throw new \Exception("You can only add routes from `routerSetup' hook");
  126. $this->doRouteAdd($route, $controller);
  127. }
  128. public function __get($key)
  129. {
  130. switch ($key)
  131. {
  132. case "rootPath": return $this->rootPath; break;
  133. case "rootUrl": return $this->rootUrl; break;
  134. case "modulesPath": return $this->rootPath."content/modules/"; break;
  135. case "modulesUrl": return $this->rootUrl."content/modules/"; break;
  136. case "themesPath": return $this->rootPath."content/theme/"; break;
  137. case "themesUrl": return $this->rootUrl."content/theme/"; break;
  138. }
  139. throw new \Exception("Cannot access attribute {$key}");
  140. }
  141. }