diff --git a/ci/build_react.msh b/ci/build_react.msh index 64a0cb6..32a5923 100755 --- a/ci/build_react.msh +++ b/ci/build_react.msh @@ -30,6 +30,5 @@ echo "];" >> views-mappings.php chmod +r views-mappings.php -// moshell does not supports file patterns -bash <<< "mv dist/* public/* front/assets/ front/style/ /outputs/public/" +mv dist/* front/assets/ front/style/ public/* /outputs/public/ mv views-mappings.php /outputs/ diff --git a/public/api/index.php b/public/api/index.php index 6f46638..fd39bfa 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -27,7 +27,7 @@ function getAuthController(): APIAuthController { function getRoutes(): AltoRouter { $router = new AltoRouter(); - $router->setBasePath(get_public_path() . "/api"); + $router->setBasePath(get_public_path(__DIR__)); $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())); diff --git a/public/assets b/public/assets new file mode 120000 index 0000000..7b299d9 --- /dev/null +++ b/public/assets @@ -0,0 +1 @@ +../front/assets \ No newline at end of file diff --git a/public/index.php b/public/index.php index 01ba967..c31e289 100644 --- a/public/index.php +++ b/public/index.php @@ -29,6 +29,9 @@ use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\TacticModel; use IQBall\Core\Model\TeamModel; use IQBall\Core\Validation\ValidationFail; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; +use Twig\TwigFunction; function getConnection(): Connection { return new Connection(get_database()); @@ -55,6 +58,16 @@ function getAuthController(): AuthController { return new AuthController(new AuthModel(new AccountGateway(getConnection()))); } +function getTwig(): Environment { + global $basePath; + $fl = new FilesystemLoader("../src/App/Views"); + $twig = new Environment($fl); + + $twig->addFunction(new TwigFunction('path', fn(string $str) => "$basePath$str")); + + return $twig; +} + function getRoutes(): AltoRouter { global $basePath; @@ -104,6 +117,6 @@ function runMatch($match, MutableSessionHandle $session): HttpResponse { //this is a global variable -$basePath = get_public_path(); +$basePath = get_public_path(__DIR__); -App::render(runMatch(getRoutes()->match(), PhpSessionHandle::init()), "../src/App/Views/"); +App::render(runMatch(getRoutes()->match(), PhpSessionHandle::init()), fn() => getTwig()); diff --git a/src/Api/API.php b/src/Api/API.php index e96b6b6..f79e1b5 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -19,8 +19,8 @@ class API { if ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); - } else { - throw new Exception("API returned a non-json response."); + } else if (get_class($response) != HttpResponse::class) { + throw new Exception("API returned unknown Http Response"); } } diff --git a/src/Api/Controller/APIAuthController.php b/src/Api/Controller/APIAuthController.php index 5e149a5..fc0eef6 100644 --- a/src/Api/Controller/APIAuthController.php +++ b/src/Api/Controller/APIAuthController.php @@ -2,8 +2,8 @@ namespace IQBall\Api\Controller; +use IQBall\App\Control; use IQBall\Core\Http\HttpCodes; -use IQBall\Core\Route\Control; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index 04f0a50..e8a1731 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -2,7 +2,7 @@ namespace IQBall\Api\Controller; -use IQBall\Core\Route\Control; +use IQBall\App\Control; use IQBall\Core\Data\Account; use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpRequest; diff --git a/src/App/App.php b/src/App/App.php index de288e8..cd3c293 100644 --- a/src/App/App.php +++ b/src/App/App.php @@ -16,13 +16,13 @@ class App { /** * renders (prints out) given HttpResponse to the client * @param HttpResponse $response - * @param string $twigViewsFolder + * @param callable(): Environment $twigSupplier * @return void * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ - public static function render(HttpResponse $response, string $twigViewsFolder): void { + public static function render(HttpResponse $response, callable $twigSupplier): void { http_response_code($response->getCode()); foreach ($response->getHeaders() as $header => $value) { @@ -30,7 +30,7 @@ class App { } if ($response instanceof ViewHttpResponse) { - self::renderView($response, $twigViewsFolder); + self::renderView($response, $twigSupplier); } elseif ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); @@ -40,13 +40,13 @@ class App { /** * renders (prints out) given ViewHttpResponse to the client * @param ViewHttpResponse $response - * @param string $twigViewsFolder + * @param callable(): Environment $twigSupplier * @return void * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ - private static function renderView(ViewHttpResponse $response, string $twigViewsFolder): void { + private static function renderView(ViewHttpResponse $response, callable $twigSupplier): void { $file = $response->getFile(); $args = $response->getArguments(); @@ -56,10 +56,9 @@ class App { break; case ViewHttpResponse::TWIG_VIEW: try { - $fl = new FilesystemLoader($twigViewsFolder); - $twig = new Environment($fl); + $twig = call_user_func($twigSupplier); $twig->display($file, $args); - } catch (RuntimeError | SyntaxError | LoaderError $e) { + } 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; diff --git a/src/App/Control.php b/src/App/Control.php index f3860ec..5c2fe0f 100644 --- a/src/App/Control.php +++ b/src/App/Control.php @@ -1,12 +1,10 @@ -
- - - -