update zend route implementation
authorStricted <info@stricted.net>
Sun, 17 Jul 2016 08:22:51 +0000 (10:22 +0200)
committerStricted <info@stricted.net>
Sun, 17 Jul 2016 08:22:51 +0000 (10:22 +0200)
lib/system/DNS.class.php
lib/system/RequestHandler.class.php
lib/system/route/Literal.class.php
lib/system/route/Regex.class.php
lib/system/route/Request.class.php

index 0679754ec79922e86cb2ad00838a5a24fbbb02f7..47ba743c3af816b191499de184dc88fb999fa02e 100644 (file)
@@ -349,7 +349,7 @@ class DNS {
                self::getTPL()->addTemplateDir(DNS_DIR."/templates/".$tpl);
                self::getTPL()->setCompileDir(DNS_DIR.(empty(self::$module) ? '' : '/'.self::$module)."/templates/compiled/".$tpl);
                self::getTPL()->setPluginsDir(array(
-                       DNS_DIR."/lib/system/api/smarty/libs/plugins",
+                       DNS_DIR."/vendor/smarty/libs/plugins",
                        DNS_DIR."/lib/system/template/plugins"
                ));
                self::getTPL()->loadFilter('pre', 'hascontent');
index bedc94fd91f8a1945216d96414c9fbf2d58202bf..5b957c72ba9175b4397da764bbf673538048b980 100644 (file)
@@ -4,6 +4,7 @@ use dns\system\cache\builder\ControllerCacheBuilder;
 use dns\system\route\Literal;
 use dns\system\route\Regex;
 use dns\system\route\Request;
+use dns\system\route\Segment;
 use Zend\Mvc\Router\SimpleRouteStack;
 
 /**
@@ -13,9 +14,10 @@ use Zend\Mvc\Router\SimpleRouteStack;
  */
 class RequestHandler extends SingletonFactory {
        protected $router = null;
+       protected $apiModule = false;
        
        /**
-        * init RequestHandler
+        * init RequestHandler
         */
        protected function init () {
                $this->router = new SimpleRouteStack();
@@ -27,25 +29,76 @@ class RequestHandler extends SingletonFactory {
                }
        }
        
+       /**
+        * set the default rules
+        *
+        * @param       string  $module
+        **/
        public function setRoutes($module='') {
                if ($module == 'api') {
-                       //api routes
+                       //api routes (excludes the default routes)
+                       $this->apiModule = true;
+                       
+                       $routes = [
+                               '' => Literal::factory([ 'route' => '', 'defaults' => [ 'controller' => 'dns\api\page\IndexPage' ] ]),
+                               'Index' => Literal::factory([ 'route' => 'Index', 'defaults' => [ 'controller' => 'dns\api\page\IndexPage' ] ]),
+                               'Server' => Literal::factory([ 'route' => 'Server', 'defaults' => [ 'controller' => 'dns\api\page\ServerPage' ] ]),
+                       ];
+                       
+                       $this->router->setRoutes($routes);
                }
-               
-               // default routes
-               $this->router->addRoute('', Literal::factory([ 'route' => '', 'defaults' => [ 'controller' => 'dns\page\IndexPage' ] ]));
-               $this->router->addRoute('Index', Literal::factory([ 'route' => 'Index', 'defaults' => [ 'controller' => 'dns\page\IndexPage' ] ]));
-               $this->router->addRoute('Login', Literal::factory([ 'route' => 'Login', 'defaults' => [ 'controller' => 'dns\page\LoginPage' ] ]));
-               $this->router->addRoute('Logout', Literal::factory([ 'route' => 'Logout', 'defaults' => [ 'controller' => 'dns\page\LogoutPage' ] ]));
-               $this->router->addRoute('DomainList', Literal::factory([ 'route' => 'DomainList', 'defaults' => [ 'controller' => 'dns\page\DomainListPage' ] ]));
-               $this->router->addRoute('DomainAdd', Literal::factory([ 'route' => 'DomainAdd', 'defaults' => [ 'controller' => 'dns\page\DomainAddPage' ] ]));
-               $this->router->addRoute('DomainEdit', Regex::factory([ 'regex' => 'DomainEdit/(?P<id>\d+)(/)?', 'defaults' => [ 'controller' => 'dns\page\DomainEditPage' ], 'spec' => '/DomainEdit/%id%' ]));
-       }       
+               else {
+                       // default routes
+                       $routes = [
+                               '' => Literal::factory([ 'route' => '', 'defaults' => [ 'controller' => 'dns\page\IndexPage' ] ]),
+                               'Index' => Literal::factory([ 'route' => 'Index', 'defaults' => [ 'controller' => 'dns\page\IndexPage' ] ]),
+                               'Login' => Literal::factory([ 'route' => 'Login', 'defaults' => [ 'controller' => 'dns\page\LoginPage' ] ]),
+                               'ApiManagement' => Literal::factory([ 'route' => 'ApiManagement', 'defaults' => [ 'controller' => 'dns\page\ApiManagementPage' ] ]),
+                               'Logout' => Literal::factory([ 'route' => 'Logout', 'defaults' => [ 'controller' => 'dns\page\LogoutPage' ] ]),
+                               'Action' => Literal::factory([ 'route' => 'Action', 'defaults' => [ 'controller' => 'dns\page\ActionPage' ] ]),
+                               'DomainList' => Literal::factory([ 'route' => 'DomainList', 'defaults' => [ 'controller' => 'dns\page\DomainListPage' ] ]),
+                               'DomainAdd' => Literal::factory([ 'route' => 'DomainAdd', 'defaults' => [ 'controller' => 'dns\page\DomainAddPage' ] ]),
+                               'RecordList' => Regex::factory([ 'regex' => 'RecordList/(?P<id>\d+)(/)?(?:-(?P<title>[^/]+))?', 'defaults' => [ 'controller' => 'dns\page\RecordListPage' ], 'spec' => 'RecordList/%id%-%title%' ]),
+                               'RecordEdit' => Regex::factory([ 'regex' => 'RecordEdit/(?P<id>\d+)(/)?', 'defaults' => [ 'controller' => 'dns\page\RecordEditPage' ], 'spec' => 'RecordEdit/%id%' ]),
+                               'RecordAdd' => Regex::factory([ 'regex' => 'RecordAdd/(?P<id>\d+)(/)?', 'defaults' => [ 'controller' => 'dns\page\RecordAddPage' ], 'spec' => 'RecordAdd/%id%' ]),
+                               'SecList' => Regex::factory([ 'regex' => 'SecList/(?P<id>\d+)(/)?(?:-(?P<title>[^/]+))?', 'defaults' => [ 'controller' => 'dns\page\SecListPage' ], 'spec' => 'SecList/%id%-%title%' ]),
+                       ];
+                       
+                       $this->router->setRoutes($routes);
+               }
+       }
        
+       /**
+        * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
+        *
+     * @param  string  $name
+     * @param  mixed   $route
+        */
+       public function addRoute ($name, $route) {
+               $this->router->addRoute($name, $route);
+       }
+       
+       /**
+        * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
+        *
+     * @param  array|Traversable       $routes
+        */
+       public function addRoutes ($routes) {
+               $this->router->addRoutes($routes);
+       }
+       
+    /**
+     * Get the added routes
+     *
+     * @return Traversable list of all routes
+     */
        public function getRoutes() {
-               $this->router->getRoutes();
+               return $this->router->getRoutes();
        }
        
+       /**
+        * handle the request
+        */
        public function handle () {
                $match = $this->router->match(new Request());
                if ($match !== null) {
@@ -56,7 +109,7 @@ class RequestHandler extends SingletonFactory {
                        
                        $className = $match->getParam("controller");
                        
-                       if (!User::isLoggedIn() && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
+                       if (!User::isLoggedIn() && $this->apiModule == false && $className != 'dns\page\LoginPage' && $className != 'dns\page\ApiPage') {
                                DNS::getTPL()->display('login.tpl');
                                exit;
                        }
index cb39a89ed06f52f3ea276ff5668e6e40c769d047..c9798137fc988f9d11790f6c32eb55383fde0bd5 100644 (file)
@@ -6,13 +6,12 @@ use Zend\Stdlib\RequestInterface as Request;
 
 class Literal extends LiteralBase {
        public function match(Request $request, $pathOffset = null) {
-               if (!method_exists($request, 'getUri')) {
+               if (!method_exists($request, 'getPath')) {
                        return;
                }
-
-               $uri  = $request->getUri();
-               $path = $uri->getPath();
-
+               
+               $path = $request->getPath();
+               
                if ($pathOffset !== null) {
                        if ($pathOffset >= 0 && strlen($path) >= $pathOffset && !empty($this->route)) {
                                if (strpos($path, $this->route, $pathOffset) === $pathOffset) {
index c411423acb40b8e603ada157c15cd2fee7d1e74e..ba029d5dfe13d55157e07a68dc16750b3070ba37 100644 (file)
@@ -6,12 +6,11 @@ use Zend\Stdlib\RequestInterface as Request;
 
 class Regex extends RegexBase {
        public function match(Request $request, $pathOffset = null) {
-               if (!method_exists($request, 'getUri')) {
+               if (!method_exists($request, 'getPath')) {
                        return;
                }
-
-               $uri  = $request->getUri();
-               $path = $uri->getPath();
+               
+               $path = $request->getPath();
 
                if ($pathOffset !== null) {
                        $result = preg_match('(\G' . $this->regex . ')i', $path, $matches, null, $pathOffset);
index ac40533cb770b4e33d457de0bb136644ac7952c3..172100c3664c6a7589d11c48cd21047e1277ee2f 100644 (file)
@@ -3,10 +3,6 @@ namespace dns\system\route;
 use Zend\Stdlib\Request as BaseRequest;
 
 class Request extends BaseRequest {
-       public function getUri() {
-               return $this;
-       }
-       
        public function getPath() {
                $queryString = $_SERVER['QUERY_STRING'];
                
@@ -14,7 +10,7 @@ class Request extends BaseRequest {
                        $pos = strpos($queryString, '&');
                        $queryString = substr($queryString, 0, $pos);
                }
-               echo $queryString;
+               
                return $queryString;
        }
 }