Router.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. class Router
  3. {
  4. /**
  5. * @var array
  6. */
  7. private static $routes = array();
  8. /**
  9. * @var array
  10. */
  11. private static $errorPages = array(
  12. 404 => 'include/php/pages/404.php',
  13. 403 => 'include/php/pages/not-allowed.php'
  14. );
  15. private function __construct()
  16. {
  17. }
  18. private function __clone()
  19. {
  20. }
  21. /**
  22. * @param string|array $methods
  23. * @param string $pattern
  24. * @param callable|array|string $routeConfig
  25. * @param array $permission
  26. */
  27. public static function addRoute($methods, $pattern, $routeConfig, $permission = null)
  28. {
  29. if(!is_array($methods)){
  30. $methods = array($methods);
  31. }
  32. $config = array(
  33. 'pattern' => $pattern,
  34. 'config' => $routeConfig,
  35. 'permission' => $permission,
  36. );
  37. foreach($methods as $method){
  38. $method = strtoupper($method);
  39. if(!isset(static::$routes[$method])){
  40. static::$routes[$method] = array();
  41. }
  42. static::$routes[$method][] = $config;
  43. }
  44. }
  45. /**
  46. * @param string $pattern
  47. * @param callable|array|string $routeConfig
  48. * @param array $permission
  49. */
  50. public static function addGet($pattern, $routeConfig, $permission = null)
  51. {
  52. static::addRoute('GET', $pattern, $routeConfig, $permission);
  53. }
  54. /**
  55. * @param string $pattern
  56. * @param callable|array|string $routeConfig
  57. * @param array $permission
  58. */
  59. public static function addPost($pattern, $routeConfig, $permission = null)
  60. {
  61. static::addRoute('POST', $pattern, $routeConfig, $permission);
  62. }
  63. /**
  64. * @param string $pattern
  65. * @param callable|array|string $routeConfig
  66. * @param array $permission
  67. */
  68. public static function addMixed($pattern, $routeConfig, $permission = null)
  69. {
  70. static::addRoute(array('GET', 'POST'), $pattern, $routeConfig, $permission);
  71. }
  72. /**
  73. * @param string $url
  74. * @param string $method
  75. *
  76. * @return string
  77. */
  78. public static function execute($url, $method = 'GET')
  79. {
  80. $method = strtoupper($method);
  81. if(!in_array($method, array('GET', 'POST')) && !isset(self::$routes[$method])){
  82. return 'Unsupported HTTP method.';
  83. }
  84. foreach(self::$routes[$method] as $route){
  85. if(rtrim($route['pattern'], '/') === rtrim($url, '/')){
  86. if(!is_null($route['permission'])){
  87. if(!Auth::isLoggedIn() || !Auth::hasPermission($route['permission'])){
  88. return static::loadAndBufferOutput(static::$errorPages[403]);
  89. }
  90. }
  91. return static::resolveRouteConfig($route['config']);
  92. }
  93. }
  94. return static::loadAndBufferOutput(static::$errorPages[404]);
  95. }
  96. /**
  97. * @return string
  98. */
  99. public static function executeCurrentRequest()
  100. {
  101. return static::execute(
  102. static::getCurrentUrlPath(),
  103. isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'
  104. );
  105. }
  106. /**
  107. * @param bool $removeGetParameters
  108. *
  109. * @return string
  110. */
  111. public static function getCurrentUrlPath($removeGetParameters = true)
  112. {
  113. $baseUrl = parse_url(FRONTEND_BASE_PATH);
  114. $basePath = isset($baseUrl['path']) ? rtrim($baseUrl['path'], '/') : '';
  115. $url = $_SERVER['REQUEST_URI'];
  116. if($removeGetParameters){
  117. $url = preg_replace('/\?.*/', '', $url); // Trim GET Parameters
  118. }
  119. // Trim all leading slashes
  120. $url = rtrim($url, '/');
  121. if(!empty($basePath) && ($basePathPos = strpos($url, $basePath)) === 0){
  122. $url = substr($url, strlen($basePath));
  123. }
  124. return $url;
  125. }
  126. /**
  127. * @param array $config
  128. *
  129. * @return string
  130. */
  131. public static function resolveRouteConfig($config)
  132. {
  133. if(is_string($config)){
  134. if(file_exists($config)){
  135. return static::loadAndBufferOutput($config);
  136. }
  137. }
  138. return static::loadAndBufferOutput(static::$errorPages[404]);
  139. }
  140. /**
  141. * @param string $file
  142. * @param array $variables
  143. *
  144. * @return string
  145. */
  146. public static function loadAndBufferOutput($file, $variables = array())
  147. {
  148. ob_start();
  149. extract($variables);
  150. require $file;
  151. return ob_get_clean();
  152. }
  153. }