From: Stricted Date: Sun, 17 Jul 2016 08:39:46 +0000 (+0200) Subject: improve zend route handling X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=e85308b64b0e58cb4e2ddf87e83c054fa62cc0e6;p=GitHub%2FStricted%2FDomain-Control-Panel.git improve zend route handling --- diff --git a/lib/system/RequestHandler.class.php b/lib/system/RequestHandler.class.php index 5b957c7..3e1d3d1 100644 --- a/lib/system/RequestHandler.class.php +++ b/lib/system/RequestHandler.class.php @@ -58,10 +58,10 @@ class RequestHandler extends SingletonFactory { '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%' ]), + '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); diff --git a/lib/system/route/Segment.class.php b/lib/system/route/Segment.class.php new file mode 100644 index 0000000..8948b65 --- /dev/null +++ b/lib/system/route/Segment.class.php @@ -0,0 +1,53 @@ +<?php +namespace dns\system\route; +use Zend\Mvc\Router\Http\Segment as SegmentBase; +use Zend\Mvc\Router\Http\RouteMatch; +use Zend\Stdlib\RequestInterface as Request; + +class Segment extends SegmentBase { + public function match(Request $request, $pathOffset = null, array $options = []) { + if (!method_exists($request, 'getPath')) { + return; + } + + $path = $request->getPath(); + + $regex = $this->regex; + + if ($this->translationKeys) { + if (!isset($options['translator']) || !$options['translator'] instanceof Translator) { + throw new Exception\RuntimeException('No translator provided'); + } + + $translator = $options['translator']; + $textDomain = (isset($options['text_domain']) ? $options['text_domain'] : 'default'); + $locale = (isset($options['locale']) ? $options['locale'] : null); + + foreach ($this->translationKeys as $key) { + $regex = str_replace('#' . $key . '#', $translator->translate($key, $textDomain, $locale), $regex); + } + } + + if ($pathOffset !== null) { + $result = preg_match('(\G' . $regex . ')i', $path, $matches, null, $pathOffset); + } + else { + $result = preg_match('(^' . $regex . '$)i', $path, $matches); + } + + if (!$result) { + return; + } + + $matchedLength = strlen($matches[0]); + $params = []; + + foreach ($this->paramMap as $index => $name) { + if (isset($matches[$index]) && $matches[$index] !== '') { + $params[$name] = $this->decode($matches[$index]); + } + } + + return new RouteMatch(array_merge($this->defaults, $params), $matchedLength); + } +}