Merge branch 'salva' of https://codefirst.iut.uca.fr/git/IQBall/Application-Web into salva
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
commit
49cdcb8972
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IQBall\Api;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use IQBall\Core\Action;
|
||||||
|
use IQBall\Core\Connection;
|
||||||
|
use IQBall\Core\Data\Account;
|
||||||
|
use IQBall\Core\Gateway\AccountGateway;
|
||||||
|
use IQBall\Core\Http\HttpResponse;
|
||||||
|
use IQBall\Core\Http\JsonHttpResponse;
|
||||||
|
use IQBall\Core\Session\PhpSessionHandle;
|
||||||
|
use IQBall\Core\Validation\ValidationFail;
|
||||||
|
|
||||||
|
class API {
|
||||||
|
public static 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();
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Controller\Api;
|
namespace IQBall\Api\Controller;
|
||||||
|
|
||||||
use App\Controller\Control;
|
use IQBall\Core\Route\Control;
|
||||||
use App\Http\HttpRequest;
|
use IQBall\Core\Http\HttpRequest;
|
||||||
use App\Http\HttpResponse;
|
use IQBall\Core\Http\HttpResponse;
|
||||||
use App\Http\JsonHttpResponse;
|
use IQBall\Core\Http\JsonHttpResponse;
|
||||||
use App\Model\AuthModel;
|
use IQBall\Core\Model\AuthModel;
|
||||||
use App\Validation\Validators;
|
use IQBall\Core\Validation\Validators;
|
||||||
|
|
||||||
class APIAuthController {
|
class APIAuthController {
|
||||||
private AuthModel $model;
|
private AuthModel $model;
|
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IQBall\Api\Controller;
|
||||||
|
|
||||||
|
use IQBall\Core\Route\Control;
|
||||||
|
use IQBall\Core\Data\Account;
|
||||||
|
use IQBall\Core\Http\HttpCodes;
|
||||||
|
use IQBall\Core\Http\HttpRequest;
|
||||||
|
use IQBall\Core\Http\HttpResponse;
|
||||||
|
use IQBall\Core\Http\JsonHttpResponse;
|
||||||
|
use IQBall\Core\Model\TacticModel;
|
||||||
|
use IQBall\Core\Validation\Validators;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API endpoint related to tactics
|
||||||
|
*/
|
||||||
|
class APITacticController {
|
||||||
|
private TacticModel $model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param TacticModel $model
|
||||||
|
*/
|
||||||
|
public function __construct(TacticModel $model) {
|
||||||
|
$this->model = $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateName(int $tactic_id, Account $account): HttpResponse {
|
||||||
|
return Control::runChecked([
|
||||||
|
"name" => [Validators::lenBetween(1, 50), Validators::nameWithSpaces()],
|
||||||
|
], function (HttpRequest $request) use ($tactic_id, $account) {
|
||||||
|
|
||||||
|
$failures = $this->model->updateName($tactic_id, $request["name"], $account->getId());
|
||||||
|
|
||||||
|
if (!empty($failures)) {
|
||||||
|
//TODO find a system to handle Unauthorized error codes more easily from failures.
|
||||||
|
return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
return HttpResponse::fromCode(HttpCodes::OK);
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IQBall\App;
|
||||||
|
|
||||||
|
use IQBall\Core\Action;
|
||||||
|
use IQBall\Core\Http\HttpCodes;
|
||||||
|
use IQBall\Core\Http\HttpResponse;
|
||||||
|
use IQBall\Core\Http\JsonHttpResponse;
|
||||||
|
use IQBall\Core\Session\MutableSessionHandle;
|
||||||
|
use IQBall\Core\Validation\ValidationFail;
|
||||||
|
use Twig\Environment;
|
||||||
|
use Twig\Error\LoaderError;
|
||||||
|
use Twig\Error\RuntimeError;
|
||||||
|
use Twig\Error\SyntaxError;
|
||||||
|
use Twig\Loader\FilesystemLoader;
|
||||||
|
|
||||||
|
class App {
|
||||||
|
public static function render(HttpResponse $response): void {
|
||||||
|
http_response_code($response->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<MutableSessionHandle> $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<string, mixed>|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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,14 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace IQBall\App\Controller;
|
||||||
|
|
||||||
use App\Data\TacticInfo;
|
use IQBall\Core\Data\TacticInfo;
|
||||||
use App\Http\HttpCodes;
|
use IQBall\Core\Http\HttpCodes;
|
||||||
use App\Http\HttpResponse;
|
use IQBall\Core\Http\HttpResponse;
|
||||||
use App\Http\ViewHttpResponse;
|
use IQBall\App\ViewHttpResponse;
|
||||||
use App\Model\TacticModel;
|
use IQBall\Core\Model\TacticModel;
|
||||||
use App\Session\SessionHandle;
|
use IQBall\Core\Session\SessionHandle;
|
||||||
use App\Validator\TacticValidator;
|
use IQBall\Core\Validator\TacticValidator;
|
||||||
|
|
||||||
class EditorController {
|
class EditorController {
|
||||||
private TacticModel $model;
|
private TacticModel $model;
|
@ -1,16 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace IQBall\App\Controller;
|
||||||
|
|
||||||
use App\Connexion;
|
use IQBall\Core\Http\HttpResponse;
|
||||||
use App\Gateway\TacticInfoGateway;
|
use IQBall\App\ViewHttpResponse;
|
||||||
use App\Http\HttpResponse;
|
use IQBall\Core\Model\TacticModel;
|
||||||
use App\Http\ViewHttpResponse;
|
use IQBall\Core\Session\SessionHandle;
|
||||||
use App\Model\TacticModel;
|
|
||||||
use App\Session\SessionHandle;
|
|
||||||
|
|
||||||
class UserController {
|
class UserController {
|
||||||
|
|
||||||
private TacticModel $tactics;
|
private TacticModel $tactics;
|
||||||
|
|
||||||
/**
|
/**
|
@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace IQBall\App\Controller;
|
||||||
|
|
||||||
use App\Http\HttpCodes;
|
use IQBall\Core\Http\HttpCodes;
|
||||||
use App\Http\HttpResponse;
|
use IQBall\Core\Http\HttpResponse;
|
||||||
use App\Http\ViewHttpResponse;
|
use IQBall\App\ViewHttpResponse;
|
||||||
use App\Model\TacticModel;
|
use IQBall\Core\Model\TacticModel;
|
||||||
use App\Session\SessionHandle;
|
use IQBall\Core\Session\SessionHandle;
|
||||||
use App\Validator\TacticValidator;
|
use IQBall\Core\Validator\TacticValidator;
|
||||||
|
|
||||||
class VisualizerController {
|
class VisualizerController {
|
||||||
private TacticModel $tacticModel;
|
private TacticModel $tacticModel;
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace IQBall\App;
|
||||||
|
|
||||||
|
use IQBall\Core\Http\HttpCodes;
|
||||||
|
use IQBall\Core\Http\HttpResponse;
|
||||||
|
|
||||||
class ViewHttpResponse extends HttpResponse {
|
class ViewHttpResponse extends HttpResponse {
|
||||||
public const TWIG_VIEW = 0;
|
public const TWIG_VIEW = 0;
|
@ -1,63 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Controller\Api;
|
|
||||||
|
|
||||||
use App\Controller\Control;
|
|
||||||
use App\Data\Account;
|
|
||||||
use App\Http\HttpCodes;
|
|
||||||
use App\Http\HttpRequest;
|
|
||||||
use App\Http\HttpResponse;
|
|
||||||
use App\Http\JsonHttpResponse;
|
|
||||||
use App\Model\TacticModel;
|
|
||||||
use App\Validation\Validators;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* API endpoint related to tactics
|
|
||||||
*/
|
|
||||||
class APITacticController {
|
|
||||||
private TacticModel $model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param TacticModel $model
|
|
||||||
*/
|
|
||||||
public function __construct(TacticModel $model) {
|
|
||||||
$this->model = $model;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updateName(int $tactic_id, Account $account): HttpResponse {
|
|
||||||
return Control::runChecked([
|
|
||||||
"name" => [Validators::lenBetween(1, 50), Validators::nameWithSpaces()],
|
|
||||||
], function (HttpRequest $request) use ($tactic_id, $account) {
|
|
||||||
|
|
||||||
$failures = $this->model->updateName($tactic_id, $request["name"], $account->getId());
|
|
||||||
|
|
||||||
if (!empty($failures)) {
|
|
||||||
//TODO find a system to handle Unauthorized error codes more easily from failures.
|
|
||||||
return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
return HttpResponse::fromCode(HttpCodes::OK);
|
|
||||||
}, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function newTactic(Account $account): HttpResponse {
|
|
||||||
return Control::runChecked([
|
|
||||||
"name" => [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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Controller\Route;
|
|
||||||
|
|
||||||
use App\Http\HttpResponse;
|
|
||||||
use App\Session\SessionHandle;
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A Front controller action
|
|
||||||
*/
|
|
||||||
class Action {
|
|
||||||
/**
|
|
||||||
* @var callable(mixed[]): HttpResponse $action action to call
|
|
||||||
*/
|
|
||||||
private $action;
|
|
||||||
|
|
||||||
private bool $isAuthRequired;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param callable(mixed[]): HttpResponse $action
|
|
||||||
*/
|
|
||||||
private function __construct(callable $action, bool $isAuthRequired) {
|
|
||||||
$this->action = $action;
|
|
||||||
$this->isAuthRequired = $isAuthRequired;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isAuthRequired(): bool {
|
|
||||||
return $this->isAuthRequired;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed[] $params
|
|
||||||
* @param SessionHandle $session
|
|
||||||
* @return HttpResponse
|
|
||||||
* @throws Exception <p>
|
|
||||||
* thrown if this action is required to be authenticated, but the given session does not contain a logged-in account.
|
|
||||||
* </p>
|
|
||||||
* <p>
|
|
||||||
* Caller is supposed to ensure that the user is logged-in before, if `$this->isAuthRequired()` is true before
|
|
||||||
* running this action.
|
|
||||||
* </p>
|
|
||||||
*/
|
|
||||||
public function run(array $params, SessionHandle $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 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[]): 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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,143 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Controller\Route;
|
|
||||||
|
|
||||||
use AltoRouter;
|
|
||||||
use App\Http\HttpCodes;
|
|
||||||
use App\Http\HttpResponse;
|
|
||||||
use App\Http\JsonHttpResponse;
|
|
||||||
use App\Http\ViewHttpResponse;
|
|
||||||
use App\Session\MutableSessionHandle;
|
|
||||||
use App\Validation\ValidationFail;
|
|
||||||
use Twig\Environment;
|
|
||||||
use Twig\Error\LoaderError;
|
|
||||||
use Twig\Error\RuntimeError;
|
|
||||||
use Twig\Error\SyntaxError;
|
|
||||||
use Twig\Loader\FilesystemLoader;
|
|
||||||
|
|
||||||
class FrontController {
|
|
||||||
private AltoRouter $router;
|
|
||||||
private string $basePath;
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct(string $basePath) {
|
|
||||||
$this->router = $this->createRouter($basePath);
|
|
||||||
$this->basePath = $basePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRoute(string $method, string $path, Action $action): void {
|
|
||||||
$this->router->map($method, $path, $action);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param MutableSessionHandle $session
|
|
||||||
* @return void
|
|
||||||
* @throws LoaderError
|
|
||||||
* @throws RuntimeError
|
|
||||||
* @throws SyntaxError
|
|
||||||
*/
|
|
||||||
public function run(MutableSessionHandle $session): void {
|
|
||||||
$match = $this->router->match();
|
|
||||||
if ($match) {
|
|
||||||
$this->handleMatch($match, $session);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [
|
|
||||||
'failures' => [ValidationFail::notFound("Could not find page ${_SERVER['REQUEST_URI']}.")],
|
|
||||||
], HttpCodes::NOT_FOUND));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new instance of an AltoRouter
|
|
||||||
*
|
|
||||||
* @param string $basePath
|
|
||||||
* @return AltoRouter
|
|
||||||
*/
|
|
||||||
public function createRouter(string $basePath): AltoRouter {
|
|
||||||
$router = new AltoRouter();
|
|
||||||
$router->setBasePath($basePath);
|
|
||||||
return $router;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array<string, mixed> $match
|
|
||||||
* @param MutableSessionHandle $session
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function handleMatch(array $match, MutableSessionHandle $session): void {
|
|
||||||
$action = $match['target'];
|
|
||||||
$params = array_values($match["params"]);
|
|
||||||
$this->handleResponseByType($this->tryToCall($action, $params, $session));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Action $action
|
|
||||||
* @param array<int, mixed> $params
|
|
||||||
* @param MutableSessionHandle $session
|
|
||||||
* @return HttpResponse
|
|
||||||
*/
|
|
||||||
private function tryToCall(Action $action, array $params, MutableSessionHandle $session): HttpResponse {
|
|
||||||
$account = null;
|
|
||||||
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($this->basePath . "/login");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $action->run($params, $session);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Redirect the return by the response's type
|
|
||||||
*
|
|
||||||
* @param HttpResponse $response
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function handleResponseByType(HttpResponse $response): void {
|
|
||||||
http_response_code($response->getCode());
|
|
||||||
|
|
||||||
foreach ($response->getHeaders() as $header => $value) {
|
|
||||||
header("$header: $value");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($response instanceof ViewHttpResponse) {
|
|
||||||
$this->displayViewByKind($response);
|
|
||||||
} elseif ($response instanceof JsonHttpResponse) {
|
|
||||||
header('Content-type: application/json');
|
|
||||||
echo $response->getJson();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use the right method to display the response
|
|
||||||
*
|
|
||||||
* @param ViewHttpResponse $response
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function displayViewByKind(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace IQBall\Core;
|
||||||
|
|
||||||
|
use IQBall\Core\Http\HttpResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template S session
|
||||||
|
*/
|
||||||
|
class Action {
|
||||||
|
/**
|
||||||
|
* @var callable(mixed[], S): HttpResponse $action action to call
|
||||||
|
*/
|
||||||
|
protected $action;
|
||||||
|
|
||||||
|
private bool $isAuthRequired;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param callable(mixed[], S): HttpResponse $action
|
||||||
|
*/
|
||||||
|
protected function __construct(callable $action, bool $isAuthRequired) {
|
||||||
|
$this->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<S> 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<S> an action that does require to have an authorization.
|
||||||
|
*/
|
||||||
|
public static function auth(callable $action): Action {
|
||||||
|
return new Action($action, true);
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App;
|
namespace IQBall\Core;
|
||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
class Connexion {
|
class Connection {
|
||||||
private PDO $pdo;
|
private PDO $pdo;
|
||||||
|
|
||||||
/**
|
/**
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Data;
|
namespace IQBall\Core\Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class of a user account.
|
* Base class of a user account.
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Data;
|
namespace IQBall\Core\Data;
|
||||||
|
|
||||||
use http\Url;
|
use http\Url;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Data;
|
namespace IQBall\Core\Data;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Data;
|
namespace IQBall\Core\Data;
|
||||||
|
|
||||||
use http\Exception\InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enumeration class workaround
|
* Enumeration class workaround
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Data;
|
namespace IQBall\Core\Data;
|
||||||
|
|
||||||
class TacticInfo implements \JsonSerializable {
|
class TacticInfo implements \JsonSerializable {
|
||||||
private int $id;
|
private int $id;
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Data;
|
namespace IQBall\Core\Data;
|
||||||
|
|
||||||
use http\Url;
|
use http\Url;
|
||||||
|
|
@ -1,18 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Gateway;
|
namespace IQBall\Core\Gateway;
|
||||||
|
|
||||||
use App\Connexion;
|
use IQBall\Core\Connection;
|
||||||
use App\Data\Account;
|
use IQBall\Core\Data\Account;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
class AccountGateway {
|
class AccountGateway {
|
||||||
private Connexion $con;
|
private Connection $con;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connexion $con
|
* @param Connection $con
|
||||||
*/
|
*/
|
||||||
public function __construct(Connexion $con) {
|
public function __construct(Connection $con) {
|
||||||
$this->con = $con;
|
$this->con = $con;
|
||||||
}
|
}
|
||||||
|
|
@ -1,17 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Gateway;
|
namespace IQBall\Core\Gateway;
|
||||||
|
|
||||||
use App\Connexion;
|
use IQBall\Core\Connection;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
class AuthGateway {
|
class AuthGateway {
|
||||||
private Connexion $con;
|
private Connection $con;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connexion $con
|
* @param Connection $con
|
||||||
*/
|
*/
|
||||||
public function __construct(Connexion $con) {
|
public function __construct(Connection $con) {
|
||||||
$this->con = $con;
|
$this->con = $con;
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace IQBall\Core\Http;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class to define constants of used http codes
|
* Utility class to define constants of used http codes
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace IQBall\Core\Http;
|
||||||
|
|
||||||
use App\Validation\FieldValidationFail;
|
use IQBall\Core\Validation\FieldValidationFail;
|
||||||
use App\Validation\Validation;
|
use IQBall\Core\Validation\Validation;
|
||||||
use App\Validation\ValidationFail;
|
use IQBall\Core\Validation\ValidationFail;
|
||||||
use App\Validation\Validator;
|
use IQBall\Core\Validation\Validator;
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace IQBall\Core\Http;
|
||||||
|
|
||||||
class HttpResponse {
|
class HttpResponse {
|
||||||
/**
|
/**
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http;
|
namespace IQBall\Core\Http;
|
||||||
|
|
||||||
class JsonHttpResponse extends HttpResponse {
|
class JsonHttpResponse extends HttpResponse {
|
||||||
/**
|
/**
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Model;
|
namespace IQBall\Core\Model;
|
||||||
|
|
||||||
use App\Data\Account;
|
use IQBall\Core\Data\Account;
|
||||||
use App\Gateway\AccountGateway;
|
use IQBall\Core\Gateway\AccountGateway;
|
||||||
use App\Validation\FieldValidationFail;
|
use IQBall\Core\Validation\FieldValidationFail;
|
||||||
use App\Validation\ValidationFail;
|
use IQBall\Core\Validation\ValidationFail;
|
||||||
|
|
||||||
class AuthModel {
|
class AuthModel {
|
||||||
private AccountGateway $gateway;
|
private AccountGateway $gateway;
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Session;
|
namespace IQBall\Core\Session;
|
||||||
|
|
||||||
use App\Data\Account;
|
use IQBall\Core\Data\Account;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The mutable side of a session handle
|
* The mutable side of a session handle
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Session;
|
namespace IQBall\Core\Session;
|
||||||
|
|
||||||
use App\Data\Account;
|
use IQBall\Core\Data\Account;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A PHP session handle
|
* A PHP session handle
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Session;
|
namespace IQBall\Core\Session;
|
||||||
|
|
||||||
use App\Data\Account;
|
use IQBall\Core\Data\Account;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An immutable session handle
|
* An immutable session handle
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
class ComposedValidator extends Validator {
|
class ComposedValidator extends Validator {
|
||||||
private Validator $first;
|
private Validator $first;
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An error that concerns a field, with a bound message name
|
* An error that concerns a field, with a bound message name
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
class FunctionValidator extends Validator {
|
class FunctionValidator extends Validator {
|
||||||
/**
|
/**
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple validator that takes a predicate and an error factory
|
* A simple validator that takes a predicate and an error factory
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for validation
|
* Utility class for validation
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
use JsonSerializable;
|
use JsonSerializable;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
abstract class Validator {
|
abstract class Validator {
|
||||||
/**
|
/**
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validation;
|
namespace IQBall\Core\Validation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A collection of standard validators
|
* A collection of standard validators
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Validator;
|
namespace IQBall\Core\Validator;
|
||||||
|
|
||||||
use App\Data\TacticInfo;
|
use IQBall\Core\Data\TacticInfo;
|
||||||
use App\Validation\ValidationFail;
|
use IQBall\Core\Validation\ValidationFail;
|
||||||
|
|
||||||
class TacticValidator {
|
class TacticValidator {
|
||||||
public static function validateAccess(?TacticInfo $tactic, int $tacticId, int $ownerId): ?ValidationFail {
|
public static function validateAccess(?TacticInfo $tactic, int $tacticId, int $ownerId): ?ValidationFail {
|
Loading…
Reference in new issue