ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RegexBasedAbstract.php
Go to the documentation of this file.
1<?php
2
4
6
7abstract class RegexBasedAbstract implements Dispatcher
8{
10 protected $staticRouteMap = [];
11
13 protected $variableRouteData = [];
14
18 abstract protected function dispatchVariableRoute($routeData, $uri);
19
20 public function dispatch($httpMethod, $uri)
21 {
22 if (isset($this->staticRouteMap[$httpMethod][$uri])) {
23 $handler = $this->staticRouteMap[$httpMethod][$uri];
24 return [self::FOUND, $handler, []];
25 }
26
27 $varRouteData = $this->variableRouteData;
28 if (isset($varRouteData[$httpMethod])) {
29 $result = $this->dispatchVariableRoute($varRouteData[$httpMethod], $uri);
30 if ($result[0] === self::FOUND) {
31 return $result;
32 }
33 }
34
35 // For HEAD requests, attempt fallback to GET
36 if ($httpMethod === 'HEAD') {
37 if (isset($this->staticRouteMap['GET'][$uri])) {
38 $handler = $this->staticRouteMap['GET'][$uri];
39 return [self::FOUND, $handler, []];
40 }
41 if (isset($varRouteData['GET'])) {
42 $result = $this->dispatchVariableRoute($varRouteData['GET'], $uri);
43 if ($result[0] === self::FOUND) {
44 return $result;
45 }
46 }
47 }
48
49 // If nothing else matches, try fallback routes
50 if (isset($this->staticRouteMap['*'][$uri])) {
51 $handler = $this->staticRouteMap['*'][$uri];
52 return [self::FOUND, $handler, []];
53 }
54 if (isset($varRouteData['*'])) {
55 $result = $this->dispatchVariableRoute($varRouteData['*'], $uri);
56 if ($result[0] === self::FOUND) {
57 return $result;
58 }
59 }
60
61 // Find allowed methods for this URI by matching against all other HTTP methods as well
62 $allowedMethods = [];
63
64 foreach ($this->staticRouteMap as $method => $uriMap) {
65 if ($method !== $httpMethod && isset($uriMap[$uri])) {
66 $allowedMethods[] = $method;
67 }
68 }
69
70 foreach ($varRouteData as $method => $routeData) {
71 if ($method === $httpMethod) {
72 continue;
73 }
74
75 $result = $this->dispatchVariableRoute($routeData, $uri);
76 if ($result[0] === self::FOUND) {
77 $allowedMethods[] = $method;
78 }
79 }
80
81 // If there are no allowed methods the route simply does not exist
82 if ($allowedMethods) {
83 return [self::METHOD_NOT_ALLOWED, $allowedMethods];
84 }
85
86 return [self::NOT_FOUND];
87 }
88}
$result
An exception for terminatinating execution or to throw for unit testing.
dispatch($httpMethod, $uri)
Dispatches against the provided HTTP method verb and URI.
$handler