improve zend route handling
authorStricted <info@stricted.net>
Sun, 17 Jul 2016 08:39:46 +0000 (10:39 +0200)
committerStricted <info@stricted.net>
Sun, 17 Jul 2016 08:39:46 +0000 (10:39 +0200)
lib/system/RequestHandler.class.php
lib/system/route/Segment.class.php [new file with mode: 0644]

index 5b957c72ba9175b4397da764bbf673538048b980..3e1d3d13a734366932772306489820d8b4690ac2 100644 (file)
@@ -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<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%' ]),
+                               '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 (file)
index 0000000..8948b65
--- /dev/null
@@ -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);
+       }
+}