]> git.andy128k.dev Git - ipf.git/commitdiff
call function by name
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 11 Aug 2013 19:16:09 +0000 (22:16 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 11 Aug 2013 19:16:09 +0000 (22:16 +0300)
ipf.php
ipf/project_template.php
ipf/router.php

diff --git a/ipf.php b/ipf.php
index 01d2a2a471e1fcd3a0fecc57bf4dcb6c6c69175b..0505f2e6c0ece04a8a2bf1a1f70c6cda87e29c84 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -112,23 +112,30 @@ final class IPF
             include_once $filename;
     }
 
-    public static function loadFunction($function)
+    public static function callFunction($function, array $args)
     {
-        if (function_exists($function))
-            return;
-
-        if (preg_match('/^(\w+)::\w+$/', $function, $m))
-            return; // nothing to do. autoloader will load a class.
-
-        $elts = explode('_', $function);
-        array_pop($elts);
-        $file = '/' . strtolower(implode(DIRECTORY_SEPARATOR, $elts)).'.php';
-
-        self::include_existing(IPF::$settings['ipf_path'] . $file);
-        self::include_existing(IPF::$settings['project_path'] . $file);
-
-        if (!function_exists($function))
-            throw new IPF_Exception('Impossible to load the function: '.$function.' in '.$file);
+        if (is_array($function)) {
+            // object/class method
+            $callable = $function;
+        } elseif (preg_match('/^(\w+)::(\w+)$/', $function, $m)) {
+            // static method
+            $callable = array($m[1], $m[2]);
+        } else {
+            // plain function
+            if (!function_exists($function)) {
+                $elts = explode('_', $function);
+                array_pop($elts);
+                $file = '/' . strtolower(implode(DIRECTORY_SEPARATOR, $elts)).'.php';
+
+                self::include_existing(IPF::$settings['ipf_path'] . $file);
+                self::include_existing(IPF::$settings['project_path'] . $file);
+
+                if (!function_exists($function))
+                    throw new IPF_Exception('Impossible to load the function: '.$function.' in '.$file);
+            }
+            $callable = $function;
+        }
+        return call_user_func_array($callable, $args);
     }
 
     public static function getUploadPath()
index f1602b7a037942f871c87768bc7115d6ff2f5a4e..42d42fcbd8e064618437cc746141ba1650669fe8 100644 (file)
@@ -45,8 +45,8 @@ final class IPF_Project_Template
         if ($request) {
             $params = array_merge(array('request' => $request), $params);
             foreach (IPF::get('template_context_processors', array()) as $proc) {
-                IPF::loadFunction($proc);
-                $params = array_merge($proc($request), $params);
+                $c = IPF::callFunction($proc, array($request));
+                $params = array_merge($c, $params);
             }
             foreach (IPF_Project::getInstance()->appList() as $app) {
                 $params = array_merge($app->templateContext($request), $params);
index efc373d87f3ffcedc04b2e399e239eb9c496a94c..ffe2dacb50dc4266dcf9f9c3a91fcdb701c77948 100644 (file)
@@ -86,8 +86,7 @@ class IPF_Router
 
         if ($func) {
             try {
-                IPF::loadFunction($func);
-                $r = $func($req, $match);
+                $r = IPF::callFunction($func, array($req, $match));
                 if (!is_a($r, 'IPF_HTTP_Response')) {
                     return self::response500(new IPF_Exception('function '.$func.'() must return IPF_HTTP_Response instance'));
                 }