|
@@ -3,6 +3,7 @@
|
|
|
namespace KarmaFW\Routing;
|
|
namespace KarmaFW\Routing;
|
|
|
|
|
|
|
|
use \KarmaFW\WebApp;
|
|
use \KarmaFW\WebApp;
|
|
|
|
|
+use \KarmaFW\App\Pipe;
|
|
|
use \KarmaFW\Http\Request;
|
|
use \KarmaFW\Http\Request;
|
|
|
use \KarmaFW\Http\Response;
|
|
use \KarmaFW\Http\Response;
|
|
|
|
|
|
|
@@ -12,6 +13,7 @@ class Router
|
|
|
private static $routes = [];
|
|
private static $routes = [];
|
|
|
private static $routed_url = null;
|
|
private static $routed_url = null;
|
|
|
private static $config = [];
|
|
private static $config = [];
|
|
|
|
|
+ private static $matched_route = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function config($config)
|
|
public static function config($config)
|
|
@@ -33,6 +35,8 @@ class Router
|
|
|
{
|
|
{
|
|
|
$old_config = self::$config;
|
|
$old_config = self::$config;
|
|
|
|
|
|
|
|
|
|
+ //$middlewares = isset($config['middlewares']) ? $config['middlewares'] : [];
|
|
|
|
|
+
|
|
|
self::$config = $config;
|
|
self::$config = $config;
|
|
|
$callable();
|
|
$callable();
|
|
|
self::$config = $old_config;
|
|
self::$config = $old_config;
|
|
@@ -44,18 +48,23 @@ class Router
|
|
|
{
|
|
{
|
|
|
$route = new Route();
|
|
$route = new Route();
|
|
|
|
|
|
|
|
|
|
+ $default_get_prefix = null;
|
|
|
|
|
+ $default_get_prefix = function () {
|
|
|
|
|
+ return Router::getMatchedRoute()->getMatchedPrefix();
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
if (! empty(self::$config['prefix'])) {
|
|
if (! empty(self::$config['prefix'])) {
|
|
|
// ex: $prefix == "/fr"
|
|
// ex: $prefix == "/fr"
|
|
|
$route->setPrefix(self::$config['prefix'], 'exact', self::$config['prefix']);
|
|
$route->setPrefix(self::$config['prefix'], 'exact', self::$config['prefix']);
|
|
|
|
|
|
|
|
} else if (! empty(self::$config['prefix_regex'])) {
|
|
} else if (! empty(self::$config['prefix_regex'])) {
|
|
|
// ex: $prefix == "/[a-zA-Z0-9-]+"
|
|
// ex: $prefix == "/[a-zA-Z0-9-]+"
|
|
|
- $get_prefix = empty(self::$config['get_prefix']) ? null : self::$config['get_prefix'];
|
|
|
|
|
|
|
+ $get_prefix = empty(self::$config['get_prefix']) ? $default_get_prefix : self::$config['get_prefix'];
|
|
|
$route->setPrefix(self::$config['prefix_regex'], 'regex', $get_prefix);
|
|
$route->setPrefix(self::$config['prefix_regex'], 'regex', $get_prefix);
|
|
|
|
|
|
|
|
} else if (! empty(self::$config['prefix_array'])) {
|
|
} else if (! empty(self::$config['prefix_array'])) {
|
|
|
// ex: $prefix == ["/fr", "/us"]
|
|
// ex: $prefix == ["/fr", "/us"]
|
|
|
- $get_prefix = empty(self::$config['get_prefix']) ? null : self::$config['get_prefix'];
|
|
|
|
|
|
|
+ $get_prefix = empty(self::$config['get_prefix']) ? $default_get_prefix : self::$config['get_prefix'];
|
|
|
$route->setPrefix(self::$config['prefix_array'], 'array', $get_prefix);
|
|
$route->setPrefix(self::$config['prefix_array'], 'array', $get_prefix);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -63,6 +72,14 @@ class Router
|
|
|
$route->setBeforeCallback(self::$config['before_callback']);
|
|
$route->setBeforeCallback(self::$config['before_callback']);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (! empty(self::$config['middlewares'])) {
|
|
|
|
|
+ $route->setMiddlewares(self::$config['middlewares']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (isset(self::$config['continue'])) {
|
|
|
|
|
+ $route->setContinue(self::$config['continue']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$route->setMatchUrl($url_match);
|
|
$route->setMatchUrl($url_match);
|
|
|
$route->setCallback($callback);
|
|
$route->setCallback($callback);
|
|
|
$route->setMatchType($type_match);
|
|
$route->setMatchType($type_match);
|
|
@@ -154,6 +171,8 @@ class Router
|
|
|
echo " => MATCH !<br />" . PHP_EOL;
|
|
echo " => MATCH !<br />" . PHP_EOL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ self::$matched_route = $route;
|
|
|
|
|
+
|
|
|
$before_callback = $route->getBeforeCallback();
|
|
$before_callback = $route->getBeforeCallback();
|
|
|
if (! empty($before_callback)) {
|
|
if (! empty($before_callback)) {
|
|
|
$before_callback($route);
|
|
$before_callback($route);
|
|
@@ -216,6 +235,8 @@ class Router
|
|
|
$match = $route->match($request_method, $request_uri);
|
|
$match = $route->match($request_method, $request_uri);
|
|
|
|
|
|
|
|
if ($match) {
|
|
if ($match) {
|
|
|
|
|
+ self::$matched_route = $route;
|
|
|
|
|
+
|
|
|
$request->setRoute($route);
|
|
$request->setRoute($route);
|
|
|
|
|
|
|
|
$before_callback = $route->getBeforeCallback();
|
|
$before_callback = $route->getBeforeCallback();
|
|
@@ -227,17 +248,49 @@ class Router
|
|
|
if (empty($callback)) {
|
|
if (empty($callback)) {
|
|
|
// route found but no callback defined
|
|
// route found but no callback defined
|
|
|
//return 0;
|
|
//return 0;
|
|
|
|
|
+
|
|
|
|
|
+ $continue = $request->getRoute()->getContinue();
|
|
|
|
|
+
|
|
|
|
|
+ $response = self::requestRouteRun($route, null, $request, $response);
|
|
|
|
|
+
|
|
|
|
|
+ //$middlewares_result = true; // TODO: $request->getRoute()->getMiddlewaresResult();
|
|
|
|
|
+
|
|
|
|
|
+ if ($continue && $response->getStatus() == 200) {
|
|
|
|
|
+ //$response = $response_old;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($response->getStatus() !== 200) {
|
|
|
|
|
+ return $response;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return $response->setHtml('<h1>Page not Found</h1><p>Warning: route found but no callback defined</p>', 404);
|
|
return $response->setHtml('<h1>Page not Found</h1><p>Warning: route found but no callback defined</p>', 404);
|
|
|
|
|
|
|
|
} else if (is_callable($callback)) {
|
|
} else if (is_callable($callback)) {
|
|
|
// OK !
|
|
// OK !
|
|
|
self::$routed_url = $route;
|
|
self::$routed_url = $route;
|
|
|
|
|
+ //$response_old = clone $response;
|
|
|
$response = self::requestRouteRun($route, $callback, $request, $response);
|
|
$response = self::requestRouteRun($route, $callback, $request, $response);
|
|
|
|
|
+ //pre($response, 1); exit;
|
|
|
|
|
+
|
|
|
|
|
+ if ($request->getRoute()->getContinue() && $response->getStatus() == 200) {
|
|
|
|
|
+ //$response = $response_old;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return $response;
|
|
return $response;
|
|
|
|
|
|
|
|
} else {
|
|
} else {
|
|
|
// route found but callback is not callable
|
|
// route found but callback is not callable
|
|
|
//return null;
|
|
//return null;
|
|
|
|
|
+
|
|
|
|
|
+ $response = self::requestRouteRun($route, null, $request, $response);
|
|
|
|
|
+
|
|
|
|
|
+ if ($request->getRoute()->getContinue() && $response->getStatus() == 200) {
|
|
|
|
|
+ //$response = $response_old;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return $response->setHtml('<h1>Page not Found</h1><p>Warning: route callback is not callable</p>', 404);
|
|
return $response->setHtml('<h1>Page not Found</h1><p>Warning: route callback is not callable</p>', 404);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -251,22 +304,48 @@ class Router
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- public static function requestRouteRun(Route $route, callable $callback, Request $request, Response $response)
|
|
|
|
|
|
|
+ public static function requestRouteRun(Route $route, callable $callback=null, Request $request, Response $response)
|
|
|
{
|
|
{
|
|
|
$matched_params = $route->getMatchedParams();
|
|
$matched_params = $route->getMatchedParams();
|
|
|
|
|
+ $middlewares_result = ['success' => false];
|
|
|
|
|
|
|
|
- if (gettype($callback) == 'array') {
|
|
|
|
|
|
|
+ if (empty($callback)) {
|
|
|
|
|
+ // do nothing
|
|
|
|
|
+ $route->addMiddleware(function () use ($request, $response, &$middlewares_result) {
|
|
|
|
|
+ $middlewares_result['success'] = true;
|
|
|
|
|
+ return $response;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ } else if (gettype($callback) == 'array') {
|
|
|
//echo " => ARRAY !<br />" . PHP_EOL;
|
|
//echo " => ARRAY !<br />" . PHP_EOL;
|
|
|
//pre($callback, 1);
|
|
//pre($callback, 1);
|
|
|
$controller = new $callback[0]($request, $response);
|
|
$controller = new $callback[0]($request, $response);
|
|
|
WebApp::$controller = $controller;
|
|
WebApp::$controller = $controller;
|
|
|
|
|
|
|
|
- $route_response = call_user_func([$controller, $callback[1]], $matched_params);
|
|
|
|
|
|
|
+ //$route_response = call_user_func([$controller, $callback[1]], $matched_params);
|
|
|
|
|
+ $route->addMiddleware(function () use ($controller, $callback, $matched_params, &$middlewares_result) {
|
|
|
|
|
+ $middlewares_result['success'] = true;
|
|
|
|
|
+ return call_user_func([$controller, $callback[1]], $matched_params);
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
} else {
|
|
} else {
|
|
|
//echo " => FUNCTION !<br />" . PHP_EOL;
|
|
//echo " => FUNCTION !<br />" . PHP_EOL;
|
|
|
//pre($callback, 1);
|
|
//pre($callback, 1);
|
|
|
- $route_response = $callback($request, $response, $matched_params);
|
|
|
|
|
|
|
+ //$route_response = $callback($request, $response, $matched_params);
|
|
|
|
|
+ $route->addMiddleware(function () use ($callback, $request, $response, $matched_params, &$middlewares_result) {
|
|
|
|
|
+ $middlewares_result['success'] = true;
|
|
|
|
|
+ return $callback($request, $response, $matched_params);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $pipe = new Pipe($route->getMiddlewares());
|
|
|
|
|
+ $route_response = $pipe->next($request, $response);
|
|
|
|
|
+
|
|
|
|
|
+ //pre($middlewares_result);
|
|
|
|
|
+ if (! $middlewares_result['success']) {
|
|
|
|
|
+ // TODO
|
|
|
|
|
+ $request->getRoute()->setContinue(false); // ?
|
|
|
|
|
+ //$request->getRoute()->setMiddlewaresResult(true); // ?
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (is_object($route_response) && get_class($route_response) === Response::class) {
|
|
if (is_object($route_response) && get_class($route_response) === Response::class) {
|
|
@@ -318,8 +397,12 @@ class Router
|
|
|
//pre($route, 1);
|
|
//pre($route, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
- $get_prefix = $route->getCallbackGetPrefix();
|
|
|
|
|
- //pre($get_prefix, 0, 'get_prefix: ');
|
|
|
|
|
|
|
+ $get_prefix = null;
|
|
|
|
|
+ if (true) {
|
|
|
|
|
+ //$get_prefix = self::$matched_route->getCallbackGetPrefix();
|
|
|
|
|
+ $get_prefix = $route->getCallbackGetPrefix();
|
|
|
|
|
+ //pre($get_prefix, 0, 'get_prefix: ');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
$link = $route->getMatchUrl();
|
|
$link = $route->getMatchUrl();
|
|
|
if ($get_prefix) {
|
|
if ($get_prefix) {
|
|
@@ -366,4 +449,10 @@ class Router
|
|
|
return self::$routed_url;
|
|
return self::$routed_url;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public static function getMatchedRoute()
|
|
|
|
|
+ {
|
|
|
|
|
+ return self::$matched_route;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|