Browse code

basePath fix for multiple trailing slashes

Rafał Szklarczyk authored on 26/08/2024 11:48:29
Showing 1 changed files
... ...
@@ -48,8 +48,7 @@ class Application{
48 48
     public function basePath(){
49 49
         if(isset($this->request->path)&&$this->request->path!=''){
50 50
             $base = explode('%23',explode('?',$_SERVER['REQUEST_URI'],2)[0],2)[0];
51
-            if (substr($base,-strlen($this->request->path))===$this->request->path) $base = substr($base, 0, strlen($base)-strlen($this->request->path));
52
-            return $base;
51
+            return strstr($base, $this->request->path, true);
53 52
         }
54 53
         else
55 54
             return explode('%23',explode('?',$_SERVER['REQUEST_URI'],2)[0],2)[0];
Rafał Szklarczyk authored on 16/05/2024 13:46:13
Showing 1 changed files
... ...
@@ -181,8 +181,12 @@ class Application{
181 181
                 return $this->handle404($this->request);
182 182
             }
183 183
 
184
-            if(!is_null($module))
184
+            if(!is_null($module)){
185 185
                 $this->module = trim($module, '\\');
186
+            }
187
+            else{
188
+                $this->module = "";
189
+            }
186 190
             $this->controller = $controller;
187 191
             $this->action = $controller_action;
188 192
             // prepare args and start the action
Browse code

Fix on deprecated trim(null).

Rafał Szklarczyk authored on 25/01/2024 11:25:44
Showing 1 changed files
... ...
@@ -181,7 +181,8 @@ class Application{
181 181
                 return $this->handle404($this->request);
182 182
             }
183 183
 
184
-            $this->module = trim($module, '\\');
184
+            if(!is_null($module))
185
+                $this->module = trim($module, '\\');
185 186
             $this->controller = $controller;
186 187
             $this->action = $controller_action;
187 188
             // prepare args and start the action
Browse code

Better 404 info notice

Rafał Szklarczyk authored on 25/02/2020 23:07:52
Showing 1 changed files
... ...
@@ -264,7 +264,7 @@ class Application{
264 264
             if($this->router->is_Match404())
265 265
                 throw new \Exception("The 404 controller or action not found!");
266 266
             else
267
-                throw new \Exception("The 404 (not found) route not set!");
267
+                throw new \Exception("404 Not found. The requested resource not found or route not set!");
268 268
         }
269 269
     }
270 270
 
Browse code

The controller object as L3 property

Rafał Szklarczyk authored on 05/02/2020 12:17:53
Showing 1 changed files
... ...
@@ -13,9 +13,10 @@ class Application{
13 13
     public $controllersDirectory; // subdirectory/namespace part added to controller class 
14 14
     public $viewsDirectory; // subdirectory to search for the view files
15 15
     public $applicationDirectory; // subdirectory/namespace part of the application
16
-    public $controller; //Active controller
17
-    public $module; //Active module
18
-    public $action; //Active action
16
+    public $controller; //Active controller name
17
+    public $controllerObject; //Active controller object
18
+    public $module; //Active module name
19
+    public $action; //Active action name
19 20
 
20 21
     public function __construct($config){
21 22
         //set L3 application config
... ...
@@ -157,7 +158,7 @@ class Application{
157 158
                 .$controller;
158 159
             
159 160
             if(class_exists($controller_class,true)){
160
-                $controller_object = new $controller_class($this);
161
+                $this->controllerObject = $controller_object = new $controller_class($this);
161 162
             }
162 163
             else{
163 164
                 return $this->handle404($this->request);
Rafał Szklarczyk authored on 27/11/2019 22:25:50
Showing 1 changed files
... ...
@@ -247,7 +247,7 @@ class Application{
247 247
         $this->response->send();
248 248
     }
249 249
 
250
-    public function handle404($request){
250
+    public function handle404($request = null){
251 251
         if(!isset($request)){
252 252
             $request = $this->request;
253 253
         }
Browse code

handle404 return fix

Rafał Szklarczyk authored on 27/11/2019 22:20:47
Showing 1 changed files
... ...
@@ -248,8 +248,11 @@ class Application{
248 248
     }
249 249
 
250 250
     public function handle404($request){
251
+        if(!isset($request)){
252
+            $request = $this->request;
253
+        }
251 254
         if(!$this->router->is_Match404() && ($action = $this->router->setMatch404($request))){
252
-            $this->runAction($action);
255
+            return $this->runAction($action);
253 256
         }
254 257
         else{
255 258
             http_response_code(404);
... ...
@@ -340,21 +343,21 @@ class Application{
340 343
             $result = $action;
341 344
         }
342 345
 
343
-        // Handle the action result
344
-        if(isset($result)){
345
-            $this->handleActionResult($result);
346
-        }
346
+        return isset($result) ? $result : null;
347 347
     }
348 348
 
349 349
     public function run(){
350 350
         // Check if routing path is found
351 351
         if($action = $this->router->match($this->request)){
352
-            $this->runAction($action);
352
+            $result = $this->runAction($action);
353 353
         }
354 354
         // routing path not found -> generate 404 response
355 355
         else{
356
-            $this->handle404($this->request);
356
+            $result = $this->handle404($this->request);
357
+        }
358
+        // Handle the action result
359
+        if(isset($result)){
360
+            $this->handleActionResult($result);
357 361
         }
358
- 
359 362
     }
360 363
 }
361 364
\ No newline at end of file
Browse code

module name bug fix

Rafał Szklarczyk authored on 28/09/2019 21:36:50
Showing 1 changed files
... ...
@@ -180,7 +180,7 @@ class Application{
180 180
                 return $this->handle404($this->request);
181 181
             }
182 182
 
183
-            $this->module = $module;
183
+            $this->module = trim($module, '\\');
184 184
             $this->controller = $controller;
185 185
             $this->action = $controller_action;
186 186
             // prepare args and start the action
Browse code

Support of path string occurrence in basePath

Rafał Szklarczyk authored on 06/09/2019 08:41:53
Showing 1 changed files
... ...
@@ -46,7 +46,9 @@ class Application{
46 46
 
47 47
     public function basePath(){
48 48
         if(isset($this->request->path)&&$this->request->path!=''){
49
-            return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
49
+            $base = explode('%23',explode('?',$_SERVER['REQUEST_URI'],2)[0],2)[0];
50
+            if (substr($base,-strlen($this->request->path))===$this->request->path) $base = substr($base, 0, strlen($base)-strlen($this->request->path));
51
+            return $base;
50 52
         }
51 53
         else
52 54
             return explode('%23',explode('?',$_SERVER['REQUEST_URI'],2)[0],2)[0];
Browse code

uri started with /%23 support in basePath

Rafał Szklarczyk authored on 21/08/2019 14:11:57
Showing 1 changed files
... ...
@@ -49,7 +49,7 @@ class Application{
49 49
             return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
50 50
         }
51 51
         else
52
-            return explode('?',$_SERVER['REQUEST_URI'],2)[0];
52
+            return explode('%23',explode('?',$_SERVER['REQUEST_URI'],2)[0],2)[0];
53 53
     }
54 54
 
55 55
     public function getServerURL(){
Browse code

Bug fix :)

Rafał Szklarczyk authored on 21/08/2019 13:58:18
Showing 1 changed files
... ...
@@ -49,7 +49,7 @@ class Application{
49 49
             return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
50 50
         }
51 51
         else
52
-            return explode('?',$_SERVER['REQUEST_URI'],1)[0];
52
+            return explode('?',$_SERVER['REQUEST_URI'],2)[0];
53 53
     }
54 54
 
55 55
     public function getServerURL(){
Browse code

query string in request uri patch for basePath method

Rafał Szklarczyk authored on 21/08/2019 13:55:04
Showing 1 changed files
... ...
@@ -49,7 +49,7 @@ class Application{
49 49
             return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
50 50
         }
51 51
         else
52
-            return $_SERVER['REQUEST_URI'];
52
+            return explode('?',$_SERVER['REQUEST_URI'],1)[0];
53 53
     }
54 54
 
55 55
     public function getServerURL(){
Browse code

Bug fix :)

Rafał Szklarczyk authored on 21/08/2019 13:39:47
Showing 1 changed files
... ...
@@ -45,7 +45,7 @@ class Application{
45 45
     }
46 46
 
47 47
     public function basePath(){
48
-        if(isset($this->request->path)&&$this->request->path==''){
48
+        if(isset($this->request->path)&&$this->request->path!=''){
49 49
             return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
50 50
         }
51 51
         else
Browse code

Empty string in request path fix

Rafał Szklarczyk authored on 21/08/2019 13:37:50
Showing 1 changed files
... ...
@@ -45,8 +45,9 @@ class Application{
45 45
     }
46 46
 
47 47
     public function basePath(){
48
-        if(isset($this->request->path))
48
+        if(isset($this->request->path)&&$this->request->path==''){
49 49
             return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
50
+        }
50 51
         else
51 52
             return $_SERVER['REQUEST_URI'];
52 53
     }
Browse code

bug fix

Rafał Szklarczyk authored on 23/07/2019 07:00:41
Showing 1 changed files
... ...
@@ -275,7 +275,7 @@ class Application{
275 275
                         $field_pattern ='#^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$#';
276 276
                         foreach($args as $index => $param){
277 277
                             if(\preg_match($field_pattern, $param, $match) ){
278
-                                $args[$index] = $this->router->parsedParameters[$match['field']]."\\";
278
+                                $args[$index] = $this->router->parsedParameters[$match['field']];
279 279
                             }
280 280
                         }
281 281
                         $fire_args = array_merge($fire_args, $args);
Browse code

bug fix

Rafał Szklarczyk authored on 23/07/2019 06:38:11
Showing 1 changed files
... ...
@@ -274,7 +274,7 @@ class Application{
274 274
                         $this->router->routeInfo::eventHandlerArgumentsFormatCheck($eh['arguments'], $args);
275 275
                         $field_pattern ='#^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$#';
276 276
                         foreach($args as $index => $param){
277
-                            if(\preg_match($field_pattern, $controllerAction['module'], $match) ){
277
+                            if(\preg_match($field_pattern, $param, $match) ){
278 278
                                 $args[$index] = $this->router->parsedParameters[$match['field']]."\\";
279 279
                             }
280 280
                         }
Browse code

Parsed eventHandler arguments

Rafał Szklarczyk authored on 22/07/2019 18:10:28
Showing 1 changed files
... ...
@@ -272,6 +272,12 @@ class Application{
272 272
                     $fire_args[] = $this;
273 273
                     if(isset($eh['arguments'])){
274 274
                         $this->router->routeInfo::eventHandlerArgumentsFormatCheck($eh['arguments'], $args);
275
+                        $field_pattern ='#^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$#';
276
+                        foreach($args as $index => $param){
277
+                            if(\preg_match($field_pattern, $controllerAction['module'], $match) ){
278
+                                $args[$index] = $this->router->parsedParameters[$match['field']]."\\";
279
+                            }
280
+                        }
275 281
                         $fire_args = array_merge($fire_args, $args);
276 282
                     }
277 283
                     $result = $reflection->invokeArgs ($object , $fire_args );
Browse code

basePath - empty request->path fix

Rafał Szklarczyk authored on 22/07/2019 13:02:14
Showing 1 changed files
... ...
@@ -45,7 +45,10 @@ class Application{
45 45
     }
46 46
 
47 47
     public function basePath(){
48
-        return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
48
+        if(isset($this->request->path))
49
+            return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
50
+        else
51
+            return $_SERVER['REQUEST_URI'];
49 52
     }
50 53
 
51 54
     public function getServerURL(){
Browse code

Application object injection modifications

Rafał Szklarczyk authored on 04/07/2019 14:18:59
Showing 1 changed files
... ...
@@ -266,8 +266,7 @@ class Application{
266 266
                     $object = new $eh['class'];
267 267
                     $reflection = new \ReflectionMethod($object, $eh['function']);
268 268
                     $fire_args = array();
269
-                    $fire_args[] = $this->request;
270
-                    $fire_args[] = $this->router->routeInfo;
269
+                    $fire_args[] = $this;
271 270
                     if(isset($eh['arguments'])){
272 271
                         $this->router->routeInfo::eventHandlerArgumentsFormatCheck($eh['arguments'], $args);
273 272
                         $fire_args = array_merge($fire_args, $args);
Browse code

getServerURL, getURL

Rafał Szklarczyk authored on 04/07/2019 09:28:23
Showing 1 changed files
... ...
@@ -48,6 +48,14 @@ class Application{
48 48
         return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
49 49
     }
50 50
 
51
+    public function getServerURL(){
52
+        return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";
53
+    }
54
+
55
+    public function getURL(){
56
+        return $this->getServerURL().$_SERVER['REQUEST_URI'];
57
+    }
58
+
51 59
     public function relPath($file){
52 60
         return ($this->router->depth>0 ? str_repeat("../", substr($this->request->path,-1)=='/'?$this->router->depth:$this->router->depth-1) : "" ).$file; 
53 61
     }
Browse code

misspelling fix

Rafał Szklarczyk authored on 12/06/2019 12:46:49
Showing 1 changed files
... ...
@@ -74,7 +74,7 @@ class Application{
74 74
         
75 75
         for($i=0; $i<$search_level; $i++){
76 76
             if(preg_match(
77
-                    $pattern = '#('.$this->applicationDirectory.')(\\\\(?P<module>.+))?\\\\'.$this->controllersDirecotry.'\\\\(?P<controller>.+)#',
77
+                    $pattern = '#('.$this->applicationDirectory.')(\\\\(?P<module>.+))?\\\\'.$this->controllersDirectory.'\\\\(?P<controller>.+)#',
78 78
                     $call_stack[$i]['class'],
79 79
                     $match
80 80
                 )
... ...
@@ -139,7 +139,7 @@ class Application{
139 139
 
140 140
             $controller_class = "\\".$this->applicationDirectory."\\"
141 141
                 .$module
142
-                .$this->controllersDirecotry."\\"
142
+                .$this->controllersDirectory."\\"
143 143
                 .$controller;
144 144
             
145 145
             if(class_exists($controller_class,true)){
Browse code

misspeling fix

Rafał Szklarczyk authored on 12/06/2019 12:39:00
Showing 1 changed files
... ...
@@ -10,8 +10,8 @@ class Application{
10 10
     public $serialization; // serialization engine
11 11
     public $viewEngines; // view engines configuration
12 12
     public $baseDirectory; // directory root of the application
13
-    public $controllersDirecotry; // subdirectory/namespace part added to controller class 
14
-    public $viewsDirecotry; // subdirectory to search for the view files
13
+    public $controllersDirectory; // subdirectory/namespace part added to controller class 
14
+    public $viewsDirectory; // subdirectory to search for the view files
15 15
     public $applicationDirectory; // subdirectory/namespace part of the application
16 16
     public $controller; //Active controller
17 17
     public $module; //Active module
... ...
@@ -23,9 +23,9 @@ class Application{
23 23
         // directory root of the application
24 24
         $this->baseDirectory = $config->getBaseDirectory();
25 25
         // subdirectory/namespace part added to controller class
26
-        $this->controllersDirecotry = $config->getControllersDirectory();
26
+        $this->controllersDirectory = $config->getControllersDirectory();
27 27
         // subdirectory to search for the view files 
28
-        $this->viewsDirecotry = $config->getViewsDirectory();
28
+        $this->viewsDirectory = $config->getViewsDirectory();
29 29
         // subdirectory/namespace part of the application
30 30
         $this->applicationDirectory = $config->getApplicationDirectory();
31 31
         //create request object
Browse code

baseDirectory fix

Rafał Szklarczyk authored on 12/06/2019 10:40:33
Showing 1 changed files
... ...
@@ -9,7 +9,7 @@ class Application{
9 9
     public $router; // routing engine
10 10
     public $serialization; // serialization engine
11 11
     public $viewEngines; // view engines configuration
12
-    public $baseDirecotry; // directory root of the application
12
+    public $baseDirectory; // directory root of the application
13 13
     public $controllersDirecotry; // subdirectory/namespace part added to controller class 
14 14
     public $viewsDirecotry; // subdirectory to search for the view files
15 15
     public $applicationDirectory; // subdirectory/namespace part of the application
... ...
@@ -21,7 +21,7 @@ class Application{
21 21
         //set L3 application config
22 22
         $this->config = $config;
23 23
         // directory root of the application
24
-        $this->baseDirecotry = $config->getBaseDirectory();
24
+        $this->baseDirectory = $config->getBaseDirectory();
25 25
         // subdirectory/namespace part added to controller class
26 26
         $this->controllersDirecotry = $config->getControllersDirectory();
27 27
         // subdirectory to search for the view files 
Browse code

404 handling bug fix

Rafał Szklarczyk authored on 11/06/2019 21:39:30
Showing 1 changed files
... ...
@@ -234,15 +234,16 @@ class Application{
234 234
     }
235 235
 
236 236
     public function handle404($request){
237
-        if(!isset($this->router->routeInfo) && ($action = $this->router->setMatch404($request))){
237
+        if(!$this->router->is_Match404() && ($action = $this->router->setMatch404($request))){
238 238
             $this->runAction($action);
239 239
         }
240 240
         else{
241
-            echo "Resource not found!<br>".PHP_EOL;
241
+            http_response_code(404);
242
+            echo "<h1>Resource not found!</h1><br>".PHP_EOL;
242 243
             echo "<pre>";
243 244
             var_dump($request);
244 245
             echo "</pre>";
245
-            if(isset($this->router->routeInfo))
246
+            if($this->router->is_Match404())
246 247
                 throw new \Exception("The 404 controller or action not found!");
247 248
             else
248 249
                 throw new \Exception("The 404 (not found) route not set!");
Browse code

prevent 404 handler infiniete loop

Rafał Szklarczyk authored on 11/06/2019 21:04:20
Showing 1 changed files
... ...
@@ -234,7 +234,7 @@ class Application{
234 234
     }
235 235
 
236 236
     public function handle404($request){
237
-        if($action = $this->router->setMatch404($request)){
237
+        if(!isset($this->router->routeInfo) && ($action = $this->router->setMatch404($request))){
238 238
             $this->runAction($action);
239 239
         }
240 240
         else{
... ...
@@ -242,7 +242,10 @@ class Application{
242 242
             echo "<pre>";
243 243
             var_dump($request);
244 244
             echo "</pre>";
245
-            throw new \Exception("The 404 (not found) route not set!");
245
+            if(isset($this->router->routeInfo))
246
+                throw new \Exception("The 404 controller or action not found!");
247
+            else
248
+                throw new \Exception("The 404 (not found) route not set!");
246 249
         }
247 250
     }
248 251
 
Browse code

The 404 (not found) support

Rafał Szklarczyk authored on 11/06/2019 20:16:39
Showing 1 changed files
... ...
@@ -234,11 +234,16 @@ class Application{
234 234
     }
235 235
 
236 236
     public function handle404($request){
237
-        echo "Resource not found!<br>".PHP_EOL;
238
-        echo "<pre>";
239
-        var_dump($request);
240
-        echo "</pre>";
241
-        throw new \Exception("Not implemented!");
237
+        if($action = $this->router->setMatch404($request)){
238
+            $this->runAction($action);
239
+        }
240
+        else{
241
+            echo "Resource not found!<br>".PHP_EOL;
242
+            echo "<pre>";
243
+            var_dump($request);
244
+            echo "</pre>";
245
+            throw new \Exception("The 404 (not found) route not set!");
246
+        }
242 247
     }
243 248
 
244 249
     public function beforeAction(){
... ...
@@ -272,52 +277,56 @@ class Application{
272 277
         }
273 278
     }
274 279
 
275
-    public function run(){
276
-        // Check if routing path is found
277
-        if($action = $this->router->match($this->request)){
278
-            // routing match found decide what to do next...
279
-            // BeforeAction event
280
-            $this->beforeAction();
280
+    public function runAction($action){
281
+        // routing match found decide what to do next...
282
+        // BeforeAction event
283
+        $this->beforeAction();
281 284
 
282
-            // function -> call it...
283
-            if(is_callable($action)){
284
-                $reflection = new \ReflectionFunction($action);
285
-                if($reflection->getNumberOfParameters()){
286
-                    $fire_args=array();
287
-      
288
-                    foreach($reflection->getParameters() AS $arg)
289
-                    {
290
-                        if(isset($this->router->parsedParameters[$arg->name]))
291
-                            $fire_args[$arg->name]=$this->router->parsedParameters[$arg->name];
292
-                        else
293
-                            $fire_args[$arg->name]=null;
294
-                    }
295
-                    
296
-                    $result = call_user_func_array($action, $fire_args);
297
-                }
298
-                else{
299
-                    $result = call_user_func($action);
285
+        // function -> call it...
286
+        if(is_callable($action)){
287
+            $reflection = new \ReflectionFunction($action);
288
+            if($reflection->getNumberOfParameters()){
289
+                $fire_args=array();
290
+    
291
+                foreach($reflection->getParameters() AS $arg)
292
+                {
293
+                    if(isset($this->router->parsedParameters[$arg->name]))
294
+                        $fire_args[$arg->name]=$this->router->parsedParameters[$arg->name];
295
+                    else
296
+                        $fire_args[$arg->name]=null;
300 297
                 }
301
-            }
302
-            else if(is_object($action) && is_a($action, 'L2_controler_info')){
303
-                // not implemented yet.
304
-                throw new \Exception("Not implemented!");
305
-                ///$action->runControlerAction($this->routing->param);
306
-            }
307
-            // array with controller, and action -> run the controller action
308
-            else if(is_array($action) && array_key_exists('controller',$action) && array_key_exists('action', $action)){
309
-                $result = $this->runControllerAction($action, $this->router->parsedParameters);
310 298
                 
299
+                $result = call_user_func_array($action, $fire_args);
311 300
             }
312
-            // string
313
-            else if(is_string($action)){
314
-                $result = $action;
301
+            else{
302
+                $result = call_user_func($action);
315 303
             }
304
+        }
305
+        else if(is_object($action) && is_a($action, 'L2_controler_info')){
306
+            // not implemented yet.
307
+            throw new \Exception("Not implemented!");
308
+            ///$action->runControlerAction($this->routing->param);
309
+        }
310
+        // array with controller, and action -> run the controller action
311
+        else if(is_array($action) && array_key_exists('controller',$action) && array_key_exists('action', $action)){
312
+            $result = $this->runControllerAction($action, $this->router->parsedParameters);
313
+            
314
+        }
315
+        // string
316
+        else if(is_string($action)){
317
+            $result = $action;
318
+        }
316 319
 
317
-            // Handle the action result
318
-            if(isset($result)){
319
-                $this->handleActionResult($result);
320
-            }
320
+        // Handle the action result
321
+        if(isset($result)){
322
+            $this->handleActionResult($result);
323
+        }
324
+    }
325
+
326
+    public function run(){
327
+        // Check if routing path is found
328
+        if($action = $this->router->match($this->request)){
329
+            $this->runAction($action);
321 330
         }
322 331
         // routing path not found -> generate 404 response
323 332
         else{
Browse code

Serialization and ViewEngines basics

Rafał Szklarczyk authored on 06/06/2019 20:03:48
Showing 1 changed files
... ...
@@ -8,6 +8,7 @@ class Application{
8 8
     public $response; // response to be send
9 9
     public $router; // routing engine
10 10
     public $serialization; // serialization engine
11
+    public $viewEngines; // view engines configuration
11 12
     public $baseDirecotry; // directory root of the application
12 13
     public $controllersDirecotry; // subdirectory/namespace part added to controller class 
13 14
     public $viewsDirecotry; // subdirectory to search for the view files
... ...
@@ -36,6 +37,11 @@ class Application{
36 37
 
37 38
         //create serialization object
38 39
         $this->serialization = new Serialization(); 
40
+        //create view engines configuretion object
41
+        $this->viewEngines = new ViewEngine();
42
+
43
+        //perform configuration operations
44
+        $this->config->configure($this);
39 45
     }
40 46
 
41 47
     public function basePath(){
Browse code

Application obj injection into new controller obj

Rafał Szklarczyk authored on 04/06/2019 22:20:15
Showing 1 changed files
... ...
@@ -137,7 +137,7 @@ class Application{
137 137
                 .$controller;
138 138
             
139 139
             if(class_exists($controller_class,true)){
140
-                $controller_object = new $controller_class;
140
+                $controller_object = new $controller_class($this);
141 141
             }
142 142
             else{
143 143
                 return $this->handle404($this->request);
... ...
@@ -157,7 +157,7 @@ class Application{
157 157
             
158 158
             // method not found - 404
159 159
             if(!method_exists($controller_object,$controller_action)){
160
-                $this->handle404($this->request);
160
+                return $this->handle404($this->request);
161 161
             }
162 162
 
163 163
             $this->module = $module;
Browse code

Bug fix

Rafał Szklarczyk authored on 26/05/2019 21:09:40
Showing 1 changed files
... ...
@@ -112,7 +112,7 @@ class Application{
112 112
             //$this->IncludeControler($ControllerAction['controller']);
113 113
             if(isset($controllerAction['module'])){
114 114
                 if(\preg_match($field_pattern, $controllerAction['module'], $match) ){
115
-                    $module = $this->router->parsedParameters[$match['field']];
115
+                    $module = $this->router->parsedParameters[$match['field']]."\\";
116 116
                 }
117 117
                 else{
118 118
                     $module = $controllerAction['module']."\\";
Browse code

Small fixes

Rafał Szklarczyk authored on 23/05/2019 21:29:11
Showing 1 changed files
... ...
@@ -12,9 +12,9 @@ class Application{
12 12
     public $controllersDirecotry; // subdirectory/namespace part added to controller class 
13 13
     public $viewsDirecotry; // subdirectory to search for the view files
14 14
     public $applicationDirectory; // subdirectory/namespace part of the application
15
-    //public $Controller; //Active controller
16
-    //public $Module; //Active module
17
-    //public $Action; //Active action
15
+    public $controller; //Active controller
16
+    public $module; //Active module
17
+    public $action; //Active action
18 18
 
19 19
     public function __construct($config){
20 20
         //set L3 application config
... ...
@@ -160,8 +160,8 @@ class Application{
160 160
                 $this->handle404($this->request);
161 161
             }
162 162
 
163
-            $this->module = (isset($controllerAction['module'])?$controllerAction['module']:"");
164
-            $this->controller = $controllerAction['controller'];
163
+            $this->module = $module;
164
+            $this->controller = $controller;
165 165
             $this->action = $controller_action;
166 166
             // prepare args and start the action
167 167
             if(!isset($reflection)){
... ...
@@ -271,7 +271,7 @@ class Application{
271 271
         if($action = $this->router->match($this->request)){
272 272
             // routing match found decide what to do next...
273 273
             // BeforeAction event
274
-            $this->BeforeAction();
274
+            $this->beforeAction();
275 275
 
276 276
             // function -> call it...
277 277
             if(is_callable($action)){
Browse code

Finding controller from routing parameter bug fix

Rafał Szklarczyk authored on 23/05/2019 21:11:51
Showing 1 changed files
... ...
@@ -135,11 +135,12 @@ class Application{
135 135
                 .$module
136 136
                 .$this->controllersDirecotry."\\"
137 137
                 .$controller;
138
-            try{
138
+            
139
+            if(class_exists($controller_class,true)){
139 140
                 $controller_object = new $controller_class;
140 141
             }
141
-            catch(Exception $e){
142
-                var_dump($e);
142
+            else{
143
+                return $this->handle404($this->request);
143 144
             }
144 145
 
145 146
             if(\preg_match($field_pattern, $controllerAction['action'], $match) ){
Browse code

Controllers from routing params development

Rafał Szklarczyk authored on 23/05/2019 20:54:39
Showing 1 changed files
... ...
@@ -135,7 +135,12 @@ class Application{
135 135
                 .$module
136 136
                 .$this->controllersDirecotry."\\"
137 137
                 .$controller;
138
-            $controller_object = new $controller_class;
138
+            try{
139
+                $controller_object = new $controller_class;
140
+            }
141
+            catch(Exception $e){
142
+                var_dump($e);
143
+            }
139 144
 
140 145
             if(\preg_match($field_pattern, $controllerAction['action'], $match) ){
141 146
                 $controller_action = $this->router->parsedParameters[$match['field']];
Browse code

Field pattern fix

Rafał Szklarczyk authored on 23/05/2019 20:17:51
Showing 1 changed files
... ...
@@ -106,7 +106,7 @@ class Application{
106 106
             }
107 107
         }*/
108 108
 
109
-        $field_pattern ='^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$';
109
+        $field_pattern ='#^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$#';
110 110
 
111 111
         if(is_array($controllerAction)){
112 112
             //$this->IncludeControler($ControllerAction['controller']);
Browse code

Controller and Action defined as a routing field

Rafał Szklarczyk authored on 23/05/2019 20:12:01
Showing 1 changed files
... ...
@@ -106,18 +106,54 @@ class Application{
106 106
             }
107 107
         }*/
108 108
 
109
+        $field_pattern ='^{(?<field>[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)}$';
110
+
109 111
         if(is_array($controllerAction)){
110 112
             //$this->IncludeControler($ControllerAction['controller']);
113
+            if(isset($controllerAction['module'])){
114
+                if(\preg_match($field_pattern, $controllerAction['module'], $match) ){
115
+                    $module = $this->router->parsedParameters[$match['field']];
116
+                }
117
+                else{
118
+                    $module = $controllerAction['module']."\\";
119
+                }
120
+            }
121
+            else{
122
+                $module = "";
123
+            }
124
+
125
+            
126
+            if(\preg_match($field_pattern, $controllerAction['controller'], $match) ){
127
+                $controller = $this->router->parsedParameters[$match['field']];
128
+            }
129
+            else{
130
+                $controller = $controllerAction['controller'];
131
+            }
132
+            
133
+
111 134
             $controller_class = "\\".$this->applicationDirectory."\\"
112
-                .(isset($controllerAction['module'])?$controllerAction['module']."\\":"")
135
+                .$module
113 136
                 .$this->controllersDirecotry."\\"
114
-                .$controllerAction['controller'];
137
+                .$controller;
115 138
             $controller_object = new $controller_class;
116
-            $controller_action = $controllerAction['action'];
139
+
140
+            if(\preg_match($field_pattern, $controllerAction['action'], $match) ){
141
+                $controller_action = $this->router->parsedParameters[$match['field']];
142
+            }
143
+            else{
144
+                $controller_action = $controllerAction['action'];
145
+            }
146
+            
117 147
         }
118 148
 
119 149
         //fire the action
120 150
         if(isset($controller_object) && isset($controller_action)){
151
+            
152
+            // method not found - 404
153
+            if(!method_exists($controller_object,$controller_action)){
154
+                $this->handle404($this->request);
155
+            }
156
+
121 157
             $this->module = (isset($controllerAction['module'])?$controllerAction['module']:"");
122 158
             $this->controller = $controllerAction['controller'];
123 159
             $this->action = $controller_action;
... ...
@@ -185,6 +221,14 @@ class Application{
185 221
         $this->response->send();
186 222
     }
187 223
 
224
+    public function handle404($request){
225
+        echo "Resource not found!<br>".PHP_EOL;
226
+        echo "<pre>";
227
+        var_dump($request);
228
+        echo "</pre>";
229
+        throw new \Exception("Not implemented!");
230
+    }
231
+
188 232
     public function beforeAction(){
189 233
         if(!empty($this->router->routeInfo->beforeAction)){
190 234
             foreach($this->router->routeInfo->beforeAction as $event_handler){
... ...
@@ -265,8 +309,7 @@ class Application{
265 309
         }
266 310
         // routing path not found -> generate 404 response
267 311
         else{
268
-            echo "Routing not found!<br>".PHP_EOL;
269
-            throw new \Exception("Not implemented!");
312
+            $this->handle404($this->request);
270 313
         }
271 314
  
272 315
     }
Browse code

Naming convention + clean up

Rafał Szklarczyk authored on 23/05/2019 18:25:39
Showing 1 changed files
... ...
@@ -3,51 +3,51 @@
3 3
 namespace elanpl\L3;
4 4
 
5 5
 class Application{
6
-    public $Config; // L3 Config object
7
-    public $Request; // build from received HTTP request or created in L3 Config object
8
-    public $Response; // response to be send
9
-    public $Router; // routing engine
10
-    public $Serialization; // serialization engine
11
-    public $BaseDirectory; // directory root of the application
12
-    public $ControllersDirectory; // subdirectory/namespace part added to controller class 
13
-    public $ViewsDirectory; // subdirectory to search for the view files
14
-    public $ApplicationDirectory; // subdirectory/namespace part of the application
6
+    public $config; // L3 Config object
7
+    public $request; // build from received HTTP request or created in L3 Config object
8
+    public $response; // response to be send
9
+    public $router; // routing engine
10
+    public $serialization; // serialization engine
11
+    public $baseDirecotry; // directory root of the application
12
+    public $controllersDirecotry; // subdirectory/namespace part added to controller class 
13
+    public $viewsDirecotry; // subdirectory to search for the view files
14
+    public $applicationDirectory; // subdirectory/namespace part of the application
15 15
     //public $Controller; //Active controller
16 16
     //public $Module; //Active module
17 17
     //public $Action; //Active action
18 18
 
19 19
     public function __construct($config){
20 20
         //set L3 application config
21
-        $this->Config = $config;
21
+        $this->config = $config;
22 22
         // directory root of the application
23
-        $this->BaseDirectory = $config->getBaseDirectory();
23
+        $this->baseDirecotry = $config->getBaseDirectory();
24 24
         // subdirectory/namespace part added to controller class
25
-        $this->ControllersDirectory = $config->getControllersDirectory();
25
+        $this->controllersDirecotry = $config->getControllersDirectory();
26 26
         // subdirectory to search for the view files 
27
-        $this->ViewsDirectory = $config->getViewsDirectory();
27
+        $this->viewsDirecotry = $config->getViewsDirectory();
28 28
         // subdirectory/namespace part of the application
29
-        $this->ApplicationDirectory = $config->getApplicationDirectory();
29
+        $this->applicationDirectory = $config->getApplicationDirectory();
30 30
         //create request object
31
-        $this->Request = $this->Config->getRequest();
31
+        $this->request = $this->config->getRequest();
32 32
         //create router object
33
-        $this->Router = new Router($this->Config->getRouting());
33
+        $this->router = new Router($this->config->getRouting());
34 34
         //create response objcet
35
-        $this->Response = new Response();
35
+        $this->response = new Response();
36 36
 
37 37
         //create serialization object
38
-        $this->Serialization = new Serialization(); 
38
+        $this->serialization = new Serialization(); 
39 39
     }
40 40
 
41 41
     public function basePath(){
42
-        return strstr($_SERVER['REQUEST_URI'], $this->Request->Path, true);
42
+        return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
43 43
     }
44 44
 
45 45
     public function relPath($file){
46
-        return ($this->Router->depth>0 ? str_repeat("../", substr($this->Request->Path,-1)=='/'?$this->Router->depth:$this->Router->depth-1) : "" ).$file; 
46
+        return ($this->router->depth>0 ? str_repeat("../", substr($this->request->path,-1)=='/'?$this->router->depth:$this->router->depth-1) : "" ).$file; 
47 47
     }
48 48
 
49 49
     public function link($name, $parameters){
50
-        return $this->Router->link($name, $parameters);
50
+        return $this->router->link($name, $parameters);
51 51
     }
52 52
 
53 53
     /*
... ...
@@ -56,7 +56,7 @@ class Application{
56 56
         include_once(_L3_CONTROLLER_PATH.$Controller.'.php');
57 57
     }*/
58 58
 
59
-    public function FindControllerInCallStack($search_level=5){
59
+    public function findControllerInCallStack($search_level=5){
60 60
         //search for controller, action and module if present
61 61
         $controller = "";
62 62
         $module = "";
... ...
@@ -68,7 +68,7 @@ class Application{
68 68
         
69 69
         for($i=0; $i<$search_level; $i++){
70 70
             if(preg_match(
71
-                    $pattern = '#('.$this->ApplicationDirectory.')(\\\\(?P<module>.+))?\\\\'.$this->ControllersDirectory.'\\\\(?P<controller>.+)#',
71
+                    $pattern = '#('.$this->applicationDirectory.')(\\\\(?P<module>.+))?\\\\'.$this->controllersDirecotry.'\\\\(?P<controller>.+)#',
72 72
                     $call_stack[$i]['class'],
73 73
                     $match
74 74
                 )
... ...
@@ -87,7 +87,7 @@ class Application{
87 87
         );
88 88
     }
89 89
 
90
-    function RunControllerAction($ControllerAction, $Parameters){
90
+    function runControllerAction($controllerAction, $parameters){
91 91
         
92 92
         //prepare object and action name
93 93
         /*
... ...
@@ -106,21 +106,21 @@ class Application{
106 106
             }
107 107
         }*/
108 108
 
109
-        if(is_array($ControllerAction)){
109
+        if(is_array($controllerAction)){
110 110
             //$this->IncludeControler($ControllerAction['controller']);
111
-            $controller_class = "\\".$this->ApplicationDirectory."\\"
112
-                .(isset($ControllerAction['module'])?$ControllerAction['module']."\\":"")
113
-                .$this->ControllersDirectory."\\"
114
-                .$ControllerAction['controller'];
111
+            $controller_class = "\\".$this->applicationDirectory."\\"
112
+                .(isset($controllerAction['module'])?$controllerAction['module']."\\":"")
113
+                .$this->controllersDirecotry."\\"
114
+                .$controllerAction['controller'];
115 115
             $controller_object = new $controller_class;
116
-            $controller_action = $ControllerAction['action'];
116
+            $controller_action = $controllerAction['action'];
117 117
         }
118 118
 
119 119
         //fire the action
120 120
         if(isset($controller_object) && isset($controller_action)){
121
-            $this->Module = (isset($ControllerAction['module'])?$ControllerAction['module']:"");
122
-            $this->Controller = $ControllerAction['controller'];
123
-            $this->Action = $controller_action;
121
+            $this->module = (isset($controllerAction['module'])?$controllerAction['module']:"");
122
+            $this->controller = $controllerAction['controller'];
123
+            $this->action = $controller_action;
124 124
             // prepare args and start the action
125 125
             if(!isset($reflection)){
126 126
                 $reflection = new \ReflectionMethod($controller_object, $controller_action);
... ...
@@ -130,11 +130,11 @@ class Application{
130 130
 
131 131
                 foreach($reflection->getParameters() AS $arg)
132 132
                 {
133
-                    if(isset($Parameters[$arg->name]) || (is_array($ControllerAction) && isset($ControllerAction['defaults'][$arg->name])))
134
-                        if(isset($Parameters[$arg->name]))
135
-                            $fire_args[$arg->name]=$Parameters[$arg->name];
133
+                    if(isset($parameters[$arg->name]) || (is_array($controllerAction) && isset($controllerAction['defaults'][$arg->name])))
134
+                        if(isset($parameters[$arg->name]))
135
+                            $fire_args[$arg->name]=$parameters[$arg->name];
136 136
                         else
137
-                            $fire_args[$arg->name]=$ControllerAction['defaults'][$arg->name];
137
+                            $fire_args[$arg->name]=$controllerAction['defaults'][$arg->name];
138 138
                     else
139 139
                     {
140 140
                         if($arg->isDefaultValueAvailable()){
... ...
@@ -153,23 +153,23 @@ class Application{
153 153
         }
154 154
     }
155 155
 
156
-    public function HandleActionResult($result){
156
+    public function handleActionResult($result){
157 157
         if(is_string($result)){
158 158
             $content = $result;
159 159
         }
160 160
         else if(is_object($result)){
161 161
             if($result instanceof ViewModel){
162
-                if($SerializationContentType = $this->Serialization->Match($this->Request->AcceptTypes, $result)){
163
-                    $content = $this->Serialization->Serialize($SerializationContentType, $result);
162
+                if($serializationContentType = $this->serialization->match($this->request->acceptTypes, $result)){
163
+                    $content = $this->serialization->serialize($serializationContentType, $result);
164 164
                     if(is_object($content)){
165
-                        if($content instanceof Response){
166
-                            $this->Response = $content;
165
+                        if($content instanceof response){
166
+                            $this->response = $content;
167 167
                             unset($content);
168 168
                         }
169 169
                     }
170 170
                 }
171 171
                 else{
172
-                    echo "Serializer not defined for Content-Type: ".$this->Request->Accept."<br>".PHP_EOL;
172
+                    echo "Serializer not defined for Content-Type: ".$this->request->accept."<br>".PHP_EOL;
173 173
                     throw new \Exception("Not implemented!");
174 174
                 }
175 175
             }
... ...
@@ -177,26 +177,26 @@ class Application{
177 177
                 $content = $result->render();
178 178
             }
179 179
             else if($result instanceof Response){
180
-                $this->Response = $result;
180
+                $this->response = $result;
181 181
             }  
182 182
         }
183 183
         if(isset($content))
184
-                $this->Response->withBody($content);
185
-        $this->Response->send();
184
+                $this->response->withBody($content);
185
+        $this->response->send();
186 186
     }
187 187
 
188
-    public function BeforeAction(){
189
-        if(!empty($this->Router->RouteInfo->BeforeAction)){
190
-            foreach($this->Router->RouteInfo->BeforeAction as $event_handler){
191
-                $this->Router->RouteInfo::EventHandlerFormatCheck($event_handler, $eh);
188
+    public function beforeAction(){
189
+        if(!empty($this->router->routeInfo->beforeAction)){
190
+            foreach($this->router->routeInfo->beforeAction as $event_handler){
191
+                $this->router->routeInfo::eventHandlerFormatCheck($event_handler, $eh);
192 192
                 if(isset($eh['class'])){
193 193
                     $object = new $eh['class'];
194 194
                     $reflection = new \ReflectionMethod($object, $eh['function']);
195 195
                     $fire_args = array();
196
-                    $fire_args[] = $this->Request;
197
-                    $fire_args[] = $this->Router->RouteInfo;
196
+                    $fire_args[] = $this->request;
197
+                    $fire_args[] = $this->router->routeInfo;
198 198
                     if(isset($eh['arguments'])){
199
-                        $this->Router->RouteInfo::EventHandlerArgumentsFormatCheck($eh['arguments'], $args);
199
+                        $this->router->routeInfo::eventHandlerArgumentsFormatCheck($eh['arguments'], $args);
200 200
                         $fire_args = array_merge($fire_args, $args);
201 201
                     }
202 202
                     $result = $reflection->invokeArgs ($object , $fire_args );
... ...
@@ -207,7 +207,7 @@ class Application{
207 207
                 if(isset($result)){
208 208
                     if(is_object($result)){
209 209
                         if($result instanceof Response){
210
-                            $this->HandleActionResult($result);
210
+                            $this->handleActionResult($result);
211 211
                             exit();
212 212
                         }
213 213
                     }
... ...
@@ -216,9 +216,9 @@ class Application{
216 216
         }
217 217
     }
218 218
 
219
-    public function Run(){
219
+    public function run(){
220 220
         // Check if routing path is found
221
-        if($action = $this->Router->match($this->Request)){
221
+        if($action = $this->router->match($this->request)){
222 222
             // routing match found decide what to do next...
223 223
             // BeforeAction event
224 224
             $this->BeforeAction();
... ...
@@ -231,8 +231,8 @@ class Application{
231 231
       
232 232
                     foreach($reflection->getParameters() AS $arg)
233 233
                     {
234
-                        if(isset($this->Router->parsedParameters[$arg->name]))
235
-                            $fire_args[$arg->name]=$this->Router->parsedParameters[$arg->name];
234
+                        if(isset($this->router->parsedParameters[$arg->name]))
235
+                            $fire_args[$arg->name]=$this->router->parsedParameters[$arg->name];
236 236
                         else
237 237
                             $fire_args[$arg->name]=null;
238 238
                     }
... ...
@@ -250,7 +250,7 @@ class Application{
250 250
             }
251 251
             // array with controller, and action -> run the controller action
252 252
             else if(is_array($action) && array_key_exists('controller',$action) && array_key_exists('action', $action)){
253
-                $result = $this->RunControllerAction($action, $this->Router->parsedParameters);
253
+                $result = $this->runControllerAction($action, $this->router->parsedParameters);
254 254
                 
255 255
             }
256 256
             // string
... ...
@@ -260,7 +260,7 @@ class Application{
260 260
 
261 261
             // Handle the action result
262 262
             if(isset($result)){
263
-                $this->HandleActionResult($result);
263
+                $this->handleActionResult($result);
264 264
             }
265 265
         }
266 266
         // routing path not found -> generate 404 response
Browse code

Config Class development

Rafał Szklarczyk authored on 22/05/2019 20:57:16
Showing 1 changed files
... ...
@@ -4,12 +4,11 @@ namespace elanpl\L3;
4 4
 
5 5
 class Application{
6 6
     public $Config; // L3 Config object
7
-    public $Request; // build from received HTTP request
7
+    public $Request; // build from received HTTP request or created in L3 Config object
8 8
     public $Response; // response to be send
9 9
     public $Router; // routing engine
10 10
     public $Serialization; // serialization engine
11 11
     public $BaseDirectory; // directory root of the application
12
-    public $ConfigDirectory; // directory containing application configuration files
13 12
     public $ControllersDirectory; // subdirectory/namespace part added to controller class 
14 13
     public $ViewsDirectory; // subdirectory to search for the view files
15 14
     public $ApplicationDirectory; // subdirectory/namespace part of the application
... ...
@@ -20,6 +19,14 @@ class Application{
20 19
     public function __construct($config){
21 20
         //set L3 application config
22 21
         $this->Config = $config;
22
+        // directory root of the application
23
+        $this->BaseDirectory = $config->getBaseDirectory();
24
+        // subdirectory/namespace part added to controller class
25
+        $this->ControllersDirectory = $config->getControllersDirectory();
26
+        // subdirectory to search for the view files 
27
+        $this->ViewsDirectory = $config->getViewsDirectory();
28
+        // subdirectory/namespace part of the application
29
+        $this->ApplicationDirectory = $config->getApplicationDirectory();
23 30
         //create request object
24 31
         $this->Request = $this->Config->getRequest();
25 32
         //create router object
... ...
@@ -43,23 +50,6 @@ class Application{
43 50
         return $this->Router->link($name, $parameters);
44 51
     }
45 52
 
46
-    public function LoadApplicationConfiguration(){
47
-        //include routing configuration
48
-        include_once($this->ConfigDirectory."routing.php");
49
-        //include the application configuration
50
-        include_once($this->ConfigDirectory.'app.php');
51
-    }
52
-
53
-    public function LoadConfigFile($file){
54
-        //include configuration file
55
-        return include_once($this->ConfigDirectory.$file);
56
-    }
57
-
58
-    public function IncludeFromConfigDir($file){
59
-        //include configuration file
60
-        return include($this->ConfigDirectory.$file);
61
-    }
62
-
63 53
     /*
64 54
     //not need - PSR-4 autoload handles that
65 55
     function IncludeControler($Controller){
Browse code

PSR-4 based app config + routing

Rafał Szklarczyk authored on 22/05/2019 19:45:49
Showing 1 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
     }
Browse code

Application class - initial feed

Rafał Szklarczyk authored on 26/04/2019 20:52:24
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,279 @@
1
+<?php
2
+
3
+namespace elanpl\L3;
4
+
5
+class Application{
6
+    public $Request; // build from received HTTP request
7
+    public $Response; // response to be send
8
+    public $Router; // routing engine
9
+    public $Serialization; // serialization engine
10
+    public $BaseDirectory; // directory root of the application
11
+    public $ConfigDirectory; // directory containing application configuration files
12
+    public $ControllersDirectory; // subdirectory/namespace part added to controller class 
13
+    public $ViewsDirectory; // subdirectory to search for the view files
14
+    public $ApplicationDirectory; // subdirectory/namespace part of the application
15
+    //public $Controller; //Active controller
16
+    //public $Module; //Active module
17
+    //public $Action; //Active action
18
+
19
+    public function __construct(){
20
+        //create request object
21
+        $this->Request = new Request();
22
+        //create response objcet
23
+        $this->Response = new Response();
24
+        //create router object
25
+        $this->Router = new Router();
26
+        //create serialization object
27
+        $this->Serialization = new Serialization(); 
28
+    }
29
+
30
+    public function basePath(){
31
+        return strstr($_SERVER['REQUEST_URI'], $this->Request->Path, true);
32
+    }
33
+
34
+    public function relPath($file){
35
+        return ($this->Router->depth>0 ? str_repeat("../", substr($this->Request->Path,-1)=='/'?$this->Router->depth:$this->Router->depth-1) : "" ).$file; 
36
+    }
37
+
38
+    public function link($name, $parameters){
39
+        return $this->Router->link($name, $parameters);
40
+    }
41
+
42
+    public function LoadApplicationConfiguration(){
43
+        //include routing configuration
44
+        include_once($this->ConfigDirectory."routing.php");
45
+        //include the application configuration
46
+        include_once($this->ConfigDirectory.'app.php');
47
+    }
48
+
49
+    public function LoadConfigFile($file){
50
+        //include configuration file
51
+        return include_once($this->ConfigDirectory.$file);
52
+    }
53
+
54
+    public function IncludeFromConfigDir($file){
55
+        //include configuration file
56
+        return include($this->ConfigDirectory.$file);
57
+    }
58
+
59
+    /*
60
+    //not need - PSR-4 autoload handles that
61
+    function IncludeControler($Controller){
62
+        include_once(_L3_CONTROLLER_PATH.$Controller.'.php');
63
+    }*/
64
+
65
+    public function FindControllerInCallStack($search_level=5){
66
+        //search for controller, action and module if present
67
+        $controller = "";
68
+        $module = "";
69
+        $action = "";
70
+        $match = array();
71
+
72
+        //analize the call stack
73
+          $call_stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $search_level);
74
+        
75
+        for($i=0; $i<$search_level; $i++){
76
+            if(preg_match(
77
+                    $pattern = '#('.$this->ApplicationDirectory.')(\\\\(?P<module>.+))?\\\\'.$this->ControllersDirectory.'\\\\(?P<controller>.+)#',
78
+                    $call_stack[$i]['class'],
79
+                    $match
80
+                )
81
+            ){
82
+                $action = $call_stack[$i]['function'];
83
+                $module = $match['module'];
84
+                $controller = $match['controller'];
85
+                break;
86
+            }
87
+        }
88
+
89
+        return array(
90
+            'module' => $module,
91
+            'controller' => $controller,
92
+            'action' => $action
93
+        );
94
+    }
95
+
96
+    function RunControllerAction($ControllerAction, $Parameters){
97
+        
98
+        //prepare object and action name
99
+        /*
100
+        TODO ...
101
+        if(is_object($this->controler) && (is_a($this->controler,'L2_controler') || is_subclass_of($this->controler,'L2_controler'))){
102
+            if(is_string($this->action)){
103
+                $controler_object = $this->controler;
104
+                $controler_action = $this->action;
105
+            }
106
+        }
107
+        else if(is_string($this->controler)){
108
+            if(is_string($this->action)){
109
+                $this->includeControler($this->controler);
110
+                $controler_object = new $this->controler();
111
+                $controler_action = $this->action;
112
+            }
113
+        }*/
114
+
115
+        if(is_array($ControllerAction)){
116
+            //$this->IncludeControler($ControllerAction['controller']);
117
+            $controller_class = "\\".$this->ApplicationDirectory."\\"
118
+                .(isset($ControllerAction['module'])?$ControllerAction['module']."\\":"")
119
+                .$this->ControllersDirectory."\\"
120
+                .$ControllerAction['controller'];
121
+            $controller_object = new $controller_class;
122
+            $controller_action = $ControllerAction['action'];
123
+        }
124
+
125
+        //fire the action
126
+        if(isset($controller_object) && isset($controller_action)){
127
+            $this->Module = (isset($ControllerAction['module'])?$ControllerAction['module']:"");
128
+            $this->Controller = $ControllerAction['controller'];
129
+            $this->Action = $controller_action;
130
+            // prepare args and start the action
131
+            if(!isset($reflection)){
132
+                $reflection = new \ReflectionMethod($controller_object, $controller_action);
133
+            }
134
+            if($reflection->getNumberOfParameters()){
135
+                $fire_args=array();
136
+
137
+                foreach($reflection->getParameters() AS $arg)
138
+                {
139
+                    if(isset($Parameters[$arg->name]) || (is_array($ControllerAction) && isset($ControllerAction['defaults'][$arg->name])))
140
+                        if(isset($Parameters[$arg->name]))
141
+                            $fire_args[$arg->name]=$Parameters[$arg->name];
142
+                        else
143
+                            $fire_args[$arg->name]=$ControllerAction['defaults'][$arg->name];
144
+                    else
145
+                    {
146
+                        if($arg->isDefaultValueAvailable()){
147
+                            $fire_args[$arg->name]=$arg->getDefaultValue();
148
+                        }
149
+                        else{
150
+                            $fire_args[$arg->name]=null;
151
+                        }
152
+                    }
153
+                }
154
+                return $reflection->invokeArgs ($controller_object , $fire_args );
155
+            }
156
+            else{
157
+                return $reflection->invoke($controller_object);
158
+            }
159
+        }
160
+    }
161
+
162
+    public function HandleActionResult($result){
163
+        if(is_string($result)){
164
+            $content = $result;
165
+        }
166
+        else if(is_object($result)){
167
+            if($result instanceof ViewModel){
168
+                if($SerializationContentType = $this->Serialization->Match($this->Request->AcceptTypes, $result)){
169
+                    $content = $this->Serialization->Serialize($SerializationContentType, $result);
170
+                    if(is_object($content)){
171
+                        if($content instanceof Response){
172
+                            $this->Response = $content;
173
+                            unset($content);
174
+                        }
175
+                    }
176
+                }
177
+                else{
178
+                    echo "Serializer not defined for Content-Type: ".$this->Request->Accept."<br>".PHP_EOL;
179
+                    throw new \Exception("Not implemented!");
180
+                }
181
+            }
182
+            else if($result instanceof View){
183
+                $content = $result->render();
184
+            }
185
+            else if($result instanceof Response){
186
+                $this->Response = $result;
187
+            }  
188
+        }
189
+        if(isset($content))
190
+                $this->Response->withBody($content);
191
+        $this->Response->send();
192
+    }
193
+
194
+    public function BeforeAction(){
195
+        if(!empty($this->Router->RouteInfo->BeforeAction)){
196
+            foreach($this->Router->RouteInfo->BeforeAction as $event_handler){
197
+                $this->Router->RouteInfo::EventHandlerFormatCheck($event_handler, $eh);
198
+                if(isset($eh['class'])){
199
+                    $object = new $eh['class'];
200
+                    $reflection = new \ReflectionMethod($object, $eh['function']);
201
+                    $fire_args = array();
202
+                    $fire_args[] = $this->Request;
203
+                    $fire_args[] = $this->Router->RouteInfo;
204
+                    if(isset($eh['arguments'])){
205
+                        $this->Router->RouteInfo::EventHandlerArgumentsFormatCheck($eh['arguments'], $args);
206
+                        $fire_args = array_merge($fire_args, $args);
207
+                    }
208
+                    $result = $reflection->invokeArgs ($object , $fire_args );
209
+                }
210
+                else if (is_callable($eh['function'])){
211
+                    throw new \Exception("Not implemented!");
212
+                }
213
+                if(isset($result)){
214
+                    if(is_object($result)){
215
+                        if($result instanceof Response){
216
+                            $this->HandleActionResult($result);
217
+                            exit();
218
+                        }
219
+                    }
220
+                }
221
+            }
222
+        }
223
+    }
224
+
225
+    public function Run(){
226
+        // Check if routing path is found
227
+        if($action = $this->Router->match($this->Request)){
228
+            // routing match found decide what to do next...
229
+            // BeforeAction event
230
+            $this->BeforeAction();
231
+
232
+            // function -> call it...
233
+            if(is_callable($action)){
234
+                $reflection = new \ReflectionFunction($action);
235
+                if($reflection->getNumberOfParameters()){
236
+                    $fire_args=array();
237
+      
238
+                    foreach($reflection->getParameters() AS $arg)
239
+                    {
240
+                        if(isset($this->Router->parsedParameters[$arg->name]))
241
+                            $fire_args[$arg->name]=$this->Router->parsedParameters[$arg->name];
242
+                        else
243
+                            $fire_args[$arg->name]=null;
244
+                    }
245
+                    
246
+                    $result = call_user_func_array($action, $fire_args);
247
+                }
248
+                else{
249
+                    $result = call_user_func($action);
250
+                }
251
+            }
252
+            else if(is_object($action) && is_a($action, 'L2_controler_info')){
253
+                // not implemented yet.
254
+                throw new \Exception("Not implemented!");
255
+                ///$action->runControlerAction($this->routing->param);
256
+            }
257
+            // array with controller, and action -> run the controller action
258
+            else if(is_array($action) && array_key_exists('controller',$action) && array_key_exists('action', $action)){
259
+                $result = $this->RunControllerAction($action, $this->Router->parsedParameters);
260
+                
261
+            }
262
+            // string
263
+            else if(is_string($action)){
264
+                $result = $action;
265
+            }
266
+
267
+            // Handle the action result
268
+            if(isset($result)){
269
+                $this->HandleActionResult($result);
270
+            }
271
+        }
272
+        // routing path not found -> generate 404 response
273
+        else{
274
+            echo "Routing not found!<br>".PHP_EOL;
275
+            throw new \Exception("Not implemented!");
276
+        }
277
+ 
278
+    }
279
+}
0 280
\ No newline at end of file