Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
apple-x-co committed Jul 29, 2024
1 parent 0b82619 commit 6651079
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 23 deletions.
5 changes: 3 additions & 2 deletions source/app/env.dist.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 8 additions & 4 deletions source/app/env.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
]
}
8 changes: 5 additions & 3 deletions source/app/src/Auth/AdminAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ public function __construct(
private readonly string $pdoDsn,
private readonly string $pdoUsername,
private readonly string $pdoPassword,
private readonly RouterInterface $router,
) {
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions source/app/src/DiBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ static function () use ($file) {
$router = new RouterContainer();
$map = $router->getMap();

$adminPrefix = getenv('ADMIN_PREFIX');

/** @psalm-suppress UnresolvableInclude */
require $file;

Expand Down
7 changes: 6 additions & 1 deletion source/app/src/RequestDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
}
Expand Down
8 changes: 8 additions & 0 deletions source/app/src/TemplateEngine/QiqCustomHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MyVendor\MyPackage\TemplateEngine;

use MyVendor\MyPackage\Router\RouterInterface;
use Qiq\Helper\Html\HtmlHelpers;

use function is_string;
Expand All @@ -13,6 +14,7 @@ final class QiqCustomHelper extends HtmlHelpers
{
public function __construct(
private readonly string $cloudflareTurnstileSiteKey,
private readonly RouterInterface $router,
) {
parent::__construct();
}
Expand Down Expand Up @@ -53,4 +55,10 @@ public function cfTurnstileWidget(

return sprintf('<div %s></div>', $this->a($attribs));
}

/** @param array<string, string|int> $data */
public function generateUrl(string $name, array $data = []): string
{
return (string) $this->router->generate($name, $data);
}
}
4 changes: 2 additions & 2 deletions source/app/var/conf/aura.route.cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
16 changes: 10 additions & 6 deletions source/app/var/conf/aura.route.web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@

/* @var Map $map */

if (empty($adminPrefix)) {
$adminPrefix = 'admin';
}

$map->attach(null, null, function (Map $map) {
$map->accepts(['application/json', 'text/html']);

$map->get('hello', '/hello', Handler\Hello::class)
->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);
});
4 changes: 2 additions & 2 deletions source/app/var/qiq/template/layout/Admin/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="{{= $baseUrl }}/admin/images/favicon.svg" type="image/svg+xml">
<link rel="icon" href="{{= $siteUrl }}/admin/images/favicon.svg" type="image/svg+xml">
{{ setBlock ('head_meta') }}{{= getBlock () ~}}
{{ setBlock ('head_styles') }}
<link href="{{= $baseUrl }}/admin/css/bundle.css" rel="stylesheet">
<link href="{{= $siteUrl }}/admin/css/bundle.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP:100|Noto+Sans+JP:400|Noto+Sans+JP:700|Noto+Sans+JP:900|Roboto:100|Roboto:400|Roboto:700|Roboto:900&display=swap&subset=japanese" rel="stylesheet">
{{= getBlock () ~}}
{{ setBlock ('head_scripts') }}
Expand Down
6 changes: 3 additions & 3 deletions source/app/var/qiq/template/layout/Admin/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<div class="max-w-7xl mx-auto">
<div class="py-4 border-b border-slate-900/10 px-4 lg:px-8 lg:border-0">
<div class="relative flex items-center">
<a class="mr-3 flex-none overflow-hidden lg:w-auto font-sans font-black text-lg bg-clip-text text-transparent bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500" href="/admin/index">
<a class="mr-3 flex-none overflow-hidden lg:w-auto font-sans font-black text-lg bg-clip-text text-transparent bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500" href="{{a generateUrl('/admin/index') }}">
ADMINISTRATOR
</a>
<div class="relative hidden lg:flex items-center ml-auto">
<div class="flex items-center border-l border-slate-200 ml-6 pl-6 dark:border-slate-800">
<form method="post" action="/admin/logout">
<form method="post" action="{{a generateUrl('/admin/logout') }}">
<button>
{{= render('partials/Admin/InlineIcon', ['name' => 'arrow-left-on-rectangle']) }}
</button>
Expand All @@ -31,7 +31,7 @@
{{ setBlock ('body_nav') }}
{{ $viewName = $this->getView() }}
<p>
<a class="text-sm font-bold tracking-wider {{ if ($view == 'Page/Admin/Index'): }}text-lime-500{{ endif }}" href="/admin/index">HOME</a>
<a class="text-sm font-bold tracking-wider {{ if ($viewName == 'Admin/Index'): }}text-lime-500{{ endif }}" href="{{a generateUrl('/admin/index') }}">HOME</a>
</p>
{{= getBlock () ~}}
</nav>
Expand Down

0 comments on commit 6651079

Please sign in to comment.