]> git.andy128k.dev Git - ipf.git/commitdiff
controllers and actions
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Dec 2014 21:21:49 +0000 (23:21 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 27 Dec 2014 21:21:49 +0000 (23:21 +0200)
ipf/controller/base.php [new file with mode: 0644]
ipf/controller/function.php [new file with mode: 0644]
ipf/middleware/dispatch.php
ipf/router.php

diff --git a/ipf/controller/base.php b/ipf/controller/base.php
new file mode 100644 (file)
index 0000000..669d6d9
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+class IPF_Controller
+{
+    protected $request, $params;
+
+    function process($action, $request, $matches)
+    {
+        $this->request = $request;
+        $this->params = $matches;
+        return $this->$action();
+    }
+}
+
diff --git a/ipf/controller/function.php b/ipf/controller/function.php
new file mode 100644 (file)
index 0000000..0344c28
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+class IPF_Controller_Function extends IPF_Controller
+{
+    function process($action, $request, $matches)
+    {
+        return IPF::callFunction($action, array($request, $matches));
+    }
+}
+
index b63bd2ccca36d5f48b753d25d71e6d622a484351..2dbd45610165041721cfc132b868f5d176d08802 100644 (file)
@@ -15,7 +15,9 @@ class IPF_Dispatch_Middleware extends IPF_Middleware
         $request->params = $match;
 
         try {
-            return IPF::callFunction($route->func, array($request, $match));
+            $controller = $route->controller();
+            $controller = new $controller;
+            return $controller->process($route->action(), $request, $match);
         } catch (IPF_Router_Shortcut $e) {
             return $e->response($request);
         }
index 99be41ba3a254919683cfbe5e8d290e61ebd1082..5e1347c6a6bcad69aead1e60933fee4ce1564ec2 100644 (file)
@@ -9,15 +9,19 @@ class IPF_Router
         $this->flattenRoutes(IPF::get('urls'));
     }
 
-    private function flattenRoutes($routes, $prefix='')
+    private function flattenRoutes($routes, $prefix='', $common=array())
     {
         foreach ($routes as $route) {
-            if (isset($route['prefix'])) {
-                $this->flattenRoutes($route['urls'], $prefix . $route['prefix']);
+            $route = array_merge($common, $route);
+
+            if (isset($route['urls'])) {
+                $nested = \PFF\Arr::pop($route, 'urls');
+                $prefix .= \PFF\Arr::pop($route, 'prefix', '');
+                $this->flattenRoutes($nested, $prefix, $route);
             } elseif (isset($route['regex'])) {
-                $this->routes[] = new IPF_Route($prefix . $route['regex'], 'IPF_Router_RegexMatch', $route['func']);
+                $this->routes[] = new IPF_Route($prefix . $route['regex'], 'IPF_Router_RegexMatch', $route);
             } elseif (isset($route['expr'])) {
-                $this->routes[] = new IPF_Route($prefix . $route['expr'], 'RouteExpression', $route['func']);
+                $this->routes[] = new IPF_Route($prefix . $route['expr'], 'RouteExpression', $route);
             } else {
                 throw new IPF_Exception('Unsupported route type');
             }
@@ -50,9 +54,9 @@ class IPF_Router
     public function reverse($view, $params=array())
     { 
         foreach ($this->routes as $route)
-            if ($route->func == $view)
+            if ($route->routesTo($view))
                 return IPF::get('app_base') . $route->matcher()->reverse($params);
-        throw new IPF_Exception('Error, the view: '.$view.' has not been found.');
+        throw new IPF_Exception('Error, the view: '.print_r($view, true).' has not been found.');
     }
 }
 
@@ -116,13 +120,13 @@ class IPF_Router_RegexMatch
 class IPF_Route
 {
     private $matcher;
-    public $expr, $func;
+    public $expr, $params;
 
-    public function __construct($expr, $matcher, $func)
+    public function __construct($expr, $matcher, $params)
     {
         $this->expr = $expr;
         $this->matcher = $matcher;
-        $this->func = $func;
+        $this->params = $params;
     }
 
     public function matcher()
@@ -133,6 +137,27 @@ class IPF_Route
         }
         return $this->matcher;
     }
+
+    public function controller()
+    {
+        return \PFF\Arr::get($this->params, 'controller', 'IPF_Controller_Function');
+    }
+
+    public function action()
+    {
+        return \PFF\Arr::get($this->params, 'func', 'index');
+    }
+
+    public function routesTo($view)
+    {
+        if (is_array($view)) {
+            list($controller, $action) = $view;
+        } else {
+            $controller = 'IPF_Controller_Function';
+            $action = $view;
+        }
+        return $controller === $this->controller() && $action === $this->action();
+    }
 }
 
 abstract class IPF_Router_Shortcut extends Exception