Base::call PHP Méthode

call() public méthode

Execute callback/hooks (supports 'class->method' format)
public call ( $func, $args = NULL, $hooks = '' ) : mixed | FALSE
$func callback
$args mixed
$hooks string
Résultat mixed | FALSE
    function call($func, $args = NULL, $hooks = '')
    {
        if (!is_array($args)) {
            $args = [$args];
        }
        // Grab the real handler behind the string representation
        if (is_string($func)) {
            $func = $this->grab($func, $args);
        }
        // Execute function; abort if callback/hook returns FALSE
        if (!is_callable($func)) {
            // No route handler
            if ($hooks == 'beforeroute,afterroute') {
                $allowed = [];
                if (is_array($func)) {
                    $allowed = array_intersect(array_map('strtoupper', get_class_methods($func[0])), explode('|', self::VERBS));
                }
                header('Allow: ' . implode(',', $allowed));
                $this->error(405);
            } else {
                user_error(sprintf(self::E_Method, is_string($func) ? $func : $this->stringify($func)), E_USER_ERROR);
            }
        }
        $obj = FALSE;
        if (is_array($func)) {
            $hooks = $this->split($hooks);
            $obj = TRUE;
        }
        // Execute pre-route hook if any
        if ($obj && $hooks && in_array($hook = 'beforeroute', $hooks) && method_exists($func[0], $hook) && call_user_func_array([$func[0], $hook], $args) === FALSE) {
            return FALSE;
        }
        // Execute callback
        $out = call_user_func_array($func, $args ?: []);
        if ($out === FALSE) {
            return FALSE;
        }
        // Execute post-route hook if any
        if ($obj && $hooks && in_array($hook = 'afterroute', $hooks) && method_exists($func[0], $hook) && call_user_func_array([$func[0], $hook], $args) === FALSE) {
            return FALSE;
        }
        return $out;
    }

Usage Example

Exemple #1
0
 /**
  * auth service callback
  * @param Base $f3
  * @param $params
  */
 function callback(\Base $f3, $params)
 {
     $Opauth = new \Opauth($this->config, false);
     switch ($Opauth->env['callback_transport']) {
         case 'session':
             $response = $f3->get('SESSION.opauth');
             $f3->clear('SESSION.opauth');
             break;
         case 'post':
             $response = unserialize(base64_decode($f3->get('POST.opauth')));
             break;
         case 'get':
             $response = unserialize(base64_decode($f3->get('GET.opauth')));
             break;
         default:
             $f3->error(400, 'Unsupported callback_transport');
             break;
     }
     if (isset($response['error'])) {
         $f3->call($this->abortFunc, array($response));
         return;
     }
     $data = $response['auth'];
     // validate
     if (empty($data) || empty($response['timestamp']) || empty($response['signature']) || empty($data['provider']) || empty($data['uid'])) {
         $f3->error(400, 'Invalid auth response: Missing key auth response components');
     } elseif (!$Opauth->validate(sha1(print_r($data, true)), $response['timestamp'], $response['signature'], $reason)) {
         $f3->error(400, 'Invalid auth response: ' . $reason);
     } else {
         // It's all good
         $f3->call($this->successFunc, array($data));
     }
 }
All Usage Examples Of Base::call