ACC SHELL
<?php
// Author: Jakub Macek, CZ; Copyright: Poski.com s.r.o.; Code is 100% my work. Do not copy.
class Router
{
const TYPE_EQUALS = 0;
const TYPE_REGEX = 1;
public static $routes = array();
public static $unused = array();
public static function addE($pattern, $locale, $location, $template = null, $simple = false)
{
if ($locale != L)
return;
$args = array();
if ($locale !== null)
$args['page.locale'] = $locale;
if ($location !== null)
$args['page.location'] = $location;
if ($simple !== null)
$args['page.simple'] = $simple;
if ($template !== null)
$args['template'] = $template;
self::add(self::TYPE_EQUALS, $pattern, $args);
}
public static function addR($pattern, $locale, $location, $template = null, $options = array())
{
if ($locale != L)
return;
$args = array();
if ($locale !== null)
$args['page.locale'] = $locale;
if ($location !== null)
$args['page.location'] = $location;
if ($template !== null)
$args['template'] = $template;
self::add(self::TYPE_REGEX, $pattern, $args, $options);
}
public static function add($type, $pattern, $args = array(), $options = array())
{
$item = array(
'type' => $type,
'pattern' => $pattern,
'args' => $args,
'options' => $options,
);
self::$routes[$pattern] = $item;
self::$unused[$pattern] = $item;
}
public static function process($restart = false)
{
if ($restart)
self::$unused = self::$routes;
$result = null;
foreach (self::$unused as $route)
{
if (page()->location)
$ok = (page()->location == @$route['args']['page.location']);
else
{
$ok = false;
$matches = array();
if ($route['type'] == self::TYPE_EQUALS)
$ok = ($route['pattern'] == page()->URL);
if ($route['type'] == self::TYPE_REGEX)
$ok = (boolean) preg_match('~^'.$route['pattern'].'$~', page()->URL, $matches);
}
if ($ok)
{
foreach ($route['args'] as $k => $v)
if ($k == 'template')
View::frame($v);
else
Application::setG($k, $v);
foreach ($route['options'] as $k => $v)
$_REQUEST[$v] = @$matches[$k+1];
page()->location();
$result = $route;
break;
}
}
self::$unused = array();
return $result;
}
public static function find($location, $options = null)
{
if ($options === null)
{
$result = array();
foreach (self::$routes as $route)
if (isset($route['args']['page.location']) && ($route['args']['page.location'] == $location))
$result[] = $route;
return $result;
}
else
foreach (self::$routes as $route)
if (isset($route['args']['page.location']) && ($route['args']['page.location'] == $location))
if (array_keys($options) == $route['options'])
return self::prepare($route, $options);
return null;
}
public static function prepare($route, $options = array())
{
$url = $route['pattern'];
$opts = $route['options'];
while ($opt = array_shift($opts))
{
$l = strpos($url, '(');
$r = strpos($url, ')');
if (($l !== false) && ($r !== false))
$url = substr_replace($url, @$options[$opt], $l, $r - $l + 1);
}
$url = Event::e('router.prepare.after')->call(array('route' => $route), $url, true);
$route['link'] = $url;
return $route;
}
public static function urlRelative($location, $options = array(), $fallback = null)
{
$route = self::find($location, $options);
if (!$route)
{
if ($fallback === null)
$fallback = '('.$location . ')';
return $fallback;
}
return $route['link'];
}
public static function url($location, $options = array(), $fallback = null)
{
$route = self::find($location, $options);
if (!$route)
{
if ($fallback === null)
$fallback = '('.$location . ')';
return $fallback;
}
return LPATH.$route['link'];
}
public static function urlGeneric($location, $options = array(), $fallback = null)
{
$routes = self::find($location, null);
if (!$routes)
{
if ($fallback === null)
$fallback = '('.$location . ')';
return $fallback;
}
foreach ($routes as $route) // HACK will take only first
{
$route = self::prepare($route, $options);
return LPATH.$route['link'];
}
}
public static function linkToCurrent()
{
$result = explode('?', $_SERVER['REQUEST_URI']);
if (isset($result[1]))
{
$result[1] = U::urlParametersToArray($result[1]);
foreach (array('page.viewstate', 'page.panel', 'page_viewstate', 'page_panel') as $k)
unset($result[1][$k]);
if ($temp0 = page()->viewstate)
$result[1]['page.viewstate'] = $temp0;
$result[1] = U::arrayToUrlParameters($result[1]);
}
$result = implode('?', $result);
return $result;
}
}
?>
ACC SHELL 2018