Browse code

PSR-4 based app config + routing

RafaƂ Szklarczyk authored on 22/05/2019 19:45:49
Showing 4 changed files
... ...
@@ -3,6 +3,7 @@
3 3
 namespace elanpl\L3;
4 4
 
5 5
 class Application{
6
+    public $Config; // L3 Config object
6 7
     public $Request; // build from received HTTP request
7 8
     public $Response; // response to be send
8 9
     public $Router; // routing engine
... ...
@@ -16,13 +17,16 @@ class Application{
16 17
     //public $Module; //Active module
17 18
     //public $Action; //Active action
18 19
 
19
-    public function __construct(){
20
+    public function __construct($config){
21
+        //set L3 application config
22
+        $this->Config = $config;
20 23
         //create request object
21
-        $this->Request = new Request();
24
+        $this->Request = $this->Config->getRequest();
25
+        //create router object
26
+        $this->Router = new Router($this->Config->getRouting());
22 27
         //create response objcet
23 28
         $this->Response = new Response();
24
-        //create router object
25
-        $this->Router = new Router();
29
+
26 30
         //create serialization object
27 31
         $this->Serialization = new Serialization(); 
28 32
     }
29 33
new file mode 100644
... ...
@@ -0,0 +1,13 @@
1
+<?php
2
+
3
+namespace elanpl\L3;
4
+
5
+class Config{
6
+    public function getRequest(){
7
+        return new Request();
8
+    }
9
+
10
+    public function getRouting(){
11
+        return new app\config\routing();
12
+    }
13
+}
0 14
\ No newline at end of file
... ...
@@ -10,6 +10,7 @@ class Request{
10 10
     public $AcceptLanguage;
11 11
     public $UserAgent;
12 12
     public $Headers;
13
+    public $Body;
13 14
     public function __construct(){
14 15
         if(isset($_GET['path']))
15 16
             $this->Path = $_GET['path'];
... ...
@@ -31,6 +32,9 @@ class Request{
31 32
                 $this->Headers[$header] = $value;
32 33
             }
33 34
         }
35
+        if (php_sapi_name() != "cli") {
36
+            $this->Body = file_get_contents('php://input');
37
+        }
34 38
     }
35 39
 
36 40
     public function ParseAccept($accept){
... ...
@@ -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