From: Stricted Date: Sun, 17 Jul 2016 08:22:51 +0000 (+0200) Subject: update zend route implementation X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f0f86c22d409440c64a765dff8e33915aa475500;p=GitHub%2FStricted%2FDomain-Control-Panel.git update zend route implementation --- diff --git a/lib/system/DNS.class.php b/lib/system/DNS.class.php index 0679754..47ba743 100644 --- a/lib/system/DNS.class.php +++ b/lib/system/DNS.class.php @@ -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'); diff --git a/lib/system/RequestHandler.class.php b/lib/system/RequestHandler.class.php index bedc94f..5b957c7 100644 --- a/lib/system/RequestHandler.class.php +++ b/lib/system/RequestHandler.class.php @@ -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\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\d+)(/)?(?:-(?P[^/]+))?', '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; } diff --git a/lib/system/route/Literal.class.php b/lib/system/route/Literal.class.php index cb39a89..c979813 100644 --- a/lib/system/route/Literal.class.php +++ b/lib/system/route/Literal.class.php @@ -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) { diff --git a/lib/system/route/Regex.class.php b/lib/system/route/Regex.class.php index c411423..ba029d5 100644 --- a/lib/system/route/Regex.class.php +++ b/lib/system/route/Regex.class.php @@ -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); diff --git a/lib/system/route/Request.class.php b/lib/system/route/Request.class.php index ac40533..172100c 100644 --- a/lib/system/route/Request.class.php +++ b/lib/system/route/Request.class.php @@ -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; } }