HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/inventory.breadsecret.com/class/Routing/Route.php
<?php
namespace Routing;

class Route {
	
	const Request_Method = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'CONNECT', 'OPTIONS', 'TRACE'];
	public static $routeList;
	public static $currRoute;
	public static $currRouteName;
	
	public static function add($method, $path, $target, $name="") {
		self::$routeList = self::$routeList ?? [];
		self::$routeList[$method] = self::$routeList[$method] ?? [];
		self::$routeList[$method][$name] = new Route($path, $target);;
	}
	
	public static function find($request) {
		$reqPath = $request->requestUrl();
		if (!isset(self::$routeList[$request->method])) return null;
		$routeList = self::$routeList[$request->method];
		//if (array_key_exists($reqPath, $routeList)) return $routeList[$reqPath];
		
		$slashCount = substr_count($reqPath, '/');
		$routeList = array_filter($routeList, function ($route) use ($slashCount) {
			return (substr_count($route->path, '/') == $slashCount);
		});
		
		foreach ($routeList as $key => $route) {
			$regex = preg_replace('/{([^{}]*)}/', '([^/]*)', $route->path);
			$count = preg_match_all('|^'.$regex.'$|', $reqPath, $values);
			if ($count) {
				preg_match_all('/{([^{}]*)}/', $route->path, $matches);
				$val = [];
				for ($i = 1; $i < count($values); $i++) 
					$val[] = $values[$i][0];
				$reqGet = array_combine($matches[1], $val);
				$request->get->addDataArr($reqGet);
				self::$currRoute = $route;
				self::$currRouteName = $key;
				return $route;
			}
		}
		return null; // Throw Exception for Handling
	}
	
	public static function getRouteByName($name, $method = '') {
		if (!empty($name)) {
			if (empty($method)) {
				foreach (self::Request_Method as $reqMet)
					if (isset(self::$routeList[$reqMet][$name]))
						return self::$routeList[$reqMet][$name];
			} else 
				return self::$routeList[$method][$name];
		}
		return null;
	}
	
	private $path;
	private $className;
	private $classMethod;
	
	public function __construct($path, $target) {
		$this->path = $path;
		list($this->className, $this->classMethod) = explode('@', $target);
	}
	
	public function __get($name) { 
		if (property_exists($this, $name)) 
			return $this->$name; 
		return null; 
	}
	
	public function path(Array $value = []) {
		$count = preg_match_all('/{([^{}]*)}/', $this->path, $matches);
		$path = $this->path;
		if ($count) {
			$path = preg_replace_callback('/{([^{}]*)}/', function ($m) use (&$value) {
				if (isset($value[$m[1]])) {
					$ret = $value[$m[1]];
					unset($value[$m[1]]);
					return $ret;
				}
				return '';
			}, $this->path);
		}
		if (count($value)) {
			array_walk($value, function (&$val, $key) {
				$val = urlencode($key).'='.urlencode($val);
			});
			$path .= '?'.implode('&', $value);
		}
		return $path;
	}
	
	public function __toString() {
		return "$this->path=>$this->className.$this->classMethod";
	}
}