Router.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 $modulePath
  23. * Contains the module directory
  24. **/
  25. /**
  26. * Create the router, initialize url and path
  27. **/
  28. public function __construct()
  29. {
  30. $pos = strrpos($_SERVER["SCRIPT_NAME"], "/");
  31. $relativePath = (($pos === FALSE) ? "" : substr($_SERVER["SCRIPT_NAME"], 0, $pos));
  32. $this->rootPath = $_SERVER["DOCUMENT_ROOT"] . $relativePath . "/";
  33. $this->rootUrl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $relativePath ."/";
  34. }
  35. /**
  36. * TODO@1 This function SHOULD be disabled by configuration
  37. * Called after database initialization
  38. * Check the site url and redirect user if the HOST does not match
  39. **/
  40. public function init()
  41. {
  42. $siteUrl = \Entity\Config::getConfig(null, "siteUrl");
  43. if ($siteUrl != $_SERVER["HTTP_HOST"])
  44. {
  45. header("location: http://{$siteUrl}{$_SERVER['REQUEST_URI']}");
  46. die;
  47. }
  48. }
  49. /**
  50. * @return \Controller\AController controller
  51. * /core/controller/AController.php
  52. * Match request to a controller
  53. * return FALSE on failure (eg. 404)
  54. **/
  55. public function serveUrl()
  56. {
  57. $this->prepareUrl();
  58. //TODO@2
  59. }
  60. /**
  61. * Append local routes to router
  62. * Will load CMS pages, categories page, products page, cart pages, etc.
  63. **/
  64. private function prepareUrl()
  65. {
  66. //TODO@2 add internal route config
  67. }
  68. /**
  69. * TODO@1 check end of tag to format url and paths
  70. **/
  71. public function __get($key)
  72. {
  73. switch ($key)
  74. {
  75. case "rootPath": return $this->rootPath; break;
  76. case "rootUrl": return $this->rootUrl; break;
  77. case "modulesPath": return $this->rootPath."content/modules/"; break;
  78. }
  79. }
  80. }