3 use dns\system\cache\builder\ControllerCacheBuilder
;
4 use dns\system\route\Request
;
5 use dns\system\route\Segment
;
6 use Zend\Router\Http\RouteMatch
;
7 use Zend\Router\SimpleRouteStack
;
10 * @author Jan Altensen (Stricted)
11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
12 * @copyright 2013-2016 Jan Altensen (Stricted)
14 class RequestHandler
extends SingletonFactory
{
15 protected $router = null;
16 protected $apiModule = false;
21 protected function init () {
22 $this->router
= new SimpleRouteStack();
24 if (DNS
::getSession()->username
!== null) {
25 DNS
::getTPL()->assign(array("username" => DNS
::getSession()->username
));
28 DNS
::getTPL()->assign(array("username" => ''));
33 * set the default rules
35 * @param string $module
37 public function setRoutes($module='') {
38 if ($module == "api") {
39 $this->apiModule
= true;
42 /* load the controllers from cache and build routes */
43 $controllers = ControllerCacheBuilder
::getInstance()->getData(array('module' => $module));
46 foreach ($controllers as $name => $data) {
47 $routes[$name] = Segment
::factory([ 'route' => $name.'[/][/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]);
50 $this->router
->setRoutes($routes);
54 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
59 public function addRoute ($name, $route) {
60 $this->router
->addRoute($name, $route);
64 * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
66 * @param array|Traversable $routes
68 public function addRoutes ($routes) {
69 $this->router
->addRoutes($routes);
73 * Get the added routes
75 * @return Traversable list of all routes
77 public function getRoutes() {
78 return $this->router
->getRoutes();
84 public function handle () {
85 $match = $this->router
->match(new Request());
86 if ($match !== null) {
87 $this->registerRouteData($match);
89 $className = $match->getParam("controller");
91 if (!User
::isLoggedIn() && $this->apiModule
== false && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
92 DNS
::getTPL()->display('login.tpl');
96 if (defined('OFFLINE') && OFFLINE
) {
97 $admin = User
::isAdmin();
100 if (defined($className . '::AVAILABLE_DURING_OFFLINE_MODE') && constant($className . '::AVAILABLE_DURING_OFFLINE_MODE')) {
104 if (!$admin && !$available) {
105 @header
('HTTP/1.1 503 Service Unavailable');
106 DNS
::getTPL()->display('offline.tpl');
114 catch (\Exception
$e) {
115 if ($e->getCode() == 404) {
116 @header
('HTTP/1.0 404 Not Found');
118 else if ($e->getCode() == 403) {
119 @header
('HTTP/1.0 403 Forbidden');
123 DNS
::getTPL()->assign(array("activeMenuItem" => '', "error" => $e->getMessage()));
124 DNS
::getTPL()->display('error.tpl');
129 @header
('HTTP/1.0 404 Not Found');
130 DNS
::getTPL()->assign(array("activeMenuItem" => '', "error" => 'The link you are trying to reach is no longer available or invalid.'));
131 DNS
::getTPL()->display('error.tpl');
137 * Registers route data within $_GET and $_REQUEST.
139 protected function registerRouteData(RouteMatch
$route) {
140 foreach ($route->getParams() as $key => $value) {
141 $_GET[$key] = $value;
142 $_REQUEST[$key] = $value;