<?php
namespace dns\system;
+use dns\system\cache\builder\ControllerCacheBuilder;
/**
* @author Jan Altensen (Stricted)
* @copyright 2013-2015 Jan Altensen (Stricted)
*/
class RequestHandler {
+ protected $pattern = "";
+ protected $routeData = array();
+ protected $controllers = array();
+
/**
* init RequestHandler
*/
public function __construct ($module = '') {
- $className = "";
- $pages = glob(DNS_DIR.'/lib/'.(empty($module) ? '' : $module.'/').'page/*Page.class.php');
- if (isset($_GET["page"]) && !empty($_GET["page"])) {
- if (strtolower($_GET["page"]) != "abstract") {
- foreach ($pages as $page) {
- $page = str_replace('Page.class.php', '', basename($page));
- if (strtolower($_GET["page"]) == strtolower($page)) {
- $class = "\\dns".(empty($module) ? '' : "\\".$module)."\\page\\".$page."Page";
- if (class_exists($class) && is_subclass_of($class, '\\dns\\page\\AbstractPage')) {
- $className = $class;
- }
- break;
- }
- }
- }
+ $this->pattern = '~/?(?:(?P<controller>[A-Za-z0-9\-]+)(?:/(?P<id>\d+)(?:-(?P<title>[^/]+))?)?)?~x';
+ $this->getControllers($module);
+
+ if (DNS::getSession()->username !== null) {
+ DNS::getTPL()->assign(array("username" => DNS::getSession()->username));
}
else {
- $className = '\\dns'.(empty($module) ? '' : '\\'.$module).'\\page\\IndexPage';
+ DNS::getTPL()->assign(array("username" => ''));
}
- if (!User::isLoggedIn() && $className != '\dns\page\LoginPage' && $className != '\dns\page\ApiPage') {
- DNS::getTPL()->display('login.tpl');
- exit;
+ $className = "";
+ if (!empty($_SERVER['QUERY_STRING'])) {
+ $this->matches($_SERVER['QUERY_STRING']);
+ $this->registerRouteData();
+ }
+ else {
+ $className = '\\dns'.(empty($module) ? '' : '\\'.$module).'\\page\\IndexPage';
}
- if (DNS::getSession()->username !== null) {
- DNS::getTPL()->assign(array("username" => DNS::getSession()->username));
+ if (isset($this->routeData['controller']) && !empty($this->routeData['controller'])) {
+ $controller = strtolower($this->routeData['controller']);
+ if (isset($this->controllers[$controller]) && !empty($this->controllers[$controller])) {
+ $className = $this->controllers[$controller];
+ }
+ else {
+ @header('HTTP/1.0 404 Not Found');
+ DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
+ DNS::getTPL()->display('error.tpl');
+ exit;
+ }
}
- if (empty($className)) {
- @header('HTTP/1.0 404 Not Found');
- DNS::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
- DNS::getTPL()->display('error.tpl');
+ if (!User::isLoggedIn() && $className != '\dns\page\LoginPage' && $className != '\dns\page\ApiPage') {
+ DNS::getTPL()->display('login.tpl');
exit;
}
exit;
}
}
+
+ /**
+ * Registers route data within $_GET and $_REQUEST.
+ */
+ protected function registerRouteData() {
+ foreach ($this->routeData as $key => $value) {
+ $_GET[$key] = $value;
+ $_REQUEST[$key] = $value;
+ }
+ }
+
+ public function getControllers ($module) {
+
+ $this->controllers = ControllerCacheBuilder::getInstance()->getData(array('module' => $module));
+ /*
+ $pages = glob(DNS_DIR.'/lib/'.(empty($module) ? '' : $module.'/').'page/*Page.class.php');
+
+ foreach ($pages as $page) {
+ $page = str_replace('Page.class.php', '', basename($page));
+
+ $class = "\\dns".(empty($module) ? '' : "\\".$module)."\\page\\".$page."Page";
+ if (class_exists($class) && is_subclass_of($class, '\\dns\\page\\AbstractPage')) {
+ $this->controllers[strtolower($page)] = $class;
+ }
+ }
+ */
+ }
+
+ public function matches($requestURL) {
+ if (preg_match($this->pattern, $requestURL, $matches)) {
+ foreach ($matches as $key => $value) {
+ if (!is_numeric($key)) {
+ $this->routeData[$key] = $value;
+ }
+ }
+
+ return true;
+ }
+
+ return false;
+ }
}