123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- class Router
- {
- const METHOD_GET = 'GET';
- const METHOD_POST = 'POST';
- /**
- * @var array
- */
- private static $routes = array();
- /**
- * @var array
- */
- private static $errorPages = array(
- 404 => 'include/php/pages/404.php',
- 403 => 'include/php/pages/not-allowed.php'
- );
- private function __construct()
- {
- }
- private function __clone()
- {
- }
- /**
- * @param string|array $methods
- * @param string $pattern
- * @param callable|array|string $routeConfig
- * @param array $permission
- */
- public static function addRoute($methods, $pattern, $routeConfig, $permission = null)
- {
- if(!is_array($methods)){
- $methods = array($methods);
- }
- $config = array(
- 'pattern' => $pattern,
- 'config' => $routeConfig,
- 'permission' => $permission,
- );
- foreach($methods as $method){
- $method = strtoupper($method);
- if(!isset(static::$routes[$method])){
- static::$routes[$method] = array();
- }
- static::$routes[$method][] = $config;
- }
- }
- /**
- * @param string $pattern
- * @param callable|string $routeConfig
- * @param array $permission
- */
- public static function addGet($pattern, $routeConfig, $permission = null)
- {
- static::addRoute(static::METHOD_GET, $pattern, $routeConfig, $permission);
- }
- /**
- * @param string $pattern
- * @param callable|string $routeConfig
- * @param array $permission
- */
- public static function addPost($pattern, $routeConfig, $permission = null)
- {
- static::addRoute(static::METHOD_POST, $pattern, $routeConfig, $permission);
- }
- /**
- * @param string $pattern
- * @param callable|string $routeConfig
- * @param array $permission
- */
- public static function addMixed($pattern, $routeConfig, $permission = null)
- {
- static::addRoute(array(static::METHOD_GET, static::METHOD_POST), $pattern, $routeConfig, $permission);
- }
- /**
- * @param string $url
- * @param string $method
- *
- * @return string
- */
- public static function execute($url, $method = self::METHOD_GET)
- {
- $method = strtoupper($method);
- if(!in_array($method, array(static::METHOD_GET, static::METHOD_POST)) && !isset(self::$routes[$method])){
- return 'Unsupported HTTP method.';
- }
- foreach(self::$routes[$method] as $route){
- if(rtrim($route['pattern'], '/') === rtrim($url, '/')){
- if(!is_null($route['permission'])){
- if(!Auth::isLoggedIn() || !Auth::hasPermission($route['permission'])){
- return static::loadAndBufferOutput(static::$errorPages[403]);
- }
- }
- return static::resolveRouteConfig($route['config']);
- }
- }
- return static::loadAndBufferOutput(static::$errorPages[404]);
- }
- /**
- * @return string
- */
- public static function executeCurrentRequest()
- {
- return static::execute(
- static::getCurrentUrlPath(),
- isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : static::METHOD_GET
- );
- }
- /**
- * @param int $errorNumber
- * @return string|null
- */
- public static function displayError($errorNumber)
- {
- $errorPage = isset(static::$errorPages[$errorNumber])
- ? static::loadAndBufferOutput(static::$errorPages[$errorNumber])
- : '';
- echo Router::loadAndBufferOutput(
- 'include/php/template/layout.php',
- array(
- 'content' => $errorPage,
- )
- );
- exit;
- }
- /**
- * @param bool $removeGetParameters
- *
- * @return string
- */
- public static function getCurrentUrlPath($removeGetParameters = true)
- {
- $baseUrl = parse_url(FRONTEND_BASE_PATH);
- $basePath = isset($baseUrl['path']) ? rtrim($baseUrl['path'], '/') : '';
- $url = $_SERVER['REQUEST_URI'];
- if($removeGetParameters){
- $url = preg_replace('/\?.*/', '', $url); // Trim GET Parameters
- }
- // Trim all leading slashes
- $url = rtrim($url, '/');
- if(!empty($basePath) && ($basePathPos = strpos($url, $basePath)) === 0){
- $url = substr($url, strlen($basePath));
- }
- return $url;
- }
- /**
- * @param array $config
- *
- * @return string
- */
- public static function resolveRouteConfig($config)
- {
- if(is_string($config)){
- if(file_exists($config)){
- return static::loadAndBufferOutput($config);
- }
- }
- elseif(is_callable($config) && $config instanceof Closure){
- return $config();
- }
- return static::loadAndBufferOutput(static::$errorPages[404]);
- }
- /**
- * @param string $file
- * @param array $variables
- *
- * @return string
- */
- public static function loadAndBufferOutput($file, $variables = array())
- {
- ob_start();
- extract($variables);
- require $file;
- return ob_get_clean();
- }
- }
|