diff --git a/Link b/Link deleted file mode 160000 index 6176303a..00000000 --- a/Link +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6176303ac4e34fadaa393a4c06d169aef3fbb32e diff --git a/api/Link.php b/api/Link.php new file mode 100644 index 00000000..090e666d --- /dev/null +++ b/api/Link.php @@ -0,0 +1,218 @@ + $routeDesc ){ + $routePath = preg_replace( $regex, $replacements, $routePath ); + if( preg_match( '#^/?' . $routePath . '/?$#', $path, $matches ) ){ + if( is_array( $routeDesc ) ) { + $handler = $routeDesc[0]; + if( isset( $routeDesc[2] )) { + $middleware = $routeDesc[2]; + } + } + else + $handler = $routeDesc; + $matched = $matches; + break; + } + } + } + unset( $matched[0] ); + + if( isset($middleware) ){ + $newMatched = self::callFunction( $middleware, $matched, $method ); + /* If new wildcard param are there pass them to main handler */ + if( $newMatched ) { + self::callFunction( $handler, $newMatched, $method ); + } else { + self::callFunction( $handler, $matched, $method ); + } + } else { + self::callFunction( $handler, $matched, $method ); + } + + /* Call all the function that are to be executed after routing */ + foreach( self::$afterFuncs as $afterFunc ) + if( $afterFunc[1] ) { + call_user_func_array( $afterFunc[0] , $afterFunc[1] ); + } else { + call_user_func( $afterFunc[0] ); + } + } + + /** + * Static function that helps you generate links effortlessly and pass parameters to them, thus enabling to generate dynamic links + * + * @param string $name name of the route for which the link has to be generated + * @param array $params An array of parameters that are replaced in the route if it contains wildcards + * For e.g. if route is /name/{i}/{a} and parameters passed are 1, aps then link generated will be /name/1/aps + */ + public static function route( $name, $params = array() ) + { + $href = null; + foreach ( self::$routes as $routePath => $routeDesc ) { + if( is_array( $routeDesc ) ){ + if( $name == $routeDesc[1] ){ + $href = $routePath; + for( $i = 0; $i < count($params); $i++){ + $href = preg_replace('#{(.*?)}#', $params[$i], $href, 1); + } + } + } + } + return $href; + } + + /** + * Static function to handle cases when route is not found, call handler of 404 if defined else + * sends a 404 header + */ + public static function handle404() + { + /* Call '404' route if it exists */ + if( isset ( self::$routes['404'] ) ) { + call_user_func( self::$routes['404'] ); + } else { + header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); + } + } + + /** + * Static function to handle both middlewares' call and main handler's call. + * + * @param array|string $handler Handler that will handle the routes call or middleware + * @param array $matched The parameters that we get from the route wildcard + * @return array $newParams The parameters return in the case of middleware if you intend to + * the wildcards that were originally passed, this newParams will + * be next passed to main handler + */ + public static function callFunction( $handler , $matched, $method ) + { + if ( $handler ) { + if ( is_callable( $handler ) ) { + $newParams = call_user_func_array( $handler, $matched ) ; + } else { + + /* Check if class exists in the case user is using RESTful pattern */ + + if( class_exists( $handler ) ) { + $instanceOfHandler = new $handler(); // Won't work in case of middleware since we aren't using RESTful in that + } else { + print_r('Class or function ' . $handler . ' not found'); + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); + die(); + } + } + } else { + self::handle404(); + } + + if( isset( $instanceOfHandler ) ) { + if( method_exists( $instanceOfHandler, $method ) ) { + try { + $newParams = call_user_func_array( array( $instanceOfHandler, $method ), $matched ); + } catch ( Exception $exception ){ + $string = str_replace("\n", ' ', var_export($exception, TRUE)); + error_log($string); //Log to error file only if display errors has been declared + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); + die(); + } + } + } + if( isset( $newParams ) && $newParams ) { + return $newParams; + } + } + + /** + * Static function to add functions that are to be excuted before each routing, must be called before Link::all + * + * @param string $funcName Name of the funtion to be called upon before + * @param array $params Array of parameters that are to be passed to before function, can be null but if not + * it must be an array + */ + public static function before( $funcName, $params = null ) + { + array_push( self::$beforeFuncs, [ $funcName, $params ]); + } + + /** + * Static function to add functions that are to be excuted after each routing, must be called before Link::all + * + * @param string $funcName Name of the funtion to be called upon after + * @param array $params Array of parameters that are to be passed to after function, can be null but if not + * it must be an array + */ + public static function after( $funcName, $params = null ) + { + array_push( self::$afterFuncs, [ $funcName, $params ]); + } +} \ No newline at end of file diff --git a/api/index.php b/api/index.php index 5858499e..b5f8c6e6 100644 --- a/api/index.php +++ b/api/index.php @@ -3,7 +3,7 @@ define('IN_SCRIPT', 1); define('HESK_PATH', '../'); require_once(__DIR__ . '/core/common.php'); -require(__DIR__ . '/../Link/src/Link.php'); +require(__DIR__ . '/Link.php'); require(__DIR__ . '/../hesk_settings.inc.php'); // Controllers