Rafał Szklarczyk authored on 16/05/2024 13:46:13
Showing 1 changed files
... ...
@@ -113,7 +113,7 @@ class Router{
113 113
     public function match($request){
114 114
 
115 115
         if(is_null($request->path)){
116
-            $auri = array();
116
+            $auri = array(0 =>"");
117 117
         }
118 118
         else{
119 119
             $auri = explode('/', trim($request->path, "/ \t\n\r\0\x0B"));
... ...
@@ -126,7 +126,7 @@ class Router{
126 126
                 $method = $routeInfo->method;
127 127
                 if($method=='ANY' || strpos($request->method,$method)!==false){
128 128
                     if(is_null($route)){
129
-                        $aroute = array();
129
+                        $aroute = array(0=>"");
130 130
                     }
131 131
                     else{
132 132
                         $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
... ...
@@ -168,7 +168,7 @@ class Router{
168 168
                 $route_pattern = $routeInfo->path;
169 169
                 $method = $routeInfo->method;
170 170
                 if($method=='ANY' || strpos($request->method, $method)!==false){
171
-                    if(\preg_match($route_pattern, $request->path, $match, \PREG_OFFSET_CAPTURE)){
171
+                    if(!\is_null($request->path) && \preg_match($route_pattern, $request->path, $match, \PREG_OFFSET_CAPTURE)){
172 172
                         $this->parsedParameters = $this->cleanPregMatch($match);
173 173
                         $this->depth = $curi;
174 174
                         $this->routeInfo = $routeInfo;
Browse code

Fix on deprecated trim(null).

Rafał Szklarczyk authored on 25/01/2024 11:25:44
Showing 1 changed files
... ...
@@ -112,7 +112,12 @@ class Router{
112 112
 
113 113
     public function match($request){
114 114
 
115
-        $auri = explode('/', trim($request->path, "/ \t\n\r\0\x0B"));
115
+        if(is_null($request->path)){
116
+            $auri = array();
117
+        }
118
+        else{
119
+            $auri = explode('/', trim($request->path, "/ \t\n\r\0\x0B"));
120
+        }
116 121
         $curi = count($auri);
117 122
 			
118 123
         foreach ($this->routes as $routeInfo) {
... ...
@@ -120,7 +125,13 @@ class Router{
120 125
                 $route = $routeInfo->path;
121 126
                 $method = $routeInfo->method;
122 127
                 if($method=='ANY' || strpos($request->method,$method)!==false){
123
-                    $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
128
+                    if(is_null($route)){
129
+                        $aroute = array();
130
+                    }
131
+                    else{
132
+                        $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
133
+                    }
134
+                    
124 135
                     //print_r($aroute);
125 136
                     if($curi==count($aroute)){ //compare path element count
126 137
                         //optimistic assumption :)
... ...
@@ -177,6 +188,7 @@ class Router{
177 188
     }
178 189
 
179 190
     public function calculateRequestDepth($request){
191
+        if(is_null($request->path)) return 0;
180 192
         return count(explode('/',trim($request->path, "/ \t\n\r\0\x0B")));
181 193
     }
182 194
 
Browse code

Empty matches fix (works also for php < 7.2)

Rafał Szklarczyk authored on 15/01/2020 22:09:48
Showing 1 changed files
... ...
@@ -100,6 +100,16 @@ class Router{
100 100
         return $this;
101 101
     }
102 102
 
103
+    public function cleanPregMatch($match){
104
+        $cleanMatch = array();
105
+        foreach($match as $key => $value){
106
+            if($value[1]>-1){
107
+                $cleanMatch[$key] = $value[0];
108
+            }
109
+        }
110
+        return $cleanMatch;
111
+    }
112
+
103 113
     public function match($request){
104 114
 
105 115
         $auri = explode('/', trim($request->path, "/ \t\n\r\0\x0B"));
... ...
@@ -147,8 +157,8 @@ class Router{
147 157
                 $route_pattern = $routeInfo->path;
148 158
                 $method = $routeInfo->method;
149 159
                 if($method=='ANY' || strpos($request->method, $method)!==false){
150
-                    if(\preg_match($route_pattern, $request->path, $match)){
151
-                        $this->parsedParameters = $match;
160
+                    if(\preg_match($route_pattern, $request->path, $match, \PREG_OFFSET_CAPTURE)){
161
+                        $this->parsedParameters = $this->cleanPregMatch($match);
152 162
                         $this->depth = $curi;
153 163
                         $this->routeInfo = $routeInfo;
154 164
                         return $routeInfo->result;
Browse code

REGEX routes

Rafał Szklarczyk authored on 26/11/2019 16:16:53
Showing 1 changed files
... ...
@@ -25,6 +25,12 @@ class Router{
25 25
         return $this;
26 26
     }
27 27
 
28
+    public function add_preg($method, $path, $result, $name = ''){
29
+        $this->routes[] = new RouteInfo($method, $path, $result, $name, RouteInfo::REGEX);
30
+        if(isset($name)&&$name!='') $this->routeNameIndex[$name] = &$this->routes[count($this->routes)-1];
31
+        return $this;
32
+    }
33
+
28 34
     public function add404($result){
29 35
         return $this->add('404', '', $result, _L3_ROUTE_404_NAME);
30 36
     }
... ...
@@ -53,6 +59,32 @@ class Router{
53 59
         return $this->add('ANY', $path, $result, $name);
54 60
     }
55 61
 
62
+    // Regular expression based routes
63
+
64
+    public function get_preg($path, $result, $name = ''){
65
+        return $this->add_preg('GET', $path, $result, $name);
66
+    }
67
+
68
+    public function post_preg($path, $result, $name = ''){
69
+        return $this->add_preg('POST', $path, $result, $name);
70
+    }
71
+
72
+    public function put_preg($path, $result, $name = ''){
73
+        return $this->add_preg('PUT', $path, $result, $name);
74
+    }
75
+
76
+    public function patch_preg($path, $result, $name = ''){
77
+        return $this->add_preg('PATCH', $path, $result, $name);
78
+    }
79
+
80
+    public function delete_preg($path, $result, $name = ''){
81
+        return $this->add_preg('DELETE', $path, $result, $name);
82
+    }
83
+
84
+    public function any_preg($path, $result, $name = ''){
85
+        return $this->add_preg('ANY', $path, $result, $name);
86
+    }
87
+
56 88
     public function addBeforeAction($event_handler){
57 89
         $this->routes[count($this->routes)-1]->addBeforeAction($event_handler);
58 90
         return $this;
... ...
@@ -74,36 +106,49 @@ class Router{
74 106
         $curi = count($auri);
75 107
 			
76 108
         foreach ($this->routes as $routeInfo) {
77
-            
78
-            $route = $routeInfo->path;
79
-            $method = $routeInfo->method;
80
-            if($method=='ANY' || strpos($request->method,$method)!==false){
81
-                $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
82
-                //print_r($aroute);
83
-                if($curi==count($aroute)){ //compare path element count
84
-                    //optimistic assumption :)
85
-                    $matchResult = true;
86
-                    for($i = 0; $i<$curi; $i++){
87
-                        $pathPartName = trim($aroute[$i],'{}');
88
-                        if($aroute[$i]==$pathPartName){
89
-                            if($auri[$i]!=$pathPartName){
90
-                                //echo "diffrence found";
91
-                                $matchResult = false;
92
-                                break;
109
+            if($routeInfo->type == RouteInfo::L3){
110
+                $route = $routeInfo->path;
111
+                $method = $routeInfo->method;
112
+                if($method=='ANY' || strpos($request->method,$method)!==false){
113
+                    $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
114
+                    //print_r($aroute);
115
+                    if($curi==count($aroute)){ //compare path element count
116
+                        //optimistic assumption :)
117
+                        $matchResult = true;
118
+                        for($i = 0; $i<$curi; $i++){
119
+                            $pathPartName = trim($aroute[$i],'{}');
120
+                            if($aroute[$i]==$pathPartName){
121
+                                if($auri[$i]!=$pathPartName){
122
+                                    //echo "diffrence found";
123
+                                    $matchResult = false;
124
+                                    break;
125
+                                }
93 126
                             }
94
-                        }
95
-                        else{ // {...} found -> catch $uri variable
96
-                            $value = $auri[$i];
97
-                            $valueKey = explode(':', $pathPartName);
98
-                            //validation
99
-                            if(isset($valueKey[1]) && $valueKey[1]=='int'){
100
-                                $value = intval($value);
127
+                            else{ // {...} found -> catch $uri variable
128
+                                $value = $auri[$i];
129
+                                $valueKey = explode(':', $pathPartName);
130
+                                //validation
131
+                                if(isset($valueKey[1]) && $valueKey[1]=='int'){
132
+                                    $value = intval($value);
133
+                                }
134
+                                //value store...
135
+                                $this->parsedParameters[$valueKey[0]] = $value;
101 136
                             }
102
-                            //value store...
103
-                            $this->parsedParameters[$valueKey[0]] = $value;
137
+                        }
138
+                        if($matchResult){ // match found
139
+                            $this->depth = $curi;
140
+                            $this->routeInfo = $routeInfo;
141
+                            return $routeInfo->result;
104 142
                         }
105 143
                     }
106
-                    if($matchResult){ // match found
144
+                }
145
+            }
146
+            if($routeInfo->type == RouteInfo::REGEX){
147
+                $route_pattern = $routeInfo->path;
148
+                $method = $routeInfo->method;
149
+                if($method=='ANY' || strpos($request->method, $method)!==false){
150
+                    if(\preg_match($route_pattern, $request->path, $match)){
151
+                        $this->parsedParameters = $match;
107 152
                         $this->depth = $curi;
108 153
                         $this->routeInfo = $routeInfo;
109 154
                         return $routeInfo->result;
... ...
@@ -142,12 +187,15 @@ class Router{
142 187
 
143 188
     public function link($name, $parameters){
144 189
         $route = $this->routeNameIndex[$name];
190
+        if($route->type == RouteInfo::REGEX){
191
+            throw new \Exception("Method Router::link not applicable to REGEX routes.");
192
+        }
145 193
         $fields = array_keys($parameters);
146 194
         $values = array_values($parameters);
147 195
         array_walk($fields, function (&$item, $key){
148 196
             $item = "/\{".$item."\}/";
149 197
         });
150
-        return preg_replace($fields, $values, $route->Path);
198
+        return preg_replace($fields, $values, $route->path);
151 199
     }
152 200
 
153 201
     public function getParameter($name){
Browse code

404 handling bug fix

Rafał Szklarczyk authored on 11/06/2019 21:39:30
Showing 1 changed files
... ...
@@ -136,6 +136,10 @@ class Router{
136 136
         }
137 137
     }
138 138
 
139
+    public function is_Match404(){
140
+        return isset($this->routeInfo) && ($this->routeInfo->name === _L3_ROUTE_404_NAME);
141
+    }
142
+
139 143
     public function link($name, $parameters){
140 144
         $route = $this->routeNameIndex[$name];
141 145
         $fields = array_keys($parameters);
Browse code

The 404 (not found) support

Rafał Szklarczyk authored on 11/06/2019 20:16:39
Showing 1 changed files
... ...
@@ -2,6 +2,7 @@
2 2
 
3 3
 namespace elanpl\L3;
4 4
 
5
+define('_L3_ROUTE_404_NAME', '_L3_404');
5 6
 class Router{
6 7
 
7 8
     protected $routes; // The defined routes collection
... ...
@@ -24,6 +25,10 @@ class Router{
24 25
         return $this;
25 26
     }
26 27
 
28
+    public function add404($result){
29
+        return $this->add('404', '', $result, _L3_ROUTE_404_NAME);
30
+    }
31
+
27 32
     public function get($path, $result, $name = ''){
28 33
         return $this->add('GET', $path, $result, $name);
29 34
     }
... ...
@@ -109,6 +114,28 @@ class Router{
109 114
         return false;
110 115
     }
111 116
 
117
+    public function getRouteInfo($name){
118
+        if(isset($this->routeNameIndex[$name]))
119
+            return $this->routeNameIndex[$name];
120
+        else
121
+            return false;
122
+    }
123
+
124
+    public function calculateRequestDepth($request){
125
+        return count(explode('/',trim($request->path, "/ \t\n\r\0\x0B")));
126
+    }
127
+
128
+    public function setMatch404($request){
129
+        if($routeInfo404 = $this->getRouteInfo(_L3_ROUTE_404_NAME)){
130
+            $this->depth = $this->calculateRequestDepth($request);
131
+            $this->routeInfo = $routeInfo404;
132
+            return $routeInfo404->result;
133
+        }
134
+        else{
135
+            return false;
136
+        }
137
+    }
138
+
112 139
     public function link($name, $parameters){
113 140
         $route = $this->routeNameIndex[$name];
114 141
         $fields = array_keys($parameters);
Browse code

bug fix...

Rafał Szklarczyk authored on 23/05/2019 19:07:35
Showing 1 changed files
... ...
@@ -65,7 +65,7 @@ class Router{
65 65
 
66 66
     public function match($request){
67 67
 
68
-        $auri = explode('/', trim($request->Path, "/ \t\n\r\0\x0B"));
68
+        $auri = explode('/', trim($request->path, "/ \t\n\r\0\x0B"));
69 69
         $curi = count($auri);
70 70
 			
71 71
         foreach ($this->routes as $routeInfo) {
... ...
@@ -101,7 +101,7 @@ class Router{
101 101
                     if($matchResult){ // match found
102 102
                         $this->depth = $curi;
103 103
                         $this->routeInfo = $routeInfo;
104
-                        return $routeInfo->Result;
104
+                        return $routeInfo->result;
105 105
                     }
106 106
                 }
107 107
             }
Browse code

Naming convention + clean up

Rafał Szklarczyk authored on 23/05/2019 18:25:39
Showing 1 changed files
... ...
@@ -8,7 +8,7 @@ class Router{
8 8
     protected $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
9 9
     public $parsedParameters; // Parameters parsed from Request Path
10 10
     public $depth; // Number of nested nodes in Request Path
11
-    public $RouteInfo; // The RouteInfo object if the route was matched
11
+    public $routeInfo; // The RouteInfo object if the route was matched
12 12
 
13 13
     public function __construct($routing)
14 14
     {
... ...
@@ -18,61 +18,61 @@ class Router{
18 18
         $routing->set($this);
19 19
     }
20 20
 
21
-    public function add($Method, $Path, $Result, $Name = ''){
22
-        $this->routes[] = new RouteInfo($Method, $Path, $Result, $Name);
23
-        if(isset($Name)&&$Name!='') $this->routeNameIndex[$Name] = &$this->routes[count($this->routes)-1];
21
+    public function add($method, $path, $result, $name = ''){
22
+        $this->routes[] = new RouteInfo($method, $path, $result, $name);
23
+        if(isset($name)&&$name!='') $this->routeNameIndex[$name] = &$this->routes[count($this->routes)-1];
24 24
         return $this;
25 25
     }
26 26
 
27
-    public function get($Path, $Result, $Name = ''){
28
-        return $this->add('GET', $Path, $Result, $Name);
27
+    public function get($path, $result, $name = ''){
28
+        return $this->add('GET', $path, $result, $name);
29 29
     }
30 30
 
31
-    public function post($Path, $Result, $Name = ''){
32
-        return $this->add('POST', $Path, $Result, $Name);
31
+    public function post($path, $result, $name = ''){
32
+        return $this->add('POST', $path, $result, $name);
33 33
     }
34 34
 
35
-    public function put($Path, $Result, $Name = ''){
36
-        return $this->add('PUT', $Path, $Result, $Name);
35
+    public function put($path, $result, $name = ''){
36
+        return $this->add('PUT', $path, $result, $name);
37 37
     }
38 38
 
39
-    public function patch($Path, $Result, $Name = ''){
40
-        return $this->add('PATCH', $Path, $Result, $Name);
39
+    public function patch($path, $result, $name = ''){
40
+        return $this->add('PATCH', $path, $result, $name);
41 41
     }
42 42
 
43
-    public function delete($Path, $Result, $Name = ''){
44
-        return $this->add('DELETE', $Path, $Result, $Name);
43
+    public function delete($path, $result, $name = ''){
44
+        return $this->add('DELETE', $path, $result, $name);
45 45
     }
46 46
 
47
-    public function any($Path, $Result, $Name = ''){
48
-        return $this->add('ANY', $Path, $Result, $Name);
47
+    public function any($path, $result, $name = ''){
48
+        return $this->add('ANY', $path, $result, $name);
49 49
     }
50 50
 
51
-    public function AddBeforeAction($event_handler){
52
-        $this->routes[count($this->routes)-1]->AddBeforeAction($event_handler);
51
+    public function addBeforeAction($event_handler){
52
+        $this->routes[count($this->routes)-1]->addBeforeAction($event_handler);
53 53
         return $this;
54 54
     }
55 55
 
56
-    public function AddAfterAction($event_handler){
57
-        $this->routes[count($this->routes)-1]->AddAfterAction($event_handler);
56
+    public function addAfterAction($event_handler){
57
+        $this->routes[count($this->routes)-1]->addAfterAction($event_handler);
58 58
         return $this;
59 59
     }
60 60
 
61
-    public function AddAfterResult($event_handler){
62
-        $this->routes[count($this->routes)-1]->AddAfterAction($event_handler);
61
+    public function addAfterResult($event_handler){
62
+        $this->routes[count($this->routes)-1]->addAfterAction($event_handler);
63 63
         return $this;
64 64
     }
65 65
 
66
-    public function match($Request){
66
+    public function match($request){
67 67
 
68
-        $auri = explode('/', trim($Request->Path, "/ \t\n\r\0\x0B"));
68
+        $auri = explode('/', trim($request->Path, "/ \t\n\r\0\x0B"));
69 69
         $curi = count($auri);
70 70
 			
71 71
         foreach ($this->routes as $routeInfo) {
72 72
             
73
-            $route = $routeInfo->Path;
74
-            $method = $routeInfo->Method;
75
-            if($method=='ANY' || strpos($Request->Method,$method)!==false){
73
+            $route = $routeInfo->path;
74
+            $method = $routeInfo->method;
75
+            if($method=='ANY' || strpos($request->method,$method)!==false){
76 76
                 $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
77 77
                 //print_r($aroute);
78 78
                 if($curi==count($aroute)){ //compare path element count
... ...
@@ -100,7 +100,7 @@ class Router{
100 100
                     }
101 101
                     if($matchResult){ // match found
102 102
                         $this->depth = $curi;
103
-                        $this->RouteInfo = $routeInfo;
103
+                        $this->routeInfo = $routeInfo;
104 104
                         return $routeInfo->Result;
105 105
                     }
106 106
                 }
... ...
@@ -119,7 +119,7 @@ class Router{
119 119
         return preg_replace($fields, $values, $route->Path);
120 120
     }
121 121
 
122
-    public function GetParameter($name){
122
+    public function getParameter($name){
123 123
         return $this->parsedParameters[$name];
124 124
     }
125 125
 
Browse code

Router bug fix

Rafał Szklarczyk authored on 22/05/2019 20:27:03
Showing 1 changed files
... ...
@@ -5,10 +5,10 @@ namespace elanpl\L3;
5 5
 class Router{
6 6
 
7 7
     protected $routes; // The defined routes collection
8
-    protected $parsedParameters; // Parameters parsed from Request Path
9
-    protected $depth; // Number of nested nodes in Request Path
10 8
     protected $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
11
-    public $RouteInfo; // The RouteInfo objcet if the route was matched
9
+    public $parsedParameters; // Parameters parsed from Request Path
10
+    public $depth; // Number of nested nodes in Request Path
11
+    public $RouteInfo; // The RouteInfo object if the route was matched
12 12
 
13 13
     public function __construct($routing)
14 14
     {
Browse code

PSR-4 based app config + routing

Rafał Szklarczyk authored on 22/05/2019 19:45:49
Showing 1 changed files
... ...
@@ -4,71 +4,71 @@ namespace elanpl\L3;
4 4
 
5 5
 class Router{
6 6
 
7
-    //protected static $_instance; // object instance (for fluent api)
8
-    protected static $routes; // The defined routes collection
9
-    protected static $parsedParameters; // Parameters parsed from Request Path
10
-    protected static $depth; // Number of nested nodes in Request Path
11
-    protected static $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
12
-    public static $RouteInfo; // The RouteInfo objcet if the route was matched
13
-
14
-    public function __construct()
7
+    protected $routes; // The defined routes collection
8
+    protected $parsedParameters; // Parameters parsed from Request Path
9
+    protected $depth; // Number of nested nodes in Request Path
10
+    protected $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
11
+    public $RouteInfo; // The RouteInfo objcet if the route was matched
12
+
13
+    public function __construct($routing)
15 14
     {
16
-        //if(!isset(self::$_instance)) self::$_instance = new self;
17
-        if(!isset(self::$routes)) self::$routes = array(); 
18
-        if(!isset(self::$routeNameIndex)) self::$routeNameIndex= array();
15
+        $this->routes = array(); 
16
+        $this->routeNameIndex = array();
17
+        //Set the routing from configuration object
18
+        $routing->set($this);
19 19
     }
20 20
 
21
-    public static function add($Method, $Path, $Result, $Name = ''){
22
-        self::$routes[] = new RouteInfo($Method, $Path, $Result, $Name);
23
-        if(isset($Name)&&$Name!='') self::$routeNameIndex[$Name] = &self::$routes[count(self::$routes)-1];
24
-        return new self;
21
+    public function add($Method, $Path, $Result, $Name = ''){
22
+        $this->routes[] = new RouteInfo($Method, $Path, $Result, $Name);
23
+        if(isset($Name)&&$Name!='') $this->routeNameIndex[$Name] = &$this->routes[count($this->routes)-1];
24
+        return $this;
25 25
     }
26 26
 
27
-    public static function get($Path, $Result, $Name = ''){
28
-        return self::add('GET', $Path, $Result, $Name);
27
+    public function get($Path, $Result, $Name = ''){
28
+        return $this->add('GET', $Path, $Result, $Name);
29 29
     }
30 30
 
31
-    public static function post($Path, $Result, $Name = ''){
32
-        return self::add('POST', $Path, $Result, $Name);
31
+    public function post($Path, $Result, $Name = ''){
32
+        return $this->add('POST', $Path, $Result, $Name);
33 33
     }
34 34
 
35
-    public static function put($Path, $Result, $Name = ''){
36
-        return self::add('PUT', $Path, $Result, $Name);
35
+    public function put($Path, $Result, $Name = ''){
36
+        return $this->add('PUT', $Path, $Result, $Name);
37 37
     }
38 38
 
39
-    public static function patch($Path, $Result, $Name = ''){
40
-        return self::add('PATCH', $Path, $Result, $Name);
39
+    public function patch($Path, $Result, $Name = ''){
40
+        return $this->add('PATCH', $Path, $Result, $Name);
41 41
     }
42 42
 
43
-    public static function delete($Path, $Result, $Name = ''){
44
-        return self::add('DELETE', $Path, $Result, $Name);
43
+    public function delete($Path, $Result, $Name = ''){
44
+        return $this->add('DELETE', $Path, $Result, $Name);
45 45
     }
46 46
 
47
-    public static function any($Path, $Result, $Name = ''){
48
-        return self::add('ANY', $Path, $Result, $Name);
47
+    public function any($Path, $Result, $Name = ''){
48
+        return $this->add('ANY', $Path, $Result, $Name);
49 49
     }
50 50
 
51
-    public static function AddBeforeAction($event_handler){
52
-        self::$routes[count(self::$routes)-1]->AddBeforeAction($event_handler);
53
-        return new self;
51
+    public function AddBeforeAction($event_handler){
52
+        $this->routes[count($this->routes)-1]->AddBeforeAction($event_handler);
53
+        return $this;
54 54
     }
55 55
 
56
-    public static function AddAfterAction($event_handler){
57
-        self::$routes[count(self::$routes)-1]->AddAfterAction($event_handler);
58
-        return new self;
56
+    public function AddAfterAction($event_handler){
57
+        $this->routes[count($this->routes)-1]->AddAfterAction($event_handler);
58
+        return $this;
59 59
     }
60 60
 
61
-    public static function AddAfterResult($event_handler){
62
-        self::$routes[count(self::$routes)-1]->AddAfterAction($event_handler);
63
-        return new self;
61
+    public function AddAfterResult($event_handler){
62
+        $this->routes[count($this->routes)-1]->AddAfterAction($event_handler);
63
+        return $this;
64 64
     }
65 65
 
66
-    public static function match($Request){
66
+    public function match($Request){
67 67
 
68 68
         $auri = explode('/', trim($Request->Path, "/ \t\n\r\0\x0B"));
69 69
         $curi = count($auri);
70 70
 			
71
-        foreach (self::$routes as $routeInfo) {
71
+        foreach ($this->routes as $routeInfo) {
72 72
             
73 73
             $route = $routeInfo->Path;
74 74
             $method = $routeInfo->Method;
... ...
@@ -95,12 +95,12 @@ class Router{
95 95
                                 $value = intval($value);
96 96
                             }
97 97
                             //value store...
98
-                            self::$parsedParameters[$valueKey[0]] = $value;
98
+                            $this->parsedParameters[$valueKey[0]] = $value;
99 99
                         }
100 100
                     }
101 101
                     if($matchResult){ // match found
102
-                        self::$depth = $curi;
103
-                        self::$RouteInfo = $routeInfo;
102
+                        $this->depth = $curi;
103
+                        $this->RouteInfo = $routeInfo;
104 104
                         return $routeInfo->Result;
105 105
                     }
106 106
                 }
... ...
@@ -109,8 +109,8 @@ class Router{
109 109
         return false;
110 110
     }
111 111
 
112
-    public static function link($name, $parameters){
113
-        $route = self::$routeNameIndex[$name];
112
+    public function link($name, $parameters){
113
+        $route = $this->routeNameIndex[$name];
114 114
         $fields = array_keys($parameters);
115 115
         $values = array_values($parameters);
116 116
         array_walk($fields, function (&$item, $key){
... ...
@@ -119,30 +119,8 @@ class Router{
119 119
         return preg_replace($fields, $values, $route->Path);
120 120
     }
121 121
 
122
-    public function __get($name)
123
-    {
124
-        if($name=="routes") return self::$routes;
125
-        if($name=="parsedParameters") return self::$parsedParameters;
126
-        if($name=="depth") return self::$depth;
127
-        if($name=="RouteInfo") return self::$RouteInfo;
128
-    }
129
-
130
-    public function __call($name, $arguments)
131
-    {
132
-        if($name == 'GetParameter'){
133
-            return self::$parsedParameters[$arguments[0]];
134
-        }
135
-        // Note: value of $name is case sensitive.
136
-        $allowed_methods = ['add', 'post', 'any', 'get', 'match', 'AddBeforeAction', 'AddAfterAction', 'AddAfterResult'];
137
-
138
-        if(in_array($name, $allowed_methods)){
139
-            $the_method = new \ReflectionMethod($this, $name);
140
-            $the_method->invokeArgs(NULL,$arguments);
141
-        }
142
-        else{
143
-            throw new \Exception("Call undefined or inaccesible method ".get_class($this)."::$name");
144
-        }
145
-        
122
+    public function GetParameter($name){
123
+        return $this->parsedParameters[$name];
146 124
     }
147 125
 
148 126
 }
149 127
\ No newline at end of file
Browse code

More initial feed...

Rafał Szklarczyk authored on 16/05/2019 22:26:29
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,148 @@
1
+<?php
2
+
3
+namespace elanpl\L3;
4
+
5
+class Router{
6
+
7
+    //protected static $_instance; // object instance (for fluent api)
8
+    protected static $routes; // The defined routes collection
9
+    protected static $parsedParameters; // Parameters parsed from Request Path
10
+    protected static $depth; // Number of nested nodes in Request Path
11
+    protected static $routeNameIndex; // An array with elements that reference to the routes ordered by a route names
12
+    public static $RouteInfo; // The RouteInfo objcet if the route was matched
13
+
14
+    public function __construct()
15
+    {
16
+        //if(!isset(self::$_instance)) self::$_instance = new self;
17
+        if(!isset(self::$routes)) self::$routes = array(); 
18
+        if(!isset(self::$routeNameIndex)) self::$routeNameIndex= array();
19
+    }
20
+
21
+    public static function add($Method, $Path, $Result, $Name = ''){
22
+        self::$routes[] = new RouteInfo($Method, $Path, $Result, $Name);
23
+        if(isset($Name)&&$Name!='') self::$routeNameIndex[$Name] = &self::$routes[count(self::$routes)-1];
24
+        return new self;
25
+    }
26
+
27
+    public static function get($Path, $Result, $Name = ''){
28
+        return self::add('GET', $Path, $Result, $Name);
29
+    }
30
+
31
+    public static function post($Path, $Result, $Name = ''){
32
+        return self::add('POST', $Path, $Result, $Name);
33
+    }
34
+
35
+    public static function put($Path, $Result, $Name = ''){
36
+        return self::add('PUT', $Path, $Result, $Name);
37
+    }
38
+
39
+    public static function patch($Path, $Result, $Name = ''){
40
+        return self::add('PATCH', $Path, $Result, $Name);
41
+    }
42
+
43
+    public static function delete($Path, $Result, $Name = ''){
44
+        return self::add('DELETE', $Path, $Result, $Name);
45
+    }
46
+
47
+    public static function any($Path, $Result, $Name = ''){
48
+        return self::add('ANY', $Path, $Result, $Name);
49
+    }
50
+
51
+    public static function AddBeforeAction($event_handler){
52
+        self::$routes[count(self::$routes)-1]->AddBeforeAction($event_handler);
53
+        return new self;
54
+    }
55
+
56
+    public static function AddAfterAction($event_handler){
57
+        self::$routes[count(self::$routes)-1]->AddAfterAction($event_handler);
58
+        return new self;
59
+    }
60
+
61
+    public static function AddAfterResult($event_handler){
62
+        self::$routes[count(self::$routes)-1]->AddAfterAction($event_handler);
63
+        return new self;
64
+    }
65
+
66
+    public static function match($Request){
67
+
68
+        $auri = explode('/', trim($Request->Path, "/ \t\n\r\0\x0B"));
69
+        $curi = count($auri);
70
+			
71
+        foreach (self::$routes as $routeInfo) {
72
+            
73
+            $route = $routeInfo->Path;
74
+            $method = $routeInfo->Method;
75
+            if($method=='ANY' || strpos($Request->Method,$method)!==false){
76
+                $aroute = explode('/', trim($route, "/ \t\n\r\0\x0B"));
77
+                //print_r($aroute);
78
+                if($curi==count($aroute)){ //compare path element count
79
+                    //optimistic assumption :)
80
+                    $matchResult = true;
81
+                    for($i = 0; $i<$curi; $i++){
82
+                        $pathPartName = trim($aroute[$i],'{}');
83
+                        if($aroute[$i]==$pathPartName){
84
+                            if($auri[$i]!=$pathPartName){
85
+                                //echo "diffrence found";
86
+                                $matchResult = false;
87
+                                break;
88
+                            }
89
+                        }
90
+                        else{ // {...} found -> catch $uri variable
91
+                            $value = $auri[$i];
92
+                            $valueKey = explode(':', $pathPartName);
93
+                            //validation
94
+                            if(isset($valueKey[1]) && $valueKey[1]=='int'){
95
+                                $value = intval($value);
96
+                            }
97
+                            //value store...
98
+                            self::$parsedParameters[$valueKey[0]] = $value;
99
+                        }
100
+                    }
101
+                    if($matchResult){ // match found
102
+                        self::$depth = $curi;
103
+                        self::$RouteInfo = $routeInfo;
104
+                        return $routeInfo->Result;
105
+                    }
106
+                }
107
+            }
108
+        }
109
+        return false;
110
+    }
111
+
112
+    public static function link($name, $parameters){
113
+        $route = self::$routeNameIndex[$name];
114
+        $fields = array_keys($parameters);
115
+        $values = array_values($parameters);
116
+        array_walk($fields, function (&$item, $key){
117
+            $item = "/\{".$item."\}/";
118
+        });
119
+        return preg_replace($fields, $values, $route->Path);
120
+    }
121
+
122
+    public function __get($name)
123
+    {
124
+        if($name=="routes") return self::$routes;
125
+        if($name=="parsedParameters") return self::$parsedParameters;
126
+        if($name=="depth") return self::$depth;
127
+        if($name=="RouteInfo") return self::$RouteInfo;
128
+    }
129
+
130
+    public function __call($name, $arguments)
131
+    {
132
+        if($name == 'GetParameter'){
133
+            return self::$parsedParameters[$arguments[0]];
134
+        }
135
+        // Note: value of $name is case sensitive.
136
+        $allowed_methods = ['add', 'post', 'any', 'get', 'match', 'AddBeforeAction', 'AddAfterAction', 'AddAfterResult'];
137
+
138
+        if(in_array($name, $allowed_methods)){
139
+            $the_method = new \ReflectionMethod($this, $name);
140
+            $the_method->invokeArgs(NULL,$arguments);
141
+        }
142
+        else{
143
+            throw new \Exception("Call undefined or inaccesible method ".get_class($this)."::$name");
144
+        }
145
+        
146
+    }
147
+
148
+}
0 149
\ No newline at end of file