From 6651079c21d08b6d5398267c49f998fe9491cd20 Mon Sep 17 00:00:00 2001 From: "Kouhei.Sano" Date: Mon, 29 Jul 2024 17:33:50 +0900 Subject: [PATCH] refactor: --- source/app/env.dist.json | 5 +++-- source/app/env.schema.json | 12 ++++++++---- source/app/src/Auth/AdminAuthenticator.php | 8 +++++--- source/app/src/DiBinder.php | 2 ++ source/app/src/RequestDispatcher.php | 7 ++++++- .../app/src/TemplateEngine/QiqCustomHelper.php | 8 ++++++++ source/app/var/conf/aura.route.cli.php | 4 ++-- source/app/var/conf/aura.route.web.php | 16 ++++++++++------ .../app/var/qiq/template/layout/Admin/base.php | 4 ++-- .../app/var/qiq/template/layout/Admin/page.php | 6 +++--- 10 files changed, 49 insertions(+), 23 deletions(-) diff --git a/source/app/env.dist.json b/source/app/env.dist.json index 0b0b72d..c3629d7 100644 --- a/source/app/env.dist.json +++ b/source/app/env.dist.json @@ -1,10 +1,11 @@ { "$schema": "./env.schema.json", - "BASE_URL": "http://localhost", + "ADMIN_PREFIX": "upvQzoCTaaYrTDP7", "CLOUDFLARE_TURNSTILE_SECRET_KEY": "", "CLOUDFLARE_TURNSTILE_SITE_KEY": "", "DB_DSN": "mysql:host=aura-mysql:3306;dbname=aura_db", "DB_PASS": "passw0rd", "DB_USER": "aura", - "QIQ_CACHE_PATH": null + "QIQ_CACHE_PATH": null, + "SITE_URL": "http://localhost" } diff --git a/source/app/env.schema.json b/source/app/env.schema.json index 97d5ddd..53662f6 100644 --- a/source/app/env.schema.json +++ b/source/app/env.schema.json @@ -2,8 +2,9 @@ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { - "BASE_URL": { - "type": "string" + "ADMIN_PREFIX": { + "type": "string", + "description": "CMS's URL path" }, "CLOUDFLARE_TURNSTILE_SECRET_KEY": { "type": "string", @@ -34,15 +35,18 @@ "string", "null" ] + }, + "SITE_URL": { + "type": "string" } }, "required": [ - "BASE_URL", "CLOUDFLARE_TURNSTILE_SECRET_KEY", "CLOUDFLARE_TURNSTILE_SITE_KEY", "DB_DSN", "DB_USER", "DB_PASS", - "QIQ_CACHE_PATH" + "QIQ_CACHE_PATH", + "SITE_URL" ] } diff --git a/source/app/src/Auth/AdminAuthenticator.php b/source/app/src/Auth/AdminAuthenticator.php index 5b7b9b1..0083fd8 100644 --- a/source/app/src/Auth/AdminAuthenticator.php +++ b/source/app/src/Auth/AdminAuthenticator.php @@ -12,6 +12,7 @@ use Aura\Auth\Service\LogoutService; use Aura\Auth\Service\ResumeService; use Aura\Auth\Verifier\PasswordVerifier; +use MyVendor\MyPackage\Router\RouterInterface; use PDO; use function assert; @@ -29,6 +30,7 @@ public function __construct( private readonly string $pdoDsn, private readonly string $pdoUsername, private readonly string $pdoPassword, + private readonly RouterInterface $router, ) { } @@ -152,17 +154,17 @@ public function getUserData(): array public function getAuthRedirect(): string { - return '/admin/index'; + return (string) $this->router->generate('/admin/index'); } public function getUnauthRedirect(): string { - return '/admin/login'; + return (string) $this->router->generate('/admin/login'); } public function getPasswordRedirect(): string { - return '/admin/password-confirm'; + return (string) $this->router->generate('/admin/password-confirm'); } public function getIdentity(): AdminIdentity diff --git a/source/app/src/DiBinder.php b/source/app/src/DiBinder.php index 01cab48..54abf72 100644 --- a/source/app/src/DiBinder.php +++ b/source/app/src/DiBinder.php @@ -208,6 +208,8 @@ static function () use ($file) { $router = new RouterContainer(); $map = $router->getMap(); + $adminPrefix = getenv('ADMIN_PREFIX'); + /** @psalm-suppress UnresolvableInclude */ require $file; diff --git a/source/app/src/RequestDispatcher.php b/source/app/src/RequestDispatcher.php index e56c12c..82d72b6 100644 --- a/source/app/src/RequestDispatcher.php +++ b/source/app/src/RequestDispatcher.php @@ -33,6 +33,7 @@ use Throwable; use function assert; +use function call_user_func_array; use function class_exists; use function is_callable; use function is_string; @@ -142,7 +143,11 @@ public function __invoke(): ResponseInterface|null try { // NOTE: RequestHandler で ServerRequest や Route の取得をしたい場合は "Typehinted constructor" を使う - $object = $object->$action(); + $callable = [$object, $action]; + if (is_callable($callable)) { + call_user_func_array($callable, $route->attributes); + } + if (! $object instanceof RequestHandler) { throw new InvalidResponseException('Invalid response type.'); } diff --git a/source/app/src/TemplateEngine/QiqCustomHelper.php b/source/app/src/TemplateEngine/QiqCustomHelper.php index 166625f..37c51bd 100644 --- a/source/app/src/TemplateEngine/QiqCustomHelper.php +++ b/source/app/src/TemplateEngine/QiqCustomHelper.php @@ -4,6 +4,7 @@ namespace MyVendor\MyPackage\TemplateEngine; +use MyVendor\MyPackage\Router\RouterInterface; use Qiq\Helper\Html\HtmlHelpers; use function is_string; @@ -13,6 +14,7 @@ final class QiqCustomHelper extends HtmlHelpers { public function __construct( private readonly string $cloudflareTurnstileSiteKey, + private readonly RouterInterface $router, ) { parent::__construct(); } @@ -53,4 +55,10 @@ public function cfTurnstileWidget( return sprintf('
', $this->a($attribs)); } + + /** @param array $data */ + public function generateUrl(string $name, array $data = []): string + { + return (string) $this->router->generate($name, $data); + } } diff --git a/source/app/var/conf/aura.route.cli.php b/source/app/var/conf/aura.route.cli.php index f24c54d..a83dd60 100644 --- a/source/app/var/conf/aura.route.cli.php +++ b/source/app/var/conf/aura.route.cli.php @@ -7,7 +7,7 @@ /* @var Map $map */ -$map->attach('cli:', null, function (Map $map) { - $map->get('hello', '/hello', Handler\Hello::class,) +$map->attach('/cli', null, function (Map $map) { + $map->get('/hello', '/hello', Handler\Hello::class,) ->extras(['a' => 'b']); }); diff --git a/source/app/var/conf/aura.route.web.php b/source/app/var/conf/aura.route.web.php index 41cb13e..0474775 100644 --- a/source/app/var/conf/aura.route.web.php +++ b/source/app/var/conf/aura.route.web.php @@ -8,6 +8,10 @@ /* @var Map $map */ +if (empty($adminPrefix)) { + $adminPrefix = 'admin'; +} + $map->attach(null, null, function (Map $map) { $map->accepts(['application/json', 'text/html']); @@ -15,20 +19,20 @@ ->extras(['a' => 'b']); }); -$map->attach('admin:', '/admin', function (Map $map) { +$map->attach('/admin', '/' . $adminPrefix, function (Map $map) { $auth = ['admin' => true]; $map->auth($auth); - $map->get('login', '/login', AdminHandler\Login::class) + $map->get('/login', '/login', AdminHandler\Login::class) ->auth([]); - $map->post('_login', '/login', AdminHandler\Login::class) + $map->post('/_login', '/login', AdminHandler\Login::class) ->auth(['login' => true]); - $map->post('logout', '/logout', AdminHandler\Logout::class) + $map->post('/logout', '/logout', AdminHandler\Logout::class) ->auth(array_merge($auth, ['logout' => true])); - $map->get('index', '/index', AdminHandler\Index::class); + $map->get('/index', '/index', AdminHandler\Index::class); - $map->get('hello', '/hello', AdminHandler\Hello::class); + $map->get('/hello', '/hello', AdminHandler\Hello::class); }); diff --git a/source/app/var/qiq/template/layout/Admin/base.php b/source/app/var/qiq/template/layout/Admin/base.php index 4dff5e4..fe59a07 100644 --- a/source/app/var/qiq/template/layout/Admin/base.php +++ b/source/app/var/qiq/template/layout/Admin/base.php @@ -7,10 +7,10 @@ - + {{ setBlock ('head_meta') }}{{= getBlock () ~}} {{ setBlock ('head_styles') }} - + {{= getBlock () ~}} {{ setBlock ('head_scripts') }} diff --git a/source/app/var/qiq/template/layout/Admin/page.php b/source/app/var/qiq/template/layout/Admin/page.php index 25f718e..1f9d84c 100644 --- a/source/app/var/qiq/template/layout/Admin/page.php +++ b/source/app/var/qiq/template/layout/Admin/page.php @@ -6,12 +6,12 @@
- + ADMINISTRATOR