소스 검색

dev 2019-12

Max YO 5 년 전
부모
커밋
6177dd2319
4개의 변경된 파일211개의 추가작업 그리고 22개의 파일을 삭제
  1. 40 0
      helpers/helpers_form.php
  2. 60 7
      src/Routing/Route.php
  3. 72 15
      src/Routing/Router.php
  4. 39 0
      src/Routing/RoutesGroup.php

+ 40 - 0
helpers/helpers_form.php

@@ -0,0 +1,40 @@
+<?php
+
+function checkEmail($email) {
+	// format: abc123@cde456.aa | abc123@cde456.aaa
+	return !! preg_match(" /^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/ ", $email);
+}
+
+function checkPhone($phone) {
+	// format: 00 00 00 00 00,
+	return !! preg_match(" \^(\d\d\s){4}(\d\d)$\ ", $phone);
+}
+
+function checkUrl($url) {
+	// format http://www.example.com | www.example.com | http://subdomain.example.com | example.com
+	return !! preg_match(" \^(http|https|ftp):\/\/([\w]*)\.([\w]*)\.(com|net|org|biz|info|mobi|us|cc|bz|tv|ws|name|co|me)(\.[a-z]{1,3})?\z/i ", $url);
+}
+
+function checkLogin($login, $min_length=3, $max_length=16) {
+	// format: abc_123
+	return !! preg_match(" \^[a-zA-Z0-9_]{" . $min_length . "," . $max_length . "}$\ ", $login);
+}
+
+function checkDateFr($date) {
+	// format: 00/00/0000
+	return !! preg_match(" \^([0-3][0-9]})(/)([0-9]{2,2})(/)([0-3]{2,2})$\ ", $date);
+}
+
+function checkZipcode($zipcode) {
+	// format: 00000
+	return !! preg_match(" \^[0-9]{5,5}$\ ", $zipcode);
+}
+
+function checkIPv4($ip) {
+	return !! preg_match(" \^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$\ ", $ip);
+}
+
+function checkHexColor($color) {
+	return !! preg_match(" \^#(?:(?:[a-f\d]{3}){1,2})$/i ", $color);
+}
+

+ 60 - 7
src/Routing/Route.php

@@ -11,6 +11,9 @@ class Route
 	private $match_type = 'exact';
 	private $regex_params = [];
 	private $callback = null;
+	private $prefix = '';
+	private $prefix_callback = null;
+	private $nomatch_patterns = [];
 
 
 	public function __construct()
@@ -35,7 +38,7 @@ class Route
 	// Get route match url
 	public function getMatchUrl()
 	{
-		return $this->match_url;
+		return $this->prefix . $this->match_url;
 	}
 
 	// Set route match type (exact, startsWith, endsWith, regex, regexStartsWith, regexEndsWith)
@@ -75,44 +78,94 @@ class Route
 	}
 
 
+	// Set route prefix
+	public function setPrefix($prefix=null, $callback=null)
+	{
+		$this->prefix = $prefix;
+		$this->prefix_callback = $callback;
+	}
+
+	// Get route prefix
+	public function getPrefix()
+	{
+		return $this->prefix;
+	}
+
+	// Set route prefix callback
+	public function setPrefixCallback($callback)
+	{
+		$this->prefix_callback = $callback;
+	}
+
+	// Get route prefix callback
+	public function getPrefixCallback()
+	{
+		return $this->prefix_callback;
+	}
+
+
+	// Declare pattern to not match
+	public function notMatch($pattern)
+	{
+		if (! is_array($pattern)) {
+			$pattern = [$pattern];
+		}
+		foreach ($pattern as $p) {
+			$this->nomatch_patterns[] = $p;
+		}
+	}
+
+
+
 	// Check if route is matching the request_method and request_uri
 	public function match($request_method, $request_uri)
 	{
 		if (empty($this->methods) || in_array($request_method, $this->methods)) {
 
 			$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;
+					}
+				}
+			}
+
+			$match_url = $this->getMatchUrl();
 			
 			// exact match
 			if ($this->match_type == 'exact') {
-				if ($request_uri_short === $this->match_url) {
+				if ($request_uri_short === $match_url) {
 					return [];
 				}
 			}
 
 			// startsWith
 			if ($this->match_type == 'startsWith') {
-				if (substr($request_uri_short, 0, strlen($this->match_url)) === $this->match_url) {
+				if (substr($request_uri_short, 0, strlen($match_url)) === $match_url) {
 					return [];
 				}
 			}
 
 			// endsWith
 			if ($this->match_type == 'endsWith') {
-				if (substr($request_uri_short, -1 * strlen($this->match_url)) === $this->match_url) {
+				if (substr($request_uri_short, -1 * strlen($match_url)) === $match_url) {
 					return [];
 				}
 			}
 
 			// regex / regexStartsWith / regexEndsWith
 			if (in_array($this->match_type, ['regex', 'regexStartsWith', 'regexEndsWith'])) {
-				$match_pattern = '#^' . $this->match_url . '$#';
+				$match_pattern = '#^' . $match_url . '$#';
 	
 				if ($this->match_type == 'regexStartsWith') {
-					$match_pattern = '#^' . $this->match_url . '#';
+					$match_pattern = '#^' . $match_url . '#';
 				}
 
 				if ($this->match_type == 'regexEndsWith') {
-					$match_pattern = '#' . $this->match_url . '$#';
+					$match_pattern = '#' . $match_url . '$#';
 				}
 
 				if (preg_match($match_pattern, $request_uri_short, $regs)) {

+ 72 - 15
src/Routing/Router.php

@@ -6,28 +6,41 @@ namespace KarmaFW\Routing;
 class Router
 {
 	private static $routes = [];
+	private static $prefixes = [];
+	private static $routed_url = null;
 
 
 	// Register a route in the router
 	public static function add($methods, $url_match, $callback=null, $type_match='exact', $regex_params=[])
 	{
-		$route = new 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);
+		$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);
+			}
+
+			$routes_group->add($route);
+			self::$routes[] = $route;
 		}
 
-		self::$routes[] = $route;
 
-		return $route;
+		return $routes_group;
+		//return $route;
 	}
 
 
@@ -64,6 +77,11 @@ class Router
 				if ($debug) {
 					echo " => MATCH !<br />" . PHP_EOL;
 				}
+				
+				$prefix_callback = $route->getPrefixCallback();
+				if (! empty($prefix_callback) && is_callable($prefix_callback)) {
+					$prefix_callback();
+				}
 
 				$callback = $route->getCallback();
 				if (empty($callback)) {
@@ -71,6 +89,7 @@ class Router
 					return 0;
 
 				} else if (is_callable($callback)) {
+					self::$routed_url = $route;
 					self::routeRun($route, $callback, $request_method, $request_uri, $match_params);
 
 				} else {
@@ -131,8 +150,20 @@ class Router
 			return null;
 		}
 
+
+		$prefix = self::$routed_url ? self::$routed_url->getPrefix() : '';
+		if ($prefix) {
+			$route_prefix = $route->getPrefix();
+			$route->setPrefix($prefix);
+		}
+
 		$link = $route->getMatchUrl();
-		//echo "<pre>"; var_dump($route); exit;
+
+		if ($prefix) {
+			$route->setPrefix($route_prefix);
+		}
+
+
 		$link = rtrim($link, '$');
 		$link = ltrim($link, '^');
 
@@ -158,4 +189,30 @@ class Router
 		return $link;
 	}
 
+
+	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;
+	}
+
 }

+ 39 - 0
src/Routing/RoutesGroup.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace KarmaFW\Routing;
+
+
+class RoutesGroup
+{
+	protected $routes = [];
+
+	public function add($route)
+	{
+		$this->routes[] = $route;
+	}
+
+	public function setName($name)
+	{
+		foreach ($this->routes as $route) {
+			$route->setName($name);
+		}
+		return $this;
+	}
+	
+	public function setPrefix($prefix=null, $prefix_callback=null)
+	{
+		foreach ($this->routes as $route) {
+			$route->setPrefix($prefix, $prefix_callback);
+		}
+		return $this;
+	}
+
+	public function notMatch($pattern)
+	{
+		foreach ($this->routes as $route) {
+			$route->notMatch($pattern);
+		}
+		return $this;
+	}
+
+}