Router.php 4.3 KB

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