build routes dynamically
authorStricted <info@stricted.net>
Sun, 17 Jul 2016 19:40:21 +0000 (21:40 +0200)
committerStricted <info@stricted.net>
Sun, 17 Jul 2016 19:40:21 +0000 (21:40 +0200)
api/templates/compiled/.gitignore [new file with mode: 0644]
api/templates/default/error.tpl [new file with mode: 0644]
lib/system/RequestHandler.class.php
lib/system/cache/builder/ControllerCacheBuilder.class.php

diff --git a/api/templates/compiled/.gitignore b/api/templates/compiled/.gitignore
new file mode 100644 (file)
index 0000000..66ca35b
--- /dev/null
@@ -0,0 +1 @@
+*.php
\ No newline at end of file
diff --git a/api/templates/default/error.tpl b/api/templates/default/error.tpl
new file mode 100644 (file)
index 0000000..e69de29
index 19aa4897709c45cd7b789838d20753b9d74d44ab..90b9553cdeac544a46c059c4577ad0bc36f05780 100644 (file)
@@ -6,6 +6,7 @@ use dns\system\route\Regex;
 use dns\system\route\Request;
 use dns\system\route\Segment;
 use Zend\Mvc\Router\SimpleRouteStack;
+use Zend\Mvc\Router\Http\RouteMatch;
 
 /**
  * @author      Jan Altensen (Stricted)
@@ -35,44 +36,26 @@ class RequestHandler extends SingletonFactory {
         * @param       string  $module
         **/
        public function setRoutes($module='') {
-               if ($module == 'api') {
-                       //api routes (excludes the default routes)
+               if ($module == "api") {
                        $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);
                }
-               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' => Segment::factory([ 'route' => 'RecordList/:id[-:title]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => 'dns\page\RecordListPage' ] ]),
-                               'RecordEdit' => Segment::factory([ 'route' => 'RecordEdit/:id', 'constraints' => [ 'id' => '[0-9]+' ], 'defaults' => [ 'controller' => 'dns\page\RecordEditPage' ] ]),
-                               'RecordAdd' => Segment::factory([ 'route' => 'RecordAdd/:id', 'constraints' => [ 'id' => '[0-9]+' ], 'defaults' => [ 'controller' => 'dns\page\RecordAddPage' ] ]),
-                               'SecList' => Segment::factory([ 'route' => 'SecList/:id[-:title]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => 'dns\page\SecListPage' ] ]),
-                       ];
-                       
-                       $this->router->setRoutes($routes);
+               
+               /* load the controllers from cache and build routes */
+               $controllers = ControllerCacheBuilder::getInstance()->getData(array('module' => $module));
+               $routes = [];
+               
+               foreach ($controllers as $name => $data) {
+                       $routes[$name] = Segment::factory([ 'route' => $name.'[/:id[-:title]]', 'constraints' => [ 'id' => '[0-9]+', 'title' => '[a-zA-Z0-9_.-/]+' ], 'defaults' => [ 'controller' => $data ] ]);
                }
+               
+               $this->router->setRoutes($routes);
        }
        
        /**
         * @see \Zend\Mvc\Router\SimpleRouteStack::addRoute()
         *
-        * @param  string  $name
-        * @param  mixed   $route
+     * @param  string  $name
+     * @param  mixed   $route
         */
        public function addRoute ($name, $route) {
                $this->router->addRoute($name, $route);
@@ -81,17 +64,17 @@ class RequestHandler extends SingletonFactory {
        /**
         * @see \Zend\Mvc\Router\SimpleRouteStack::addRoutes()
         *
-        * @param       array|Traversable       $routes
+     * @param  array|Traversable       $routes
         */
        public function addRoutes ($routes) {
                $this->router->addRoutes($routes);
        }
        
-       /**
-        * Get the added routes
-        *
-        * @return      Traversable list of all routes
-        */
+    /**
+     * Get the added routes
+     *
+     * @return Traversable list of all routes
+     */
        public function getRoutes() {
                return $this->router->getRoutes();
        }
@@ -102,10 +85,7 @@ class RequestHandler extends SingletonFactory {
        public function handle () {
                $match = $this->router->match(new Request());
                if ($match !== null) {
-                       foreach ($match->getParams() as $key => $value) {
-                               $_GET[$key] = $value;
-                               $_REQUEST[$key] = $value;
-                       }
+                       $this->registerRouteData($match);
                        
                        $className = $match->getParam("controller");
                        
@@ -153,4 +133,14 @@ class RequestHandler extends SingletonFactory {
                        exit;
                }
        }
+       
+       /**
+       * Registers route data within $_GET and $_REQUEST.
+       */
+       protected function registerRouteData(RouteMatch $route) {
+               foreach ($route->getParams() as $key => $value) {
+                       $_GET[$key] = $value;
+                       $_REQUEST[$key] = $value;
+               }
+       }
 }
index 4a3979ce42f60f2e042d4e687fb14f26ae93ac41..d0f703e2ec658ea86be2ac47dae2fc9ecdfeb7d9 100644 (file)
@@ -21,11 +21,14 @@ class ControllerCacheBuilder extends AbstractCacheBuilder {
                $pages = glob(DNS_DIR.'/lib/'.(empty($parameters['module']) ? '' : $parameters['module'].'/').'page/*Page.class.php');
                
                foreach ($pages as $page) {
-                       $page = str_replace('Page.class.php', '', basename($page));
+                       $pageName = str_replace('Page.class.php', '', basename($page));
 
-                       $class = "\\dns".(empty($parameters['module']) ? '' : "\\".$parameters['module'])."\\page\\".$page."Page";
-                       if (class_exists($class) && is_subclass_of($class, '\\dns\\page\\AbstractPage')) {
-                               $data[strtolower($page)] = $class;
+                       $className = "\\dns".(empty($parameters['module']) ? '' : "\\".$parameters['module'])."\\page\\".$pageName."Page";
+                       if (class_exists($className) && is_subclass_of($className, '\\dns\\page\\AbstractPage')) {
+                               if ($className == '\dns\page\LoginPage') {
+                                       $data[''] = $className;
+                               }
+                               $data[$pageName] = $className;
                        }
                }