Max F 5 سال پیش
والد
کامیت
728b94e2e5
3فایلهای تغییر یافته به همراه152 افزوده شده و 93 حذف شده
  1. 15 2
      src/Database/Sql/SqlTableModel.php
  2. 82 32
      src/Routing/Route.php
  3. 55 59
      src/Routing/Router.php

+ 15 - 2
src/Database/Sql/SqlTableModel.php

@@ -192,8 +192,21 @@ class SqlTableModel
 			$where = [];
 		}
 
-		foreach (static::$primary_key as $key) {
-			if (! isset($pk_where[$key])) {
+		foreach (static::$primary_key as $key_idx => $key) {
+			if (! array_key_exists($key, $pk_where)) {
+				//if (count($pk_where) == count(static::$primary_key) && isset($pk_where[$key_idx])) {
+				if (count($pk_where) == count(static::$primary_key) && array_key_exists($key_idx, $pk_where)) {
+					//pre(static::$primary_key);
+					$pk_where = array_combine(static::$primary_key, $pk_where);
+					//pre($pk_where, 0, 'pk_where updated: ');
+				} else {
+					//pre($key_idx, 0, 'key_idx: ');
+					//pre($pk_where, 0, 'pk_where KO: ');
+				}
+			}
+			if (! array_key_exists($key, $pk_where)) {
+				//exit;
+				//pre(static::$primary_key); pre($pk_where, 1);
 				throw new \Exception("missing value for primary_key " . $key . " in " . get_called_class(), 1);
 			}
 			$value = $pk_where[$key];

+ 82 - 32
src/Routing/Route.php

@@ -12,10 +12,12 @@ class Route
 	private $match_url = '';
 	private $match_type = 'exact';
 	private $regex_params = [];
+	private $before_callback = null;
 	private $callback = null;
 	private $prefix = '';
-	private $prefix_callback = null;
-	private $nomatch_patterns = [];
+	private $prefix_match_type = null;
+	private $get_prefix = null;
+	private $matched_prefix = '';
 	private $matched_params = [];
 
 
@@ -53,7 +55,8 @@ class Route
 	// Get route match url
 	public function getMatchUrl()
 	{
-		return $this->prefix . $this->match_url;
+		//return $this->prefix . $this->match_url;
+		return $this->match_url;
 	}
 
 	// Set route match type (exact, startsWith, endsWith, regex, regexStartsWith, regexEndsWith)
@@ -73,6 +76,7 @@ class Route
 		return $this->regex_params;
 	}
 
+
 	public function setMatchedParams(array $matched_params)
 	{
 		$this->matched_params = $matched_params;
@@ -107,45 +111,51 @@ class Route
 	}
 
 
-	// Set route prefix
-	public function setPrefix($prefix=null, $callback=null)
+	// Get route before_callback
+	public function setBeforeCallback($before_callback)
 	{
-		$this->prefix = $prefix;
-		$this->prefix_callback = $callback;
+		$this->before_callback = $before_callback;
 	}
 
-	// Get route prefix
-	public function getPrefix()
+	// Get route before_callback
+	public function getBeforeCallback()
 	{
-		return $this->prefix;
+		return $this->before_callback;
 	}
 
-	// Set route prefix callback
-	public function setPrefixCallback($callback)
+
+	// Set route prefix
+	public function setPrefix($prefix=null, $match_type=null, $get_prefix=null)
 	{
-		$this->prefix_callback = $callback;
+		$this->prefix = $prefix;
+		$this->prefix_match_type = $match_type;
+		$this->get_prefix = $get_prefix;
 	}
 
-	// Get route prefix callback
-	public function getPrefixCallback()
+	// Get route prefix
+	public function getPrefix()
 	{
-		return $this->prefix_callback;
+		return $this->prefix;
 	}
 
 
-	// Declare pattern to not match
-	public function notMatch($pattern)
+	// Get route get_prefix
+	public function getCallbackGetPrefix()
 	{
-		if (! is_array($pattern)) {
-			$pattern = [$pattern];
+		if (empty($this->get_prefix)) {
+			return null;
 		}
-		foreach ($pattern as $p) {
-			$this->nomatch_patterns[] = $p;
+
+		$func = $this->get_prefix;
+		if (! is_callable($func)) {
+			//error500("get_prefix is not callable");
+			return null;
 		}
+		//pre($func, 1);
+		return $func();
 	}
 
 
-
 	// Check if route is matching the request_method and request_uri
 	public function match($request_method, $request_uri)
 	{
@@ -153,35 +163,66 @@ class Route
 
 			$request_uri_short = explode('?', $request_uri)[0];
 
-			// on verifie qu'il n'y a pas un pattern de nomatching
-			if ($this->nomatch_patterns) {
-				foreach ($this->nomatch_patterns as $pattern) {
-					if (preg_match('#' . $pattern . '#', $request_uri_short, $regs)) {
-						return null;
+			$matched_params = [];
+			
+			$prefix = $this->getPrefix();
+			if ($prefix) {
+				if ($this->prefix_match_type == 'regex') {
+					$match_pattern = '#^(' . $prefix . ')#';
+					if (preg_match($match_pattern, $request_uri_short, $regs)) {
+						$this->matched_prefix = $regs[1];
+						$matched_params['prefix'] = $this->matched_prefix;
+						//pre($matched_prefix, 1, 'regex matched_prefix: ');
+					}
+
+				} else if ($this->prefix_match_type == 'array') {
+					foreach ($prefix as $prefix_value) {
+						if (strpos($request_uri_short, $prefix_value) === 0) {
+							$this->matched_prefix = $prefix_value;
+							$matched_params['prefix'] = $this->matched_prefix;
+							//pre($matched_prefix, 1, 'array matched_prefix: ');
+						}
 					}
+
+				} else {
+					if (strpos($request_uri_short, $prefix) === 0) {
+						$this->matched_prefix = $prefix;
+						$matched_params['prefix'] = $this->matched_prefix;
+						//pre($matched_prefix, 1, 'exact matched_prefix: ');
+					}
+
 				}
 			}
 
+
+			if (! empty($this->matched_prefix)) {
+				$request_uri_short = substr($request_uri_short, strlen($this->matched_prefix));
+			}
+			
+
 			$match_url = $this->getMatchUrl();
 			
 			// exact match
 			if ($this->match_type == 'exact') {
 				if ($request_uri_short === $match_url) {
-					return [];
+					$this->setMatchedParams($matched_params);
+					return true;
 				}
 			}
 
 			// startsWith
 			if ($this->match_type == 'startsWith') {
 				if (substr($request_uri_short, 0, strlen($match_url)) === $match_url) {
-					return [];
+					$this->setMatchedParams($matched_params);
+					return true;
 				}
 			}
 
 			// endsWith
 			if ($this->match_type == 'endsWith') {
 				if (substr($request_uri_short, -1 * strlen($match_url)) === $match_url) {
-					return [];
+					$this->setMatchedParams($matched_params);
+					return true;
 				}
 			}
 
@@ -204,8 +245,17 @@ class Route
 					if (! empty($this->regex_params)) {
 						$args = array_combine($this->regex_params, $args);
 					}
+					//pre($args, 1);
+
+					if (! empty($this->matched_prefix)) {
+						$matched_params = array_merge($matched_params, $args);
+						//$matched_params['prefix'] = $this->matched_prefix;
+					}
+					//pre($matched_params, 1);
+
 
-					return $args;
+					$this->setMatchedParams($matched_params);
+					return true;
 				}
 			}
 

+ 55 - 59
src/Routing/Router.php

@@ -6,41 +6,52 @@ namespace KarmaFW\Routing;
 class Router
 {
 	private static $routes = [];
-	private static $prefixes = [];
 	private static $routed_url = null;
+	private static $config = [];
+
+
+	public static function config($config)
+	{
+		self::$config = $config;
+	}
 
 
 	// Register a route in the router
 	public static function add($methods, $url_match, $callback=null, $type_match='exact', $regex_params=[])
 	{
-		$prefixes = self::$prefixes ?: ['' => null];
-
-		$routes_group = new RoutesGroup;
-
-		foreach ($prefixes as $prefix => $prefix_callback) {
-			$route = new Route();
-
-			$route->setPrefix($prefix, $prefix_callback);
-			//$route->setPrefixCallback($prefix_callback);
-			$route->setMatchUrl($url_match);
-			$route->setCallback($callback);
-			$route->setMatchType($type_match);
-			$route->setRegexParams($regex_params);
-			
-			if (! is_array($methods)) {
-				$methods = [$methods];
-			}
-			foreach ($methods as $method) {
-				$route->setMethod($method);
-			}
+		$route = new Route();
+
+		if (! empty(self::$config['prefix'])) {
+			$route->setPrefix(self::$config['prefix'], 'exact', self::$config['prefix']);
+		
+		} else if (! empty(self::$config['prefix_regex'])) {
+			$get_prefix = empty(self::$config['get_prefix']) ? null : self::$config['get_prefix'];
+			$route->setPrefix(self::$config['prefix_regex'], 'regex', $get_prefix);
+
+		} else if (! empty(self::$config['prefix_array'])) {
+			$get_prefix = empty(self::$config['get_prefix']) ? null : self::$config['get_prefix'];
+			$route->setPrefix(self::$config['prefix_array'], 'array', $get_prefix);
+		}
+
+		if (! empty(self::$config['before_callback'])) {
+			$route->setBeforeCallback(self::$config['before_callback']);
+		}
 
-			$routes_group->add($route);
-			self::$routes[] = $route;
+		$route->setMatchUrl($url_match);
+		$route->setCallback($callback);
+		$route->setMatchType($type_match);
+		$route->setRegexParams($regex_params);
+		
+		if (! is_array($methods)) {
+			$methods = [$methods];
+		}
+		foreach ($methods as $method) {
+			$route->setMethod($method);
 		}
 
+		self::$routes[] = $route;
 
-		return $routes_group;
-		//return $route;
+		return $route;
 	}
 
 
@@ -74,19 +85,19 @@ class Router
 			$route->setCalledMethod($request_method);
 			$route->setCalledUrl($request_uri);
 
-			$match_params = $route->match($request_method, $request_uri);
+			$match = $route->match($request_method, $request_uri);
 
-			if (! is_null($match_params)) {
+			if ($match) {
 				if ($debug) {
 					echo " => MATCH !<br />" . PHP_EOL;
 				}
-				
-				$prefix_callback = $route->getPrefixCallback();
-				if (! empty($prefix_callback) && is_callable($prefix_callback)) {
-					$prefix = $route->getPrefix();
-					$prefix_callback($prefix);
+
+				$before_callback = $route->getBeforeCallback();
+				if (! empty($before_callback)) {
+					$before_callback($route);
 				}
 
+
 				$callback = $route->getCallback();
 				if (empty($callback)) {
 					// Do nothing
@@ -94,7 +105,7 @@ class Router
 
 				} else if (is_callable($callback)) {
 					self::$routed_url = $route;
-					self::routeRun($route, $callback, $request_method, $request_uri, $match_params);
+					self::routeRun($route, $callback, $request_method, $request_uri);
 
 				} else {
 					// Error: callback not callable
@@ -109,21 +120,21 @@ class Router
 		return false;
 	}
 
-	public static function routeRun($route, $callback, $request_method, $request_uri, $match_params)
+
+	public static function routeRun($route, $callback, $request_method, $request_uri)
 	{
-		$route->setMatchedParams($match_params);
+		$matched_params = $route->getMatchedParams();
 
 		if (gettype($callback) == 'array') {
 			//echo " => ARRAY !<br />" . PHP_EOL;
 			//pre($callback, 1);
 			$class = new $callback[0]($route, $request_method, $request_uri);
-			call_user_func([$class, $callback[1]], $match_params);
+			call_user_func([$class, $callback[1]], $matched_params);
 
 		} else {
 			//echo " => FUNCTION !<br />" . PHP_EOL;
 			//pre($callback, 1);
-			//$callback($route, $request_method, $request_uri);
-			$callback($route, $match_params);
+			$callback($route, $matched_params);
 		}
 
 
@@ -161,19 +172,17 @@ class Router
 		if (empty($route) || $route === true) {
 			return null;
 		}
+		//pre($route, 1);
 
 
-		$prefix = self::$routed_url ? self::$routed_url->getPrefix() : '';
-		if ($prefix) {
-			$route_prefix = $route->getPrefix();
-			$route->setPrefix($prefix);
-		}
+		$get_prefix = $route->getCallbackGetPrefix();
+		//pre($get_prefix, 0, 'get_prefix: ');
 
 		$link = $route->getMatchUrl();
-
-		if ($prefix) {
-			$route->setPrefix($route_prefix);
+		if ($get_prefix) {
+			$link = $get_prefix . $link;
 		}
+		//pre($link, 1, 'link: ');
 
 
 		$link = rtrim($link, '$');
@@ -204,24 +213,11 @@ class Router
 
 	public static function printRoutes()
 	{
-		dump(self::$prefixes);
 		dump(self::$routes);
 		exit;
 	}
 
 
-	public static function prefix($prefix, $callback)
-	{
-		self::$prefixes[$prefix] = $callback;
-	}
-
-
-	public static function clearPrefixes()
-	{
-		self::$prefixes = [];
-	}
-
-
 	public static function getRoutedUrl()
 	{
 		return self::$routed_url;