From d20cfd2486d842fd2c67d0f6b248f69965b5459a Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Fri, 24 Nov 2023 11:10:08 +0100 Subject: [PATCH] simplify actions, move ViewHttpResponse into src/App --- public/api/index.php | 76 ++---------- public/index.php | 113 ++++-------------- src/Api/API.php | 71 +++++++++++ src/Api/ApiAction.php | 46 ------- .../{ => Controller}/APIAuthController.php | 2 +- .../{ => Controller}/APITacticController.php | 23 +--- src/App/App.php | 90 ++++++++++++++ src/App/AppAction.php | 55 --------- src/{Core/Route => App}/Control.php | 2 +- src/App/Controller/AuthController.php | 2 +- src/App/Controller/EditorController.php | 2 +- src/App/Controller/TeamController.php | 2 +- src/App/Controller/UserController.php | 2 +- src/App/Controller/VisualizerController.php | 2 +- src/{Core/Http => App}/ViewHttpResponse.php | 5 +- src/Core/Action.php | 56 +++++++++ src/Core/Route/AbstractAction.php | 36 ------ 17 files changed, 258 insertions(+), 327 deletions(-) create mode 100644 src/Api/API.php delete mode 100644 src/Api/ApiAction.php rename src/Api/{ => Controller}/APIAuthController.php (96%) rename src/Api/{ => Controller}/APITacticController.php (60%) create mode 100644 src/App/App.php delete mode 100644 src/App/AppAction.php rename src/{Core/Route => App}/Control.php (98%) rename src/{Core/Http => App}/ViewHttpResponse.php (95%) create mode 100644 src/Core/Action.php delete mode 100644 src/Core/Route/AbstractAction.php diff --git a/public/api/index.php b/public/api/index.php index eeac0c1..076dd11 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -5,20 +5,16 @@ require "../../vendor/autoload.php"; require "../../sql/database.php"; require "../utils.php"; -use IQBall\Api\ApiAction; +use IQBall\Api\API; +use IQBall\Api\Controller\APIAuthController; +use IQBall\Api\Controller\APITacticController; +use IQBall\Core\Action; use IQBall\Core\Connection; -use IQBall\Api\APIAuthController; -use IQBall\Api\APITacticController; use IQBall\Core\Data\Account; use IQBall\Core\Gateway\AccountGateway; use IQBall\Core\Gateway\TacticInfoGateway; -use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\JsonHttpResponse; -use IQBall\Core\Http\ViewHttpResponse; use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\TacticModel; -use IQBall\Core\Session\PhpSessionHandle; -use IQBall\Core\Validation\ValidationFail; function getTacticController(): APITacticController { return new APITacticController(new TacticModel(new TacticInfoGateway(new Connection(get_database())))); @@ -32,68 +28,10 @@ function getRoutes(): AltoRouter { $router = new AltoRouter(); $router->setBasePath(get_public_path() . "/api"); - $router->map("POST", "/tactic/[i:id]/edit/name", ApiAction::auth(fn(int $id, Account $acc) => getTacticController()->updateName($id, $acc))); - $router->map("GET", "/tactic/[i:id]", ApiAction::auth(fn(int $id, Account $acc) => getTacticController()->getTacticInfo($id, $acc))); - $router->map("POST", "/tactic/new", ApiAction::auth(fn(Account $acc) => getTacticController()->newTactic($acc))); - $router->map("POST", "/auth", ApiAction::noAuth(fn() => getAuthController()->authorize())); + $router->map("POST", "/tactic/[i:id]/edit/name", Action::auth(fn(int $id, Account $acc) => getTacticController()->updateName($id, $acc))); + $router->map("POST", "/auth", Action::noAuth(fn() => getAuthController()->authorize())); return $router; } -/** - * @param mixed[] $match - * @return HttpResponse - * @throws Exception - */ -function handleMatch(array $match): HttpResponse { - if (!$match) { - return new JsonHttpResponse([ValidationFail::notFound("not found")]); - } - - $action = $match['target']; - if (!$action instanceof ApiAction) { - throw new Exception("routed action is not an AppAction object."); - } - - $auth = null; - - if ($action->isAuthRequired()) { - $auth = tryGetAuthorization(); - if ($auth == null) { - return new JsonHttpResponse([ValidationFail::unauthorized("Missing or invalid 'Authorization' header.")]); - } - } - - return $action->run($match['params'], $auth); -} - -function tryGetAuthorization(): ?Account { - $headers = getallheaders(); - - // If no authorization header is set, try fallback to php session. - if (!isset($headers['Authorization'])) { - $session = PhpSessionHandle::init(); - return $session->getAccount(); - } - - $token = $headers['Authorization']; - $gateway = new AccountGateway(new Connection(get_database())); - return $gateway->getAccountFromToken($token); -} - -function render(HttpResponse $response): void { - http_response_code($response->getCode()); - - foreach ($response->getHeaders() as $header => $value) { - header("$header: $value"); - } - - if ($response instanceof JsonHttpResponse) { - header('Content-type: application/json'); - echo $response->getJson(); - } elseif ($response instanceof ViewHttpResponse) { - throw new Exception("API returned a view http response."); - } -} - -render(handleMatch(getRoutes()->match())); +Api::render(API::handleMatch(getRoutes()->match())); diff --git a/public/index.php b/public/index.php index 07d58c1..705284d 100644 --- a/public/index.php +++ b/public/index.php @@ -7,32 +7,22 @@ require "../sql/database.php"; require "../src/utils.php"; require "../src/App/react-display.php"; -use IQBall\App\AppAction; +use IQBall\App\App; use IQBall\App\Controller\AuthController; use IQBall\App\Controller\EditorController; use IQBall\App\Controller\TeamController; use IQBall\App\Controller\UserController; use IQBall\App\Controller\VisualizerController; +use IQBall\Core\Action; use IQBall\Core\Connection; use IQBall\Core\Gateway\AccountGateway; use IQBall\Core\Gateway\TacticInfoGateway; use IQBall\Core\Gateway\TeamGateway; -use IQBall\Core\Http\HttpCodes; -use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\JsonHttpResponse; -use IQBall\Core\Http\ViewHttpResponse; use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\TacticModel; use IQBall\Core\Model\TeamModel; -use IQBall\Core\Session\MutableSessionHandle; use IQBall\Core\Session\PhpSessionHandle; use IQBall\Core\Session\SessionHandle; -use IQBall\Core\Validation\ValidationFail; -use Twig\Environment; -use Twig\Error\LoaderError; -use Twig\Error\RuntimeError; -use Twig\Error\SyntaxError; -use Twig\Loader\FilesystemLoader; function getConnection(): Connection { return new Connection(get_database()); @@ -65,96 +55,37 @@ function getRoutes(): AltoRouter { $ar->setBasePath($basePath); //authentication - $ar->map("GET", "/login", AppAction::noAuth(fn() => getAuthController()->displayLogin())); - $ar->map("GET", "/register", AppAction::noAuth(fn() => getAuthController()->displayRegister())); - $ar->map("POST", "/login", AppAction::noAuth(fn(SessionHandle $s) => getAuthController()->confirmLogin($_POST, $s))); - $ar->map("POST", "/register", AppAction::noAuth(fn(SessionHandle $s) => getAuthController()->confirmRegister($_POST, $s))); + $ar->map("GET", "/login", Action::noAuth(fn() => getAuthController()->displayLogin())); + $ar->map("GET", "/register", Action::noAuth(fn() => getAuthController()->displayRegister())); + $ar->map("POST", "/login", Action::noAuth(fn(SessionHandle $s) => getAuthController()->confirmLogin($_POST, $s))); + $ar->map("POST", "/register", Action::noAuth(fn(SessionHandle $s) => getAuthController()->confirmRegister($_POST, $s))); //user-related - $ar->map("GET", "/home", AppAction::auth(fn(SessionHandle $s) => getUserController()->home($s))); - $ar->map("GET", "/settings", AppAction::auth(fn(SessionHandle $s) => getUserController()->settings($s))); + $ar->map("GET", "/home", Action::auth(fn(SessionHandle $s) => getUserController()->home($s))); + $ar->map("GET", "/", Action::auth(fn(SessionHandle $s) => getUserController()->home($s))); + $ar->map("GET", "/settings", Action::auth(fn(SessionHandle $s) => getUserController()->settings($s))); //tactic-related - $ar->map("GET", "/tactic/[i:id]/view", AppAction::auth(fn(int $id, SessionHandle $s) => getVisualizerController()->visualize($id, $s))); - $ar->map("GET", "/tactic/[i:id]/edit", AppAction::auth(fn(int $id, SessionHandle $s) => getEditorController()->edit($id, $s))); - $ar->map("GET", "/tactic/new", AppAction::auth(fn(SessionHandle $s) => getEditorController()->createNew($s))); + $ar->map("GET", "/tactic/[i:id]/view", Action::auth(fn(int $id, SessionHandle $s) => getVisualizerController()->visualize($id, $s))); + $ar->map("GET", "/tactic/[i:id]/edit", Action::auth(fn(int $id, SessionHandle $s) => getEditorController()->edit($id, $s))); + $ar->map("GET", "/tactic/new", Action::auth(fn(SessionHandle $s) => getEditorController()->createNew($s))); //team-related - $ar->map("GET", "/team/new", AppAction::auth(fn(SessionHandle $s) => getTeamController()->displayCreateTeam($s))); - $ar->map("POST", "/team/new", AppAction::auth(fn(SessionHandle $s) => getTeamController()->submitTeam($_POST, $s))); - $ar->map("GET", "/team/search", AppAction::auth(fn(SessionHandle $s) => getTeamController()->displayListTeamByName($s))); - $ar->map("POST", "/team/search", AppAction::auth(fn(SessionHandle $s) => getTeamController()->listTeamByName($_POST, $s))); - $ar->map("GET", "/team/[i:id]", AppAction::auth(fn(int $id, SessionHandle $s) => getTeamController()->displayTeam($id, $s))); - $ar->map("GET", "/team/members/add", AppAction::auth(fn(SessionHandle $s) => getTeamController()->displayAddMember($s))); - $ar->map("POST", "/team/members/add", AppAction::auth(fn(SessionHandle $s) => getTeamController()->addMember($_POST, $s))); - $ar->map("GET", "/team/members/remove", AppAction::auth(fn(SessionHandle $s) => getTeamController()->displayDeleteMember($s))); - $ar->map("POST", "/team/members/remove", AppAction::auth(fn(SessionHandle $s) => getTeamController()->deleteMember($_POST, $s))); + $ar->map("GET", "/team/new", Action::auth(fn(SessionHandle $s) => getTeamController()->displayCreateTeam($s))); + $ar->map("POST", "/team/new", Action::auth(fn(SessionHandle $s) => getTeamController()->submitTeam($_POST, $s))); + $ar->map("GET", "/team/search", Action::auth(fn(SessionHandle $s) => getTeamController()->displayListTeamByName($s))); + $ar->map("POST", "/team/search", Action::auth(fn(SessionHandle $s) => getTeamController()->listTeamByName($_POST, $s))); + $ar->map("GET", "/team/[i:id]", Action::auth(fn(int $id, SessionHandle $s) => getTeamController()->displayTeam($id, $s))); + $ar->map("GET", "/team/members/add", Action::auth(fn(SessionHandle $s) => getTeamController()->displayAddMember($s))); + $ar->map("POST", "/team/members/add", Action::auth(fn(SessionHandle $s) => getTeamController()->addMember($_POST, $s))); + $ar->map("GET", "/team/members/remove", Action::auth(fn(SessionHandle $s) => getTeamController()->displayDeleteMember($s))); + $ar->map("POST", "/team/members/remove", Action::auth(fn(SessionHandle $s) => getTeamController()->deleteMember($_POST, $s))); return $ar; } -function render(HttpResponse $response): void { - http_response_code($response->getCode()); - - foreach ($response->getHeaders() as $header => $value) { - header("$header: $value"); - } - - if ($response instanceof ViewHttpResponse) { - renderView($response); - } elseif ($response instanceof JsonHttpResponse) { - header('Content-type: application/json'); - echo $response->getJson(); - } -} - -function renderView(ViewHttpResponse $response): void { - $file = $response->getFile(); - $args = $response->getArguments(); - - switch ($response->getViewKind()) { - case ViewHttpResponse::REACT_VIEW: - send_react_front($file, $args); - break; - case ViewHttpResponse::TWIG_VIEW: - try { - $loader = new FilesystemLoader('../src/Views/'); - $twig = new Environment($loader); - $twig->display($file, $args); - } catch (RuntimeError | SyntaxError | LoaderError $e) { - http_response_code(500); - echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s"); - throw $e; - } - break; - } -} - -function runAction(AppAction $action, array $params, MutableSessionHandle $session): HttpResponse { - global $basePath; - if ($action->isAuthRequired()) { - $account = $session->getAccount(); - if ($account == null) { - // put in the session the initial url the user wanted to get - $session->setInitialTarget($_SERVER['REQUEST_URI']); - return HttpResponse::redirect($basePath . "/login"); - } - } - - return $action->run($params, $session); -} - -function runMatch(array $match, MutableSessionHandle $session): HttpResponse { - if (!$match) { - return ViewHttpResponse::twig("error.html.twig", [ - 'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")], - ], HttpCodes::NOT_FOUND); - } - - return runAction($match['target'], $match['params'], $session); -} //this is a global variable $basePath = get_public_path(); -render(runMatch(getRoutes()->match(), PhpSessionHandle::init())); +App::render(App::runMatch(getRoutes()->match(), PhpSessionHandle::init())); diff --git a/src/Api/API.php b/src/Api/API.php new file mode 100644 index 0000000..d266e79 --- /dev/null +++ b/src/Api/API.php @@ -0,0 +1,71 @@ +getCode()); + + foreach ($response->getHeaders() as $header => $value) { + header("$header: $value"); + } + + if ($response instanceof JsonHttpResponse) { + header('Content-type: application/json'); + echo $response->getJson(); + } else { + throw new Exception("API returned a non-json response."); + } + } + + /** + * @param mixed[] $match + * @return HttpResponse + * @throws Exception + */ + public static function handleMatch(array $match): HttpResponse { + if (!$match) { + return new JsonHttpResponse([ValidationFail::notFound("not found")]); + } + + $action = $match['target']; + if (!$action instanceof Action) { + throw new Exception("routed action is not an AppAction object."); + } + + $auth = null; + + if ($action->isAuthRequired()) { + $auth = self::tryGetAuthorization(); + if ($auth == null) { + return new JsonHttpResponse([ValidationFail::unauthorized("Missing or invalid 'Authorization' header.")]); + } + } + + return $action->run($match['params'], $auth); + } + + private static function tryGetAuthorization(): ?Account { + $headers = getallheaders(); + + // If no authorization header is set, try fallback to php session. + if (!isset($headers['Authorization'])) { + $session = PhpSessionHandle::init(); + return $session->getAccount(); + } + + $token = $headers['Authorization']; + $gateway = new AccountGateway(new Connection(get_database())); + return $gateway->getAccountFromToken($token); + } +} diff --git a/src/Api/ApiAction.php b/src/Api/ApiAction.php deleted file mode 100644 index 9cfd0aa..0000000 --- a/src/Api/ApiAction.php +++ /dev/null @@ -1,46 +0,0 @@ - - */ -class ApiAction extends AbstractAction { - - /** - * @param mixed[] $params - * @param ?Account $session - * @return HttpResponse - */ - public function run(array $params, $session): HttpResponse { - $params = array_values($params); - if ($this->isAuthRequired()) { - if ($session == null) { - throw new \Exception("action requires authorization."); - } - $params[] = $session; - } - - return call_user_func_array($this->action, $params); - } - - /** - * @param callable(mixed[]): HttpResponse $action - * @return ApiAction an action that does not require to have an authorization. - */ - public static function noAuth(callable $action): ApiAction { - return new ApiAction($action, false); - } - - /** - * @param callable(mixed[]): HttpResponse $action - * @return ApiAction an action that does require to have an authorization. - */ - public static function auth(callable $action): ApiAction { - return new ApiAction($action, true); - } -} \ No newline at end of file diff --git a/src/Api/APIAuthController.php b/src/Api/Controller/APIAuthController.php similarity index 96% rename from src/Api/APIAuthController.php rename to src/Api/Controller/APIAuthController.php index c4f5976..59ec1d0 100644 --- a/src/Api/APIAuthController.php +++ b/src/Api/Controller/APIAuthController.php @@ -1,6 +1,6 @@ [Validators::lenBetween(1, 50), Validators::nameWithSpaces()], - ], function (HttpRequest $request) use ($account) { - $tactic = $this->model->makeNew($request["name"], $account->getId()); - $id = $tactic->getId(); - return new JsonHttpResponse(["id" => $id]); - }, true); - } - - public function getTacticInfo(int $id, Account $account): HttpResponse { - $tactic_info = $this->model->get($id); - - if ($tactic_info == null) { - return new JsonHttpResponse("could not find tactic #$id", HttpCodes::NOT_FOUND); - } - - return new JsonHttpResponse($tactic_info); - } - } diff --git a/src/App/App.php b/src/App/App.php new file mode 100644 index 0000000..9126afd --- /dev/null +++ b/src/App/App.php @@ -0,0 +1,90 @@ +getCode()); + + foreach ($response->getHeaders() as $header => $value) { + header("$header: $value"); + } + + if ($response instanceof ViewHttpResponse) { + self::renderView($response); + } elseif ($response instanceof JsonHttpResponse) { + header('Content-type: application/json'); + echo $response->getJson(); + } + } + + private static function renderView(ViewHttpResponse $response): void { + $file = $response->getFile(); + $args = $response->getArguments(); + + switch ($response->getViewKind()) { + case ViewHttpResponse::REACT_VIEW: + send_react_front($file, $args); + break; + case ViewHttpResponse::TWIG_VIEW: + try { + $loader = new FilesystemLoader('../src/App/Views/'); + $twig = new Environment($loader); + $twig->display($file, $args); + } catch (RuntimeError | SyntaxError | LoaderError $e) { + http_response_code(500); + echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s"); + throw $e; + } + break; + } + } + + /** + * @param Action $action + * @param mixed[] $params + * @param MutableSessionHandle $session + * @return HttpResponse + */ + private static function runAction(Action $action, array $params, MutableSessionHandle $session): HttpResponse { + global $basePath; + if ($action->isAuthRequired()) { + $account = $session->getAccount(); + if ($account == null) { + // put in the session the initial url the user wanted to get + $session->setInitialTarget($_SERVER['REQUEST_URI']); + return HttpResponse::redirect($basePath . "/login"); + } + } + + return $action->run($params, $session); + } + + /** + * @param array|false $match + * @param MutableSessionHandle $session + * @return HttpResponse + */ + public static function runMatch($match, MutableSessionHandle $session): HttpResponse { + if (!$match) { + return ViewHttpResponse::twig("error.html.twig", [ + 'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")], + ], HttpCodes::NOT_FOUND); + } + + return self::runAction($match['target'], $match['params'], $session); + } + +} diff --git a/src/App/AppAction.php b/src/App/AppAction.php deleted file mode 100644 index 0e4dcdf..0000000 --- a/src/App/AppAction.php +++ /dev/null @@ -1,55 +0,0 @@ - - */ -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); - } -} diff --git a/src/Core/Route/Control.php b/src/App/Control.php similarity index 98% rename from src/Core/Route/Control.php rename to src/App/Control.php index f29557d..3dea9b1 100644 --- a/src/Core/Route/Control.php +++ b/src/App/Control.php @@ -2,11 +2,11 @@ namespace IQBall\Core\Route; +use IQBall\App\ViewHttpResponse; use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; -use IQBall\Core\Http\ViewHttpResponse; use IQBall\Core\Validation\ValidationFail; use IQBall\Core\Validation\Validator; diff --git a/src/App/Controller/AuthController.php b/src/App/Controller/AuthController.php index f74e45d..46ef838 100644 --- a/src/App/Controller/AuthController.php +++ b/src/App/Controller/AuthController.php @@ -4,7 +4,7 @@ namespace IQBall\App\Controller; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\ViewHttpResponse; +use IQBall\App\ViewHttpResponse; use IQBall\Core\Model\AuthModel; use IQBall\Core\Session\MutableSessionHandle; use IQBall\Core\Validation\ValidationFail; diff --git a/src/App/Controller/EditorController.php b/src/App/Controller/EditorController.php index 0b47cba..b004e10 100644 --- a/src/App/Controller/EditorController.php +++ b/src/App/Controller/EditorController.php @@ -5,7 +5,7 @@ namespace IQBall\App\Controller; use IQBall\Core\Data\TacticInfo; use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\ViewHttpResponse; +use IQBall\App\ViewHttpResponse; use IQBall\Core\Model\TacticModel; use IQBall\Core\Session\SessionHandle; use IQBall\Core\Validator\TacticValidator; diff --git a/src/App/Controller/TeamController.php b/src/App/Controller/TeamController.php index d8db9e9..c34bfa8 100644 --- a/src/App/Controller/TeamController.php +++ b/src/App/Controller/TeamController.php @@ -4,7 +4,7 @@ namespace IQBall\App\Controller; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\ViewHttpResponse; +use IQBall\App\ViewHttpResponse; use IQBall\Core\Model\TeamModel; use IQBall\Core\Session\SessionHandle; use IQBall\Core\Validation\FieldValidationFail; diff --git a/src/App/Controller/UserController.php b/src/App/Controller/UserController.php index 5d45038..7fd1a8d 100644 --- a/src/App/Controller/UserController.php +++ b/src/App/Controller/UserController.php @@ -3,7 +3,7 @@ namespace IQBall\App\Controller; use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\ViewHttpResponse; +use IQBall\App\ViewHttpResponse; use IQBall\Core\Model\TacticModel; use IQBall\Core\Session\SessionHandle; diff --git a/src/App/Controller/VisualizerController.php b/src/App/Controller/VisualizerController.php index 46946dc..22a4367 100644 --- a/src/App/Controller/VisualizerController.php +++ b/src/App/Controller/VisualizerController.php @@ -4,7 +4,7 @@ namespace IQBall\App\Controller; use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpResponse; -use IQBall\Core\Http\ViewHttpResponse; +use IQBall\App\ViewHttpResponse; use IQBall\Core\Model\TacticModel; use IQBall\Core\Session\SessionHandle; use IQBall\Core\Validator\TacticValidator; diff --git a/src/Core/Http/ViewHttpResponse.php b/src/App/ViewHttpResponse.php similarity index 95% rename from src/Core/Http/ViewHttpResponse.php rename to src/App/ViewHttpResponse.php index 1f7c692..dfbd1da 100644 --- a/src/Core/Http/ViewHttpResponse.php +++ b/src/App/ViewHttpResponse.php @@ -1,6 +1,9 @@ action = $action; + $this->isAuthRequired = $isAuthRequired; + } + + public function isAuthRequired(): bool { + return $this->isAuthRequired; + } + + /** + * @param mixed[] $params + * @param S $session + * @return HttpResponse + */ + public function run(array $params, $session): HttpResponse { + $params = array_values($params); + $params[] = $session; + return call_user_func_array($this->action, $params); + } + + /** + * @param callable(mixed[], S): HttpResponse $action + * @return Action an action that does not require to have an authorization. + */ + public static function noAuth(callable $action): Action { + return new Action($action, false); + } + + /** + * @param callable(mixed[], S): HttpResponse $action + * @return Action an action that does require to have an authorization. + */ + public static function auth(callable $action): Action { + return new Action($action, true); + } +} diff --git a/src/Core/Route/AbstractAction.php b/src/Core/Route/AbstractAction.php deleted file mode 100644 index 443d071..0000000 --- a/src/Core/Route/AbstractAction.php +++ /dev/null @@ -1,36 +0,0 @@ -action = $action; - $this->isAuthRequired = $isAuthRequired; - } - - public function isAuthRequired(): bool { - return $this->isAuthRequired; - } - - /** - * @param mixed[] $params - * @param S $session - * @return HttpResponse - */ - public abstract function run(array $params, $session): HttpResponse; -} \ No newline at end of file