*/ class AppAction extends AbstractAction { /** * @param mixed[] $params * @param MutableSessionHandle $session * @return HttpResponse * @throws Exception
* thrown if this action is required to be authenticated, but the given session does not contain a logged-in account. *
** Caller is supposed to ensure that the user is logged-in before, if `$this->isAuthRequired()` is true before * running this action. *
*/ public function run(array $params, $session): HttpResponse { $params = array_values($params); if ($this->isAuthRequired()) { if ($session->getAccount() == null) { throw new Exception("action requires authorization."); } } $params[] = $session; return call_user_func_array($this->action, $params); } /** * @param callable(mixed[]): HttpResponse $action * @return AppAction an action that does not require to have an authorization. */ public static function noAuth(callable $action): AppAction { return new AppAction($action, false); } /** * @param callable(mixed[]): HttpResponse $action * @return AppAction an action that does require to have an authorization. */ public static function auth(callable $action): AppAction { return new AppAction($action, true); } }