All Data Structures Namespaces Files Functions Variables
Router.php
Go to the documentation of this file.
<?php
namespace Tools;
class Router
{
private $rootPath;
private $rootUrl;
private $requestUrl;
private $routes;
private $context;
private $routeParams;
private $overridden;
public function __construct($server, $context)
{
$pos = strrpos($server["SCRIPT_NAME"], "/");
$relativePath = (($pos === FALSE) ? "" : substr($server["SCRIPT_NAME"], 0, $pos));
$this->rootPath = $server["DOCUMENT_ROOT"] . $relativePath . "/";
$this->rootUrl = $server["REQUEST_SCHEME"] . "://" . $server["HTTP_HOST"] . $relativePath ."/";
$this->requestUrl = substr($server["REQUEST_URI"], count($this->rootUrl) -1);
$this->context = $context;
$this->routes = array();
$this->overridden = null;
}
public function init($server)
{
$siteUrl = \Entity\Config::getConfig(null, "siteUrl");
// @codeCoverageIgnoreStart
// This code is tested under another process
if ($siteUrl != $server["HTTP_HOST"] && $siteUrl !== null)
{
header("location: http://{$siteUrl}{$server['REQUEST_URI']}");
die;
}
// @codeCoverageIgnoreEnd
}
public function serveUrl()
{
//TODO trigger hook GET, POST
$this->prepareUrl();
$requestParams = explode("/", $this->requestUrl);
foreach ($this->routes as $i)
{
$routeParams = explode("/", $i[0]);
$p = $this->routeMatch($requestParams, $routeParams);
if ($p === false)
continue;
$controller = $this->createController($i[1], $p);
if ($controller)
return $controller;
}
return false;
}
private function createController($className, $params)
{
if (!class_exists($className))
return false;
$this->routeParams = $params;
$result = null;
try
{
$result = new $className($this->context, $params);
if (!($result instanceof \Tools\AController))
return false;
}
catch (\Exception\Error404 $e)
{
return false;
}
return $result;
}
private function routeMatch($request, $route)
{
$i = count($request);
$params = array();
if ($i != count($route))
return false;
while ($i)
{
$i--;
if ($route[$i] == '' && $request[$i] == '')
continue;
if ($route[$i] == '' || $request[$i] == '')
return false;
if ($route[$i][0] != ':' && ($route[$i] != $request[$i]))
return false;
if ($route[$i][0] == ':')
$params[$route[$i]] = $request[$i];
$params[$i -1] = $request[$i];
}
return array_reverse($params);
}
private function prepareUrl()
{
$fetcher = new \Entity\Cms();
$pages = $fetcher->selects(null, array("order"));
foreach ($pages as $i)
$this->doRouteAdd($i->shurl, $i->controller);
}
private function doRouteAdd($route, $controller)
{
$this->routes[] = array($route, $controller);
}
public function routeAdd($route, $controller)
{
if (!$this->context->hookManager->isInHook("routerSetup"))
throw new \Exception("You can only add routes from `routerSetup' hook");
$this->doRouteAdd($route, $controller);
}
public function overrideUrl($type, $value)
{
if (!$this->context->isTestingEnvironment())
return false;
$this->overridden[$type] = $value;
return true;
}
public function __get($key)
{
if (isset($this->overridden) && in_array($key, array("modulesPath")))
return $this->overridden[$key];
switch ($key)
{
case "rootPath": return $this->rootPath; break;
case "rootUrl": return $this->rootUrl; break;
case "modulesPath": return $this->rootPath."content/modules/"; break;
case "modulesUrl": return $this->rootUrl."content/modules/"; break;
case "themesPath": return $this->rootPath."content/theme/"; break;
case "themesUrl": return $this->rootUrl."content/theme/"; break;
}
throw new \Exception("Cannot access attribute {$key}");
}
}
Tools\Router\doRouteAdd
doRouteAdd($route, $controller)
Definition: Router.php:205
Tools\Router\prepareUrl
prepareUrl()
Definition: Router.php:193
Tools\Router\serveUrl
serveUrl()
Definition: Router.php:118
Tools\Router\$routes
$routes
Definition: Router.php:34
Tools\Router\$overridden
$overridden
Definition: Router.php:57
Tools\Router\$rootPath
$rootPath
Definition: Router.php:16
Tools\Router\__get
__get($key)
Definition: Router.php:246
Tools\Router\$routeParams
$routeParams
Definition: Router.php:51
Exception
Definition: Error404.php:3
Tools\Router
Definition: Router.php:9
Tools\Router\overrideUrl
overrideUrl($type, $value)
Definition: Router.php:235
Tools\Router\$requestUrl
$requestUrl
Definition: Router.php:29
Tools\Router\$rootUrl
$rootUrl
Definition: Router.php:23
Entity\Config\getConfig
static getConfig($lang=null, $key=null, $defaultValue=null)
Definition: Config.php:43
Tools\Router\__construct
__construct($server, $context)
Definition: Router.php:82
Tools\Router\createController
createController($className, $params)
Definition: Router.php:142
Tools
Definition: AController.php:3
Tools\Router\$context
$context
Definition: Router.php:41
Tools\Router\init
init($server)
Definition: Router.php:99
Tools\Router\routeAdd
routeAdd($route, $controller)
Definition: Router.php:220
Tools\Router\routeMatch
routeMatch($request, $route)
Definition: Router.php:167
Tools\AController
Definition: AController.php:8