From fb037320d7a95c88aad4d9abb6a9225811131510 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 11 Aug 2013 22:16:09 +0300 Subject: [PATCH] call function by name --- ipf.php | 39 +++++++++++++++++++++++---------------- ipf/project_template.php | 4 ++-- ipf/router.php | 3 +-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/ipf.php b/ipf.php index 01d2a2a..0505f2e 100644 --- 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() diff --git a/ipf/project_template.php b/ipf/project_template.php index f1602b7..42d42fc 100644 --- a/ipf/project_template.php +++ b/ipf/project_template.php @@ -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); diff --git a/ipf/router.php b/ipf/router.php index efc373d..ffe2dac 100644 --- a/ipf/router.php +++ b/ipf/router.php @@ -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')); } -- 2.49.0