44 public static function setRouteHandler($route, $handlerModule, $handlerClass, array $handlerParameters)
48 if(!is_string($route) || $route ==
'')
50 if(!is_string($handlerModule) || $handlerModule ==
'')
52 if(!is_string($handlerClass) || $handlerClass ==
'')
56 if(isset(static::$routeTable[$route]))
58 if ( $handlerModule === static::$routeTable[$route][
'MODULE']
59 && $handlerClass === static::$routeTable[$route][
'CLASS']
60 && $handlerParameters == static::$routeTable[$route][
'PARAMETERS']
68 $allowSlashes = ($handlerParameters[
'allowSlashes'] ??
'N') ===
'Y';
69 static::$routeTable[$route][
'ROUTE'] = $route;
70 static::$routeTable[$route][
'REGEXP'] = static::convertRouteToRegexp($route, $allowSlashes);
71 static::$routeTable[$route][
'MODULE'] = $handlerModule;
72 static::$routeTable[$route][
'CLASS'] = $handlerClass;
73 static::$routeTable[$route][
'PARAMETERS'] = $handlerParameters;
75 static::persistRoute($route, $newRoute);
90 foreach(static::$routeTable as $routeRecord)
92 if(preg_match($routeRecord[
'REGEXP'], $urlPath, $matches))
94 $result = $routeRecord;
96 foreach($result[
'PARAMETERS'] as $parameterName => &$parameterValue)
98 if(mb_strpos($parameterValue,
'$') === 0)
100 $variableName = mb_substr($parameterValue, 1);
101 if(isset($matches[$variableName]))
103 $parameterValue = $matches[$variableName];
107 unset($parameterValue);
110 if (mb_strlen($uriQuery) > 0)
112 $uriQueryParams = static::parseQueryParams($uriQuery);
113 foreach ($result[
'PARAMETERS'] as $parameterName => &$parameterValue)
115 if (mb_strpos($parameterValue,
'$') === 0)
117 $variableName = mb_substr($parameterValue, 1);
118 if (isset($uriQueryParams[$variableName]))
120 $parameterValue = $uriQueryParams[$variableName];
124 unset($parameterValue);
136 $data = preg_replace_callback(
137 '/(?:^|(?<=&))[^=[]+/',
140 return bin2hex(urldecode($match[0]));
145 parse_str($data, $values);
147 return array_combine(array_map(
'hex2bin', array_keys($values)), $values);
154 protected static function init()
156 if(static::$initialized)
161 if(static::$managedCache->read(static::CACHE_TTL, static::CACHE_ID))
163 static::$routeTable = (array)static::$managedCache->
get(static::CACHE_ID);
167 $queryResult = RouteTable::getList(array(
168 'select' => array(
'*')
171 while($routeRecord = $queryResult->fetch())
173 $allowSlashes = ($routeRecord[
'PARAMETERS'][
'allowSlashes'] ??
'N') ===
'Y';
174 $routeRecord[
'REGEXP'] = static::convertRouteToRegexp($routeRecord[
'ROUTE'], $allowSlashes);
175 static::$routeTable[$routeRecord[
'ROUTE']] = $routeRecord;
178 uksort(static::$routeTable,
function($a, $b)
180 $lengthOfA = mb_strlen($a);
181 $lengthOfB = mb_strlen($b);
182 if($lengthOfA > $lengthOfB)
184 else if($lengthOfA == $lengthOfB)
190 static::$managedCache->set(static::CACHE_ID, static::$routeTable);
193 static::$initialized =
true;
205 static::invalidateRouteCache();
208 'ROUTE' => static::$routeTable[$route][
'ROUTE'],
209 'MODULE' => static::$routeTable[$route][
'MODULE'],
210 'CLASS' => static::$routeTable[$route][
'CLASS'],
215 $addResult = RouteTable::merge($routeData);
216 if($addResult->isSuccess())
218 static::$routeTable[$route][
'ID'] = $addResult->getId();
220 static::$routeTable[$route][
'ID'],
222 'PARAMETERS' => static::$routeTable[$route][
'PARAMETERS']
226 $result = $addResult->isSuccess();
230 $routeData[
'PARAMETERS'] = static::$routeTable[$route][
'PARAMETERS'];
231 $updateResult = RouteTable::update(static::$routeTable[$route][
'ID'], $routeData);
232 $result = $updateResult->isSuccess();