perf(core): 대용량 목록 상한 총 건수·커서 계약 신설 및 요청당 반복 실행 비용 정리
공개 이슈 gnuboard/g7 이 지목한 병목은 "같은 일을 반복 실행한다" 축이다. 응답·행의 무게를 다룬 · 과 겹치지 않는다. 공통 계약 — 총 건수 상한과 페이지 이동 범위는 별개 결정이다. 묶으면 필요 없이 기능이 깎인다. 총 건수만 상한을 받고(파생 테이블 COUNT), "다음" 이동은 per_page + 1 실측으로 끝까지 열어 둔다. 계산이 불가능해지는 것은 마지막 페이지 번호 하나뿐이며 그 사실은 last_page: null 이 알린다. 최신순처럼 실제 컬럼으로 정렬하는 목록은 커서로 전환해 깊이와 무관하게 일정 속도로 이동한다. 관련도순은 계산값 정렬이라 커서 키로 쓸 수 없어 offset 을 유지한다. 계약의 입구는 표준 paginate 와 같은 폭이어야 한다. 관계·쿼리 빌더를 받지 못하면 그 좁은 만큼이 그대로 운영 500 이 되고, 실제로 관리자 알림 목록에서 그렇게 터졌다. 응답 조립도 컬렉션마다 손으로 하면 형태가 늘어나는 순간 없는 값을 부르거나 새 필드를 흘리므로, 형태 판정을 paginationMeta 한 곳에 모았다. 표준 paginate 응답은 필드 단위로 이전과 동일하다. 요청당 반복 비용 — 훅 구독마다 남기던 로그 400줄, 요청당 스무 번 넘던 설정 파일 재읽기, 이미 캐시된 목록의 DB 재조회를 없앴다. 권한 판정은 요청 스코프 메모를 두어 화면 요소마다 나가던 조회를 한 번으로 줄였고, 크로스 요청 캐시는 두지 않아 권한 변경이 종전처럼 다음 요청에 반영된다. 검색 질의는 활성 엔진이 만든다. 저장소가 구체 엔진을 지목하면 플러그인이 등록한 엔진은 호출될 기회 자체를 잃고 오류 없이 다른 방식으로 동작한다. 해석기를 두어 활성 엔진에 위임하고, 전문검색이 없는 DBMS 의 부분일치 폴백도 드라이버명 하드코딩 대신 선언형 config 로 옮겼다. 총 건수가 잘린 목록에서 순번을 역산하면 0 과 음수가 나온다. 지어내지 않고 null 을 돌려주며, 그 원칙을 last_page 와 동일하게 적용했다.
This commit is contained in:
1 parent
97ab99b7cd
commit
2ae1972a3b
101 files changed
+6160
-322
No files matched your search
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts\Pagination;
|
||||
|
||||
use App\Enums\TotalRelation;
|
||||
|
||||
/**
|
||||
* 총 건수 정확도를 스스로 밝히는 페이지 결과 계약
|
||||
*
|
||||
* 표준 `LengthAwarePaginator` 는 `total()` 이 항상 정확하다고 가정한다. 대용량 목록에서
|
||||
* 상한을 걸고 센 결과는 그 가정을 만족하지 못하므로, 정확도를 함께 실어 나른다.
|
||||
*
|
||||
* 이 계약을 구현한 페이지는 `BaseApiCollection::paginationMeta()` 가 자동으로 알아보고
|
||||
* `total_relation` / `total_is_exact` / `result_cap` 메타를 응답에 덧붙인다. 컬렉션 쪽
|
||||
* 코드 변경은 필요 없다.
|
||||
*/
|
||||
interface BoundedTotalAware
|
||||
{
|
||||
/**
|
||||
* 총 건수와 실제 매칭 건수의 관계를 반환합니다.
|
||||
*
|
||||
* @return TotalRelation 정확(Exact) 또는 하한(AtLeast)
|
||||
*/
|
||||
public function totalRelation(): TotalRelation;
|
||||
|
||||
/**
|
||||
* 총 건수 집계에 적용된 상한을 반환합니다.
|
||||
*
|
||||
* @return int|null 상한 (무제한이면 null)
|
||||
*/
|
||||
public function resultCap(): ?int;
|
||||
|
||||
/**
|
||||
* 총 건수가 상한에 걸려 잘렸는지 여부를 반환합니다.
|
||||
*
|
||||
* @return bool 잘렸으면 true (= totalRelation 이 AtLeast)
|
||||
*/
|
||||
public function isTruncated(): bool;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Contracts\Repositories;
|
||||
|
||||
use Illuminate\Contracts\Pagination\CursorPaginator;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -18,20 +19,22 @@ interface ActivityLogRepositoryInterface
|
||||
* @param array $filters 필터 조건
|
||||
* @return LengthAwarePaginator 페이지네이션된 로그 목록
|
||||
*/
|
||||
|
||||
public function getPaginatedForModel(Model $model, array $filters = []): LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* 활동 로그 목록을 페이지네이션하여 조회합니다.
|
||||
*
|
||||
* 요청에 `cursor` 가 있으면 키셋(커서) 방식으로 응답합니다.
|
||||
*
|
||||
* @param array $filters 필터 조건
|
||||
* @return LengthAwarePaginator 페이지네이션된 로그 목록
|
||||
* @return LengthAwarePaginator|CursorPaginator 페이지네이션된 로그 목록
|
||||
*/
|
||||
public function getPaginated(array $filters = []): LengthAwarePaginator;
|
||||
public function getPaginated(array $filters = []): LengthAwarePaginator|CursorPaginator;
|
||||
|
||||
/**
|
||||
* 활동 로그를 삭제합니다.
|
||||
*
|
||||
* @param int $id 삭제할 활동 로그 ID
|
||||
* @param int $id 삭제할 활동 로그 ID
|
||||
* @return bool 삭제 성공 여부
|
||||
*/
|
||||
public function delete(int $id): bool;
|
||||
@@ -39,7 +42,7 @@ interface ActivityLogRepositoryInterface
|
||||
/**
|
||||
* 여러 활동 로그를 일괄 삭제합니다.
|
||||
*
|
||||
* @param array<int> $ids 삭제할 활동 로그 ID 목록
|
||||
* @param array<int> $ids 삭제할 활동 로그 ID 목록
|
||||
* @return int 삭제된 건수
|
||||
*/
|
||||
public function deleteMany(array $ids): int;
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Contracts\Repositories;
|
||||
use App\Models\NotificationLog;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Pagination\CursorPaginator;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface NotificationLogRepositoryInterface
|
||||
@@ -44,5 +45,5 @@ interface NotificationLogRepositoryInterface
|
||||
*
|
||||
* @param User|null $scopeUser 스코프 적용 대상 사용자 (null이면 스코프 미적용)
|
||||
*/
|
||||
public function getPaginated(array $filters = [], int $perPage = 20, ?User $scopeUser = null): LengthAwarePaginator;
|
||||
public function getPaginated(array $filters = [], int $perPage = 20, ?User $scopeUser = null): LengthAwarePaginator|CursorPaginator;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
/**
|
||||
* 목록 총 건수(total)와 실제 매칭 건수의 관계
|
||||
*
|
||||
* 대용량 목록에서 `COUNT(*)` 전량 집계는 비용이 커서 상한을 걸고 센다.
|
||||
* 상한 이하면 센 값이 곧 실제 건수이고, 상한을 넘으면 "그 이상"이라는 사실만 알 수 있다.
|
||||
* 화면은 이 값을 보고 "1,234건" 과 "10,000건 이상" 을 구분해 표기한다.
|
||||
*/
|
||||
enum TotalRelation: string
|
||||
{
|
||||
/** 정확 — total 이 실제 매칭 건수와 같다 */
|
||||
case Exact = 'exact';
|
||||
|
||||
/** 하한 — 실제 매칭 건수가 total 이상이다 (상한 초과로 정확히 세지 않음) */
|
||||
case AtLeast = 'at_least';
|
||||
|
||||
/**
|
||||
* 모든 값을 문자열 배열로 반환합니다.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(self::cases(), 'value');
|
||||
}
|
||||
|
||||
/**
|
||||
* 유효한 값인지 확인합니다.
|
||||
*
|
||||
* @param string $value 검증할 값
|
||||
* @return bool 유효 여부
|
||||
*/
|
||||
public static function isValid(string $value): bool
|
||||
{
|
||||
return in_array($value, self::values(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수가 정확한지 여부를 반환합니다.
|
||||
*
|
||||
* @return bool 정확하면 true
|
||||
*/
|
||||
public function isExact(): bool
|
||||
{
|
||||
return $this === self::Exact;
|
||||
}
|
||||
|
||||
/**
|
||||
* 다국어 라벨을 반환합니다.
|
||||
*
|
||||
* @return string lang 키 해석 결과
|
||||
*/
|
||||
public function label(): string
|
||||
{
|
||||
return __('pagination.total_relation.'.$this->value);
|
||||
}
|
||||
}
|
||||
@@ -124,15 +124,10 @@ class HookListenerRegistrar
|
||||
self::addQueuedAction($hookName, $listenerClass, $method, $priority);
|
||||
}
|
||||
|
||||
Log::info('훅 리스너 등록 완료', [
|
||||
'hook' => $hookName,
|
||||
'listener' => $listenerClass,
|
||||
'method' => $method,
|
||||
'priority' => $priority,
|
||||
'type' => $type,
|
||||
'sync' => $forceSync,
|
||||
'source' => $source,
|
||||
]);
|
||||
// 등록 성공은 로그로 남기지 않는다. 리스너 112개 × 구독 400건이 요청마다 부팅되므로
|
||||
// 한 줄씩만 남겨도 매 요청 400줄이 쌓인다. 기본 설치가 production + LOG_LEVEL 조합상
|
||||
// 이 줄들을 그대로 기록하므로, 정상 동작을 알리는 데 드는 비용이 동작 자체보다 커진다.
|
||||
// 등록 실패는 아래 catch 에서 계속 기록한다.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Base;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
@@ -21,7 +22,7 @@ abstract class AuthBaseController extends BaseApiController
|
||||
/**
|
||||
* 사용자 소유권을 확인합니다.
|
||||
*
|
||||
* @param int $userId 확인할 사용자 ID
|
||||
* @param int $userId 확인할 사용자 ID
|
||||
* @return bool
|
||||
*/
|
||||
protected function isOwner(int $userId): bool
|
||||
@@ -32,7 +33,7 @@ abstract class AuthBaseController extends BaseApiController
|
||||
/**
|
||||
* 사용자가 특정 리소스에 접근할 수 있는지 확인합니다.
|
||||
*
|
||||
* @param mixed $resource 접근하려는 리소스
|
||||
* @param mixed $resource 접근하려는 리소스
|
||||
* @return bool
|
||||
*/
|
||||
protected function canAccessResource($resource): bool
|
||||
@@ -48,8 +49,8 @@ abstract class AuthBaseController extends BaseApiController
|
||||
/**
|
||||
* 사용자 활동을 기록합니다.
|
||||
*
|
||||
* @param string $action 수행한 작업
|
||||
* @param array $data 관련 데이터
|
||||
* @param string $action 수행한 작업
|
||||
* @param array $data 관련 데이터
|
||||
* @return void
|
||||
*/
|
||||
protected function logUserActivity(string $action, array $data = []): void
|
||||
@@ -58,20 +59,20 @@ abstract class AuthBaseController extends BaseApiController
|
||||
Log::info("User Activity: {$action}", [
|
||||
'user_id' => $this->getCurrentUser()?->uuid,
|
||||
'data' => $data,
|
||||
'timestamp' => now()
|
||||
'timestamp' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 리소스 소유권을 확인하고, 소유자가 아니면 Forbidden 응답을 반환합니다.
|
||||
*
|
||||
* @param mixed $resource 확인할 리소스
|
||||
* @param string $messageKey 오류 메시지 키
|
||||
* @return \Illuminate\Http\JsonResponse|null 소유자이면 null, 아니면 Forbidden 응답
|
||||
* @param mixed $resource 확인할 리소스
|
||||
* @param string $messageKey 오류 메시지 키
|
||||
* @return JsonResponse|null 소유자이면 null, 아니면 Forbidden 응답
|
||||
*/
|
||||
protected function checkOwnership($resource, string $messageKey = 'common.forbidden')
|
||||
{
|
||||
if (!$this->canAccessResource($resource)) {
|
||||
if (! $this->canAccessResource($resource)) {
|
||||
return $this->forbidden($messageKey);
|
||||
}
|
||||
|
||||
@@ -81,14 +82,21 @@ abstract class AuthBaseController extends BaseApiController
|
||||
/**
|
||||
* API 사용량을 기록합니다.
|
||||
*
|
||||
* @param string $endpoint 엔드포인트
|
||||
* @param array $data 관련 데이터
|
||||
* @param string $endpoint 엔드포인트
|
||||
* @param array $data 관련 데이터
|
||||
* @return void
|
||||
*/
|
||||
protected function logApiUsage(string $endpoint, array $data = []): void
|
||||
{
|
||||
// TODO: API 사용량 통계 시스템 구현
|
||||
Log::info("Auth API Usage: {$endpoint}", [
|
||||
//
|
||||
// PublicBaseController 와 같은 이유로 디버그 모드에서만 기록한다.
|
||||
// 통계 시스템이 서기 전까지 요청마다 로그 파일에 줄을 쌓지 않는다.
|
||||
if (! config('app.debug')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log::debug("Auth API Usage: {$endpoint}", [
|
||||
'user_id' => $this->getCurrentUser()?->uuid,
|
||||
'ip' => request()->ip(),
|
||||
'user_agent' => request()->userAgent(),
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
namespace App\Http\Controllers\Api\Base;
|
||||
|
||||
use App\Contracts\Extension\CacheInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
/**
|
||||
* 공개 API용 베이스 컨트롤러
|
||||
@@ -27,9 +25,9 @@ abstract class PublicBaseController extends BaseApiController
|
||||
* 이는 확장 설치 직후·활성화 전 등의 일시적 상태에서 얻은 "not found" 같은
|
||||
* 에러 응답이 영구 캐시되어 복구 후에도 잘못된 응답을 반환하는 문제를 방지한다.
|
||||
*
|
||||
* @param string $key 캐시 키
|
||||
* @param callable $callback 데이터 생성 콜백
|
||||
* @param int $ttl 캐시 유지 시간 (초)
|
||||
* @param string $key 캐시 키
|
||||
* @param callable $callback 데이터 생성 콜백
|
||||
* @param int $ttl 캐시 유지 시간 (초)
|
||||
* @return mixed
|
||||
*/
|
||||
protected function cached(string $key, callable $callback, int $ttl = 3600)
|
||||
@@ -54,18 +52,27 @@ abstract class PublicBaseController extends BaseApiController
|
||||
/**
|
||||
* API 사용량을 기록합니다.
|
||||
*
|
||||
* @param string $endpoint 엔드포인트
|
||||
* @param array $data 관련 데이터
|
||||
* @param string $endpoint 엔드포인트
|
||||
* @param array $data 관련 데이터
|
||||
* @return void
|
||||
*/
|
||||
protected function logApiUsage(string $endpoint, array $data = []): void
|
||||
{
|
||||
// TODO: API 사용량 통계 시스템 구현
|
||||
Log::info("Public API Usage: {$endpoint}", [
|
||||
//
|
||||
// 호출 지점은 레이아웃·확장 번들·정적 자산처럼 페이지 로드마다 여러 번 열리는
|
||||
// 공개 엔드포인트다. 통계 시스템이 서기 전까지 이 자리가 요청마다 로그 파일에
|
||||
// 줄을 쌓지 않도록, 디버그 모드에서만 기록한다. 기본 설치는 APP_DEBUG=false 이므로
|
||||
// 아무것도 쓰지 않는다.
|
||||
if (! config('app.debug')) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log::debug("Public API Usage: {$endpoint}", [
|
||||
'ip' => request()->ip(),
|
||||
'user_agent' => request()->userAgent(),
|
||||
'data' => $data,
|
||||
'timestamp' => now()
|
||||
'timestamp' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -80,8 +87,7 @@ abstract class PublicBaseController extends BaseApiController
|
||||
'ip' => request()->ip(),
|
||||
'user_agent' => request()->userAgent(),
|
||||
'referer' => request()->header('referer'),
|
||||
'timestamp' => now()
|
||||
'timestamp' => now(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Public;
|
||||
|
||||
use App\Enums\TotalRelation;
|
||||
use App\Extension\HookManager;
|
||||
use App\Http\Controllers\Api\Base\PublicBaseController;
|
||||
use App\Http\Requests\Public\SearchRequest;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
@@ -18,7 +20,7 @@ class PublicSearchController extends PublicBaseController
|
||||
/**
|
||||
* 통합 검색 수행
|
||||
*
|
||||
* @param SearchRequest $request HTTP 요청
|
||||
* @param SearchRequest $request HTTP 요청
|
||||
* @return JsonResponse 검색 결과
|
||||
*/
|
||||
public function search(SearchRequest $request): JsonResponse
|
||||
@@ -36,6 +38,14 @@ class PublicSearchController extends PublicBaseController
|
||||
return $this->success(__('search.empty_keyword'), $this->buildEmptyResponse());
|
||||
}
|
||||
|
||||
// 특정 탭을 보는 중이라면, 다른 탭의 배지 건수를 아예 세지 않을 수 있다.
|
||||
// 기본값은 세는 쪽이다 — 탭 배지가 화면 요소라 끄면 숫자가 사라진다. 다만 배지가
|
||||
// 필요 없는 화면(더 보기 페이지 등)은 with_counts=false 로 그 비용을 없앤다.
|
||||
// 전체 탭은 배지가 화면의 주 정보라 이 값과 무관하게 항상 센다.
|
||||
$includeInactiveCounts = $type === 'all'
|
||||
? true
|
||||
: $request->boolean('with_counts', true);
|
||||
|
||||
// 검색 컨텍스트 구성 (모듈별 파라미터는 request 전체를 전달하여 각 모듈이 직접 처리)
|
||||
$context = [
|
||||
'q' => $q,
|
||||
@@ -44,6 +54,10 @@ class PublicSearchController extends PublicBaseController
|
||||
'page' => $page,
|
||||
'per_page' => $perPage,
|
||||
'all_tab_limit' => 5,
|
||||
// 커서는 특정 탭을 볼 때만 의미가 있다. 전체 탭은 카테고리마다 몇 건씩만
|
||||
// 보여 주므로 깊은 페이지 자체가 없고, 카테고리별 커서를 하나로 합칠 수도 없다.
|
||||
'cursor' => $type === 'all' ? null : ($validated['cursor'] ?? null),
|
||||
'include_inactive_counts' => $includeInactiveCounts,
|
||||
'user' => $request->user(),
|
||||
'request' => $request,
|
||||
];
|
||||
@@ -56,8 +70,14 @@ class PublicSearchController extends PublicBaseController
|
||||
// 프론트엔드용 응답 구조로 변환 (훅을 통해 모듈이 처리)
|
||||
$response = $this->buildResponse($q, $results, $context);
|
||||
|
||||
// 총 건수가 상한에 걸렸으면 "N건" 이 아니라 "N건 이상" 으로 알린다.
|
||||
// 세지 않은 값을 정확한 것처럼 말하지 않는다.
|
||||
$messageKey = ($response['total_is_exact'] ?? true)
|
||||
? 'search.results_found'
|
||||
: 'search.results_found_at_least';
|
||||
|
||||
return $this->success(
|
||||
__('search.results_found', ['count' => number_format($response['total'])]),
|
||||
__($messageKey, ['count' => number_format($response['total'])]),
|
||||
$response
|
||||
);
|
||||
}
|
||||
@@ -72,6 +92,9 @@ class PublicSearchController extends PublicBaseController
|
||||
return [
|
||||
'q' => '',
|
||||
'total' => 0,
|
||||
'total_relation' => TotalRelation::Exact->value,
|
||||
'total_is_exact' => true,
|
||||
'result_cap' => null,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -81,9 +104,9 @@ class PublicSearchController extends PublicBaseController
|
||||
* 코어는 기본 응답 구조만 생성하고,
|
||||
* 각 모듈이 core.search.build_response 훅을 통해 자신의 카테고리 응답을 구성합니다.
|
||||
*
|
||||
* @param string $q 검색어
|
||||
* @param array $results Hook에서 반환된 검색 결과
|
||||
* @param array $context 검색 컨텍스트
|
||||
* @param string $q 검색어
|
||||
* @param array $results Hook에서 반환된 검색 결과
|
||||
* @param array $context 검색 컨텍스트
|
||||
* @return array 프론트엔드용 응답 구조
|
||||
*/
|
||||
private function buildResponse(string $q, array $results, array $context): array
|
||||
@@ -97,8 +120,15 @@ class PublicSearchController extends PublicBaseController
|
||||
// 모듈은 $results에서 자신의 데이터를 가져와 $response에 추가
|
||||
$response = HookManager::applyFilters('core.search.build_response', $response, $results, $context);
|
||||
|
||||
// 전체 합계를 항상 보존 (탭 UI에서 전체 건수 표시용)
|
||||
// 전체 합계를 항상 보존 (탭 UI에서 전체 건수 표시용).
|
||||
// "전체" 탭 배지도 잘린 합계인지 알아야 하므로 정확도를 함께 붙인다.
|
||||
$response['all_count'] = $response['total'];
|
||||
$response['all_count_is_exact'] = $this->resolveTotalAccuracy($results)['total_is_exact'];
|
||||
|
||||
// 탭 배지는 카테고리마다 하나씩 그려지므로 정확도도 카테고리마다 필요하다.
|
||||
// 모듈이 각자 자기 키를 내보내면 어떤 배지는 정확도를 받고 어떤 배지는 못 받아,
|
||||
// 상한에 걸린 숫자가 그 배지에서만 정확한 값처럼 보인다. 코어가 일괄로 붙인다.
|
||||
$response['counts_are_exact'] = $this->resolveCategoryAccuracy($results);
|
||||
|
||||
// 특정 탭 조회 시 total을 해당 탭의 count로 설정
|
||||
$type = $context['type'] ?? 'all';
|
||||
@@ -109,13 +139,14 @@ class PublicSearchController extends PublicBaseController
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
// 정확도 메타 — 전체 탭은 카테고리 합집합, 특정 탭은 그 카테고리 하나만 본다.
|
||||
return array_merge($response, $this->resolveTotalAccuracy($results, $type));
|
||||
}
|
||||
|
||||
/**
|
||||
* 전체 검색 결과 수 계산
|
||||
*
|
||||
* @param array $results Hook에서 반환된 검색 결과
|
||||
* @param array $results Hook에서 반환된 검색 결과
|
||||
* @return int 전체 결과 수
|
||||
*/
|
||||
private function calculateTotal(array $results): int
|
||||
@@ -127,4 +158,60 @@ class PublicSearchController extends PublicBaseController
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* 카테고리별 총 건수 정확도를 모읍니다.
|
||||
*
|
||||
* 탭 배지는 카테고리 수만큼 그려지므로 정확도도 그 수만큼 있어야 합니다. 하나라도
|
||||
* 빠지면 그 배지에서만 상한에 걸린 숫자가 정확한 값처럼 보이고, 오류로는 드러나지
|
||||
* 않은 채 그냥 틀린 숫자로 남습니다.
|
||||
*
|
||||
* @param array $results Hook에서 반환된 검색 결과
|
||||
* @return array<string, bool> 카테고리 => 정확 여부
|
||||
*/
|
||||
private function resolveCategoryAccuracy(array $results): array
|
||||
{
|
||||
$accuracy = [];
|
||||
|
||||
foreach ($results as $category => $categoryData) {
|
||||
$accuracy[$category] = ($categoryData['total_is_exact'] ?? true) === true;
|
||||
}
|
||||
|
||||
return $accuracy;
|
||||
}
|
||||
|
||||
/**
|
||||
* 응답 전체의 총 건수 정확도를 정합니다.
|
||||
*
|
||||
* 전체 탭에서는 카테고리 중 하나라도 상한에 걸리면 합계도 "이상" 이다 — 정확한
|
||||
* 카테고리 몇 개를 더해 봐야 전체가 정확해지지 않는다.
|
||||
*
|
||||
* 특정 탭을 보는 중이면 `total` 이 그 카테고리 건수로 바뀌므로 정확도도 그 카테고리
|
||||
* 하나만 본다. 다른 카테고리가 잘렸다는 이유로 정확한 값을 "이상" 이라 말하지 않는다.
|
||||
*
|
||||
* @param array $results Hook에서 반환된 검색 결과
|
||||
* @param string $type 조회 탭 (all 이면 전체 합계)
|
||||
* @return array{total_relation: string, total_is_exact: bool, result_cap: int|null} 정확도 메타
|
||||
*/
|
||||
private function resolveTotalAccuracy(array $results, string $type = 'all'): array
|
||||
{
|
||||
$scoped = ($type !== 'all' && array_key_exists($type, $results))
|
||||
? [$results[$type]]
|
||||
: $results;
|
||||
|
||||
$isExact = true;
|
||||
|
||||
foreach ($scoped as $categoryData) {
|
||||
if (($categoryData['total_is_exact'] ?? true) === false) {
|
||||
$isExact = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'total_relation' => $isExact ? TotalRelation::Exact->value : TotalRelation::AtLeast->value,
|
||||
'total_is_exact' => $isExact,
|
||||
'result_cap' => PaginationLimits::resultCap('search'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use App\Enums\PermissionType;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Helpers\ResponseHelper;
|
||||
use App\Models\Role;
|
||||
use App\Support\GuestRoleResolver;
|
||||
use Closure;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -14,13 +15,6 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class PermissionMiddleware
|
||||
{
|
||||
/**
|
||||
* guest role 캐시
|
||||
*
|
||||
* @var Role|null
|
||||
*/
|
||||
protected static $guestRoleCache = null;
|
||||
|
||||
/**
|
||||
* 특정 권한을 가진 사용자 또는 비회원(guest)의 접근을 허용합니다.
|
||||
*
|
||||
@@ -125,10 +119,9 @@ class PermissionMiddleware
|
||||
return false;
|
||||
}
|
||||
|
||||
return $guestRole->permissions()
|
||||
->where('identifier', $permission)
|
||||
->where('type', $type)
|
||||
->exists();
|
||||
// 적재해 둔 권한 컬렉션에서 판정한다. permissions() 로 빌더를 다시 열면
|
||||
// 캐시해 둔 의미가 사라져 판정마다 쿼리가 나간다.
|
||||
return GuestRoleResolver::hasPermission($permission, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,13 +131,7 @@ class PermissionMiddleware
|
||||
*/
|
||||
protected function getGuestRole(): ?Role
|
||||
{
|
||||
if (self::$guestRoleCache === null) {
|
||||
self::$guestRoleCache = Role::where('identifier', 'guest')
|
||||
->with('permissions')
|
||||
->first();
|
||||
}
|
||||
|
||||
return self::$guestRoleCache;
|
||||
return GuestRoleResolver::resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +145,7 @@ class PermissionMiddleware
|
||||
*/
|
||||
public static function clearGuestRoleCache(): void
|
||||
{
|
||||
self::$guestRoleCache = null;
|
||||
GuestRoleResolver::flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,6 +47,10 @@ class ActivityLogIndexRequest extends FormRequest
|
||||
// 조용히 기본 정렬로 되돌리지 않도록 한다 (service-repository.md "정렬 컬럼 화이트리스트").
|
||||
'sort_by' => ['nullable', 'string', Rule::in(['created_at', 'action', 'log_type'])],
|
||||
'sort_order' => ['nullable', 'string', Rule::in(['asc', 'desc'])],
|
||||
// 커서를 주면 목록이 키셋 방식으로 응답한다. 로그는 계속 쌓이기만 하므로 깊은
|
||||
// 페이지를 OFFSET 으로 훑으면 건너뛸 행을 실제로 읽어야 한다.
|
||||
// 형식이 깨진 값은 KeysetPaginator 가 첫 페이지로 되돌리므로 여기서는 길이만 본다.
|
||||
'cursor' => ['nullable', 'string', 'max:500'],
|
||||
];
|
||||
|
||||
return HookManager::applyFilters('core.activity_log.index_validation_rules', $rules, $this);
|
||||
|
||||
@@ -40,6 +40,10 @@ class NotificationLogIndexRequest extends FormRequest
|
||||
// 게이트가 더 좁으면 화면 정렬 셀렉트가 제공하는 옵션(수신자명순/제목순)이 422 로 막힌다.
|
||||
'sort_by' => ['nullable', 'string', 'in:id,channel,notification_type,status,sent_at,created_at,recipient_name,subject'],
|
||||
'sort_order' => ['nullable', 'string', 'in:asc,desc'],
|
||||
// 커서를 주면 목록이 키셋 방식으로 응답한다. 로그는 계속 쌓이기만 하므로 깊은
|
||||
// 페이지를 OFFSET 으로 훑으면 건너뛸 행을 실제로 읽어야 한다.
|
||||
// 형식이 깨진 값은 KeysetPaginator 가 첫 페이지로 되돌리므로 여기서는 길이만 본다.
|
||||
'cursor' => ['nullable', 'string', 'max:500'],
|
||||
];
|
||||
|
||||
return HookManager::applyFilters(
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Http\Requests\Public;
|
||||
|
||||
use App\Extension\HookManager;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
@@ -23,10 +25,30 @@ class SearchRequest extends FormRequest
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 검증 전에 쿼리 파라미터를 정규화합니다.
|
||||
*
|
||||
* 쿼리스트링은 언제나 문자열로 도착하므로 `with_counts=false` 가 `boolean` 규칙에
|
||||
* 걸려 422 가 된다. 해석 가능한 값만 캐스팅하고, 그 외(오타 등)는 손대지 않아
|
||||
* 규칙이 그대로 걸러내게 둔다 — null 로 바꾸면 오타가 "미지정" 으로 통과한다.
|
||||
*/
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if (! $this->has('with_counts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$normalized = filter_var($this->input('with_counts'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
|
||||
if ($normalized !== null) {
|
||||
$this->merge(['with_counts' => $normalized]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
@@ -35,8 +57,21 @@ class SearchRequest extends FormRequest
|
||||
'q' => ['nullable', 'string', 'min:2', 'max:200'],
|
||||
'type' => ['nullable', 'string'],
|
||||
'sort' => ['nullable', 'string'],
|
||||
'page' => ['nullable', 'integer', 'min:1'],
|
||||
// 페이지 상한은 남용 차단용이다. 정상 탐색은 has_more_pages 로 계속 열려 있고,
|
||||
// 임의의 큰 페이지 번호를 직접 던져 초대형 OFFSET 을 만드는 것만 막는다.
|
||||
'page' => array_filter([
|
||||
'nullable',
|
||||
'integer',
|
||||
'min:1',
|
||||
($maxPage = PaginationLimits::maxPage('search')) !== null ? 'max:'.$maxPage : null,
|
||||
]),
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
// 깊은 페이지를 OFFSET 없이 넘기기 위한 커서. 형식이 깨진 값은
|
||||
// KeysetPaginator 가 첫 페이지로 되돌리므로 여기서는 길이만 본다.
|
||||
'cursor' => ['nullable', 'string', 'max:500'],
|
||||
// 특정 탭만 볼 때 다른 탭의 배지 건수를 세지 않게 하는 스위치.
|
||||
// 배지를 그리지 않는 화면에서 그 집계 비용을 없앤다 (기본값은 세는 쪽).
|
||||
'with_counts' => ['nullable', 'boolean'],
|
||||
];
|
||||
|
||||
// 모듈/플러그인이 validation rules를 동적으로 추가할 수 있도록 훅 제공
|
||||
@@ -59,6 +94,7 @@ class SearchRequest extends FormRequest
|
||||
'q.max' => __('search.validation.q_max'),
|
||||
'page.integer' => __('search.validation.page_integer'),
|
||||
'page.min' => __('search.validation.page_min'),
|
||||
'page.max' => __('search.validation.page_max'),
|
||||
'per_page.integer' => __('search.validation.per_page_integer'),
|
||||
'per_page.min' => __('search.validation.per_page_min'),
|
||||
'per_page.max' => __('search.validation.per_page_max'),
|
||||
|
||||
@@ -308,6 +308,10 @@ class SaveSettingsRequest extends FormRequest
|
||||
'advanced.geoip_license_key' => ['nullable', 'string', 'max:200', 'regex:/^[A-Za-z0-9_]+$/'],
|
||||
'advanced.geoip_auto_update_enabled' => ['nullable', 'boolean'],
|
||||
|
||||
// 목록 한계값 (advanced 탭) — 0 은 무제한
|
||||
'advanced.pagination_result_cap' => ['nullable', 'integer', 'min:'.config('core.settings_limits.advanced_pagination_result_cap_min', 0), 'max:'.config('core.settings_limits.advanced_pagination_result_cap_max', 1000000)],
|
||||
'advanced.pagination_max_page' => ['nullable', 'integer', 'min:'.config('core.settings_limits.advanced_pagination_max_page_min', 0), 'max:'.config('core.settings_limits.advanced_pagination_max_page_max', 100000)],
|
||||
|
||||
// 드라이버 설정 (drivers 탭)
|
||||
'drivers.storage_driver' => $this->getTabRules($tab, 'drivers', [Rule::in(self::SUPPORTED_STORAGE_DRIVERS)]),
|
||||
'drivers.s3_bucket' => ['nullable', 'string', 'max:255'],
|
||||
@@ -713,6 +717,14 @@ class SaveSettingsRequest extends FormRequest
|
||||
'advanced.sql_query_log.required' => __('validation.settings.sql_query_log_required'),
|
||||
'advanced.sql_query_log.boolean' => __('validation.settings.sql_query_log_boolean'),
|
||||
|
||||
// 목록 한계값
|
||||
'advanced.pagination_result_cap.integer' => __('validation.settings.pagination_result_cap_integer'),
|
||||
'advanced.pagination_result_cap.min' => __('validation.settings.pagination_result_cap_min'),
|
||||
'advanced.pagination_result_cap.max' => __('validation.settings.pagination_result_cap_max'),
|
||||
'advanced.pagination_max_page.integer' => __('validation.settings.pagination_max_page_integer'),
|
||||
'advanced.pagination_max_page.min' => __('validation.settings.pagination_max_page_min'),
|
||||
'advanced.pagination_max_page.max' => __('validation.settings.pagination_max_page_max'),
|
||||
|
||||
// 코어 업데이트 설정
|
||||
'advanced.core_update_github_url.url' => __('validation.settings.core_update_github_url_invalid'),
|
||||
'advanced.core_update_github_url.max' => __('validation.settings.core_update_github_url_max'),
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* 활동 로그 컬렉션 리소스
|
||||
@@ -36,15 +35,10 @@ class ActivityLogCollection extends BaseApiCollection
|
||||
'data' => $this->mapWithRowNumber(function ($activityLog) {
|
||||
return (new ActivityLogResource($activityLog))->toArray(request());
|
||||
}),
|
||||
'pagination' => $this->resource instanceof LengthAwarePaginator ? [
|
||||
'current_page' => $this->resource->currentPage(),
|
||||
'last_page' => $this->resource->lastPage(),
|
||||
'per_page' => $this->resource->perPage(),
|
||||
'total' => $this->resource->total(),
|
||||
'from' => $this->resource->firstItem(),
|
||||
'to' => $this->resource->lastItem(),
|
||||
'has_more_pages' => $this->resource->hasMorePages(),
|
||||
] : null,
|
||||
// 메타를 손으로 조립하면 정확도 필드(total_relation·total_is_exact·result_cap)가
|
||||
// 빠진다. 그러면 상한에 걸려 잘린 건수가 화면에서 정확한 값처럼 읽힌다
|
||||
// (실측: 실제 88,792 건이 "10000건" 으로 표기). 표준 메타 한 곳만 쓴다.
|
||||
...$this->paginationMeta(),
|
||||
...($abilities ? ['abilities' => $abilities] : []),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -36,15 +36,8 @@ class IdentityMessageDefinitionCollection extends BaseApiCollection
|
||||
'data' => $this->mapWithRowNumber(function ($definition) use ($request) {
|
||||
return (new IdentityMessageDefinitionResource($definition))->toArray($request);
|
||||
}, $sortOrder),
|
||||
'pagination' => [
|
||||
'current_page' => $this->currentPage(),
|
||||
'last_page' => $this->lastPage(),
|
||||
'per_page' => $this->perPage(),
|
||||
'total' => $this->total(),
|
||||
'from' => $this->firstItem(),
|
||||
'to' => $this->lastItem(),
|
||||
'has_more_pages' => $this->hasMorePages(),
|
||||
],
|
||||
// 표준 메타를 쓴다 — 페이지 결과의 형태를 스스로 판정해 그 형태가 아는 값만 낸다.
|
||||
...$this->paginationMeta(),
|
||||
...($abilities ? ['abilities' => $abilities] : []),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Pagination\BoundedTotalAware;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Http\Resources\Traits\HasRowNumber;
|
||||
use App\Support\Query\BoundedPage;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use Illuminate\Pagination\CursorPaginator;
|
||||
|
||||
/**
|
||||
* API 컬렉션 리소스 기본 클래스
|
||||
@@ -61,24 +65,71 @@ abstract class BaseApiCollection extends ResourceCollection
|
||||
* 정확히 판정할 수 있도록 노출합니다. 전체 조회(get) 등 paginator 가 아닌
|
||||
* 경우에는 빈 배열을 반환하므로 toArray 에서 array_merge 로 안전하게 합칠 수 있습니다.
|
||||
*
|
||||
* 페이지 결과는 세 형태 중 하나이며, 형태를 스스로 판정해 그 형태가 실제로 아는
|
||||
* 값만 내보냅니다. 모르는 값을 0 이나 1 로 채우면 화면이 그것을 사실로 읽습니다.
|
||||
*
|
||||
* | 형태 | 예 | total | last_page | 추가 |
|
||||
* |---|---|---|---|---|
|
||||
* | 전체건수형 | `paginate()` | 정확 | 정확 | — |
|
||||
* | 상한형 | {@see BoundedPage} | 정확 또는 하한 | 하한이면 null | `total_relation` `total_is_exact` `result_cap` |
|
||||
* | 단순형 | `simplePaginate()` / `cursorPaginate()` | 없음 | 없음 | 커서면 `next_cursor` `prev_cursor` |
|
||||
*
|
||||
* 기존 필드는 하나도 제거하지 않으므로, 표준 `paginate()` 를 쓰는 기존 컬렉션의
|
||||
* 응답은 이전과 완전히 동일합니다.
|
||||
*
|
||||
* @return array<string, mixed> ['pagination' => [...]] 또는 빈 배열
|
||||
*/
|
||||
protected function paginationMeta(): array
|
||||
{
|
||||
if (! method_exists($this->resource, 'currentPage')) {
|
||||
$resource = $this->resource;
|
||||
|
||||
if ($resource instanceof CursorPaginator) {
|
||||
return ['pagination' => $this->cursorPaginationMeta($resource)];
|
||||
}
|
||||
|
||||
if (! method_exists($resource, 'currentPage')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$meta = [
|
||||
'current_page' => $resource->currentPage(),
|
||||
'per_page' => $resource->perPage(),
|
||||
'from' => $resource->firstItem(),
|
||||
'to' => $resource->lastItem(),
|
||||
'has_more_pages' => $resource->hasMorePages(),
|
||||
];
|
||||
|
||||
// 단순형(simplePaginate)은 총 건수를 아예 세지 않으므로 total/last_page 를 내보내지 않는다.
|
||||
if ($resource instanceof LengthAwarePaginatorContract) {
|
||||
$meta['last_page'] = $resource->lastPage();
|
||||
$meta['total'] = $resource->total();
|
||||
}
|
||||
|
||||
if ($resource instanceof BoundedTotalAware) {
|
||||
$meta['total_relation'] = $resource->totalRelation()->value;
|
||||
$meta['total_is_exact'] = $resource->totalRelation()->isExact();
|
||||
$meta['result_cap'] = $resource->resultCap();
|
||||
}
|
||||
|
||||
return ['pagination' => $meta];
|
||||
}
|
||||
|
||||
/**
|
||||
* 커서 페이지의 pagination 메타를 만듭니다.
|
||||
*
|
||||
* 커서 방식은 총 건수와 페이지 번호를 계산하지 않습니다. 대신 앞뒤 이동 커서를
|
||||
* 그대로 실어 보내며, 화면은 이 값으로 이전/다음 버튼을 만듭니다.
|
||||
*
|
||||
* @param CursorPaginator $resource 커서 페이지 결과
|
||||
* @return array<string, mixed> pagination 메타
|
||||
*/
|
||||
private function cursorPaginationMeta(CursorPaginator $resource): array
|
||||
{
|
||||
return [
|
||||
'pagination' => [
|
||||
'current_page' => $this->resource->currentPage(),
|
||||
'last_page' => $this->resource->lastPage(),
|
||||
'per_page' => $this->resource->perPage(),
|
||||
'total' => $this->resource->total(),
|
||||
'from' => $this->resource->firstItem(),
|
||||
'to' => $this->resource->lastItem(),
|
||||
'has_more_pages' => $this->resource->hasMorePages(),
|
||||
],
|
||||
'per_page' => $resource->perPage(),
|
||||
'has_more_pages' => $resource->hasMorePages(),
|
||||
'next_cursor' => $resource->nextCursor()?->encode(),
|
||||
'prev_cursor' => $resource->previousCursor()?->encode(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ class NotificationDefinitionCollection extends BaseApiCollection
|
||||
/**
|
||||
* 컬렉션을 배열로 변환합니다.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
@@ -31,15 +31,8 @@ class NotificationDefinitionCollection extends BaseApiCollection
|
||||
'data' => $this->mapWithRowNumber(function ($definition) use ($request) {
|
||||
return (new NotificationDefinitionResource($definition))->toArray($request);
|
||||
}, $sortOrder),
|
||||
'pagination' => [
|
||||
'current_page' => $this->currentPage(),
|
||||
'last_page' => $this->lastPage(),
|
||||
'per_page' => $this->perPage(),
|
||||
'total' => $this->total(),
|
||||
'from' => $this->firstItem(),
|
||||
'to' => $this->lastItem(),
|
||||
'has_more_pages' => $this->hasMorePages(),
|
||||
],
|
||||
// 표준 메타를 쓴다 — 페이지 결과의 형태를 스스로 판정해 그 형태가 아는 값만 낸다.
|
||||
...$this->paginationMeta(),
|
||||
...($abilities ? ['abilities' => $abilities] : []),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class NotificationLogCollection extends BaseApiCollection
|
||||
/**
|
||||
* 컬렉션을 배열로 변환합니다.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
@@ -31,15 +31,10 @@ class NotificationLogCollection extends BaseApiCollection
|
||||
'data' => $this->mapWithRowNumber(function ($log) use ($request) {
|
||||
return (new NotificationLogResource($log))->toArray($request);
|
||||
}, $sortOrder),
|
||||
'pagination' => [
|
||||
'current_page' => $this->resource->currentPage(),
|
||||
'last_page' => $this->resource->lastPage(),
|
||||
'per_page' => $this->resource->perPage(),
|
||||
'total' => $this->resource->total(),
|
||||
'from' => $this->resource->firstItem(),
|
||||
'to' => $this->resource->lastItem(),
|
||||
'has_more_pages' => $this->resource->hasMorePages(),
|
||||
],
|
||||
// 표준 메타를 쓴다. 이 목록은 커서 요청이면 CursorPaginator 로, 그 외에는 상한형
|
||||
// 페이지로 온다. 손으로 조립하면 커서 결과에 없는 total()/lastPage() 를 불러
|
||||
// 그 요청만 500 이 되고, 상한형에서는 정확도 메타가 빠진다.
|
||||
...$this->paginationMeta(),
|
||||
...($abilities ? ['abilities' => $abilities] : []),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources\Traits;
|
||||
|
||||
use App\Contracts\Pagination\BoundedTotalAware;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -57,7 +58,7 @@ trait HasRowNumber
|
||||
* @param int $total 전체 항목 수
|
||||
* @param int $perPage 페이지당 항목 수
|
||||
* @param int $currentPage 현재 페이지 번호
|
||||
* @return int 계산된 순번
|
||||
* @return int|null 계산된 순번 (총 건수가 잘린 내림차순이면 null)
|
||||
*/
|
||||
protected function calculateRowNumber(
|
||||
int $index,
|
||||
@@ -65,13 +66,22 @@ trait HasRowNumber
|
||||
int $total,
|
||||
int $perPage,
|
||||
int $currentPage
|
||||
): int {
|
||||
): ?int {
|
||||
if ($sortOrder === 'asc') {
|
||||
// 오름차순: 1, 2, 3, ... (페이지별 연속)
|
||||
// 예: 2페이지 → ((2-1) * 10) + 0 + 1 = 11, 12, 13, ...
|
||||
// offset 만으로 정해지므로 총 건수가 잘려도 그대로 정확하다.
|
||||
return (($currentPage - 1) * $perPage) + $index + 1;
|
||||
}
|
||||
|
||||
// 내림차순 순번은 "전체 몇 건 중 몇 번째" 라 총 건수를 알아야 나온다. 총 건수가
|
||||
// 상한에 걸려 잘렸으면 그 값으로 역산한 순번은 첫 페이지부터 이미 틀리고, 상한을
|
||||
// 넘어선 페이지에서는 0 과 음수까지 내려간다. 틀린 숫자를 내보내는 것보다
|
||||
// 내보내지 않는 편이 낫으므로 `last_page` 와 같은 원칙으로 null 을 돌려준다.
|
||||
if ($this->resource instanceof BoundedTotalAware && ! $this->resource->totalRelation()->isExact()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 내림차순: total, total-1, ... (역순)
|
||||
// 예: 총 30개, 2페이지 → 30 - ((2-1) * 10) - 0 = 20, 19, 18, ...
|
||||
return $total - (($currentPage - 1) * $perPage) - $index;
|
||||
|
||||
@@ -5,14 +5,16 @@ namespace App\Http\Resources;
|
||||
use App\Contracts\Repositories\NotificationDefinitionRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* 알림함 컬렉션
|
||||
*/
|
||||
class UserNotificationCollection extends BaseApiCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* 컬렉션을 배열로 변환합니다.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @param Request $request HTTP 요청
|
||||
* @return array<string, mixed> 변환된 배열
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
@@ -29,15 +31,10 @@ class UserNotificationCollection extends BaseApiCollection
|
||||
->withTypeLabelMap($typeLabelMap)
|
||||
->toArray($request);
|
||||
}, $sortOrder),
|
||||
'pagination' => [
|
||||
'current_page' => $this->resource->currentPage(),
|
||||
'last_page' => $this->resource->lastPage(),
|
||||
'per_page' => $this->resource->perPage(),
|
||||
'total' => $this->resource->total(),
|
||||
'from' => $this->resource->firstItem(),
|
||||
'to' => $this->resource->lastItem(),
|
||||
'has_more_pages' => $this->resource->hasMorePages(),
|
||||
],
|
||||
// 표준 메타를 쓴다. 손으로 조립하면 페이지 결과의 형태(전체건수형/상한형/단순형)를
|
||||
// 구분하지 못해, 상한에 걸려 잘린 총 건수가 정확한 값처럼 나가고 마지막 페이지도
|
||||
// 계산할 수 없는데 숫자가 채워진다.
|
||||
...$this->paginationMeta(),
|
||||
];
|
||||
}
|
||||
}
|
||||
+114
-50
@@ -51,6 +51,22 @@ class User extends Authenticatable implements HasLocalePreference
|
||||
*/
|
||||
protected array $effectiveScopeCache = [];
|
||||
|
||||
/**
|
||||
* 보유 권한 부여 내역 캐시 (인스턴스 레벨)
|
||||
*
|
||||
* 한 요청에서 권한 판정은 수십~수백 번 일어난다 — 레이아웃 노드마다, 목록 행마다,
|
||||
* 리소스 필드마다 부른다. 판정마다 DB 에 물으면 노드/행 수에 비례해 쿼리가 늘어난다.
|
||||
* 첫 판정에서 `roles.permissions` 를 한 번 적재해 두고 이후로는 배열만 본다.
|
||||
*
|
||||
* 캐시 수명은 **모델 인스턴스**, 즉 요청 스코프다. 크로스 요청 캐시를 두지 않으므로
|
||||
* 권한을 바꾸면 다음 요청부터 즉시 반영된다.
|
||||
*
|
||||
* 구조: 권한 식별자 ⇒ [{type, scope_type}, ...] (같은 권한을 여러 역할이 주면 여러 건)
|
||||
*
|
||||
* @var array<string, array<int, array{type: string|null, scope_type: string|null}>>|null
|
||||
*/
|
||||
protected ?array $permissionGrantsCache = null;
|
||||
|
||||
/**
|
||||
* 테이블명
|
||||
*
|
||||
@@ -244,14 +260,79 @@ class User extends Authenticatable implements HasLocalePreference
|
||||
*/
|
||||
public function hasPermission(string $permission, ?PermissionType $type = null): bool
|
||||
{
|
||||
return $this->roles()
|
||||
->whereHas('permissions', function ($query) use ($permission, $type) {
|
||||
$query->where('identifier', $permission);
|
||||
if ($type !== null) {
|
||||
$query->where('type', $type);
|
||||
}
|
||||
})
|
||||
->exists();
|
||||
$grants = $this->permissionGrants()[$permission] ?? null;
|
||||
|
||||
if ($grants === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($type === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($grants as $grant) {
|
||||
if ($grant['type'] === $type->value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 보유 권한 부여 내역을 반환합니다 (인스턴스 캐시).
|
||||
*
|
||||
* 역할 → 권한을 한 번만 적재하고, 이후 판정은 전부 이 배열에서 이루어집니다.
|
||||
*
|
||||
* @return array<string, array<int, array{type: string|null, scope_type: string|null}>> 권한 식별자 ⇒ 부여 내역
|
||||
*/
|
||||
protected function permissionGrants(): array
|
||||
{
|
||||
if ($this->permissionGrantsCache !== null) {
|
||||
return $this->permissionGrantsCache;
|
||||
}
|
||||
|
||||
$grants = [];
|
||||
|
||||
foreach ($this->roles()->with('permissions')->get() as $role) {
|
||||
foreach ($role->permissions as $permission) {
|
||||
$grants[$permission->identifier][] = [
|
||||
'type' => $this->enumValue($permission->type),
|
||||
'scope_type' => $this->enumValue($permission->pivot->scope_type ?? null),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $this->permissionGrantsCache = $grants;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enum 또는 원시 값을 문자열로 정규화합니다.
|
||||
*
|
||||
* 캐스팅 설정 유무에 따라 Enum 인스턴스와 문자열이 섞여 들어오므로 한 형태로 맞춥니다.
|
||||
*
|
||||
* @param mixed $value Enum 인스턴스 · 문자열 · null
|
||||
* @return string|null 정규화된 문자열 (부재 시 null)
|
||||
*/
|
||||
private function enumValue(mixed $value): ?string
|
||||
{
|
||||
if ($value instanceof \BackedEnum) {
|
||||
return (string) $value->value;
|
||||
}
|
||||
|
||||
return $value === null ? null : (string) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한 판정 캐시를 비웁니다.
|
||||
*
|
||||
* 같은 인스턴스에서 역할을 바꾼 직후 다시 판정해야 하는 경우에 호출합니다.
|
||||
* (역할 동기화 서비스가 호출하며, 일반 조회 경로에서는 필요하지 않습니다)
|
||||
*/
|
||||
public function flushPermissionCaches(): void
|
||||
{
|
||||
$this->permissionGrantsCache = null;
|
||||
$this->effectiveScopeCache = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,27 +344,17 @@ class User extends Authenticatable implements HasLocalePreference
|
||||
*/
|
||||
public function hasPermissions(array $permissions, bool $requireAll = true, ?PermissionType $type = null): bool
|
||||
{
|
||||
$userPermissions = $this->roles()
|
||||
->whereHas('permissions', function ($query) use ($permissions, $type) {
|
||||
$query->whereIn('identifier', $permissions);
|
||||
if ($type !== null) {
|
||||
$query->where('type', $type);
|
||||
}
|
||||
})
|
||||
->with(['permissions' => function ($query) use ($permissions, $type) {
|
||||
$query->whereIn('identifier', $permissions);
|
||||
if ($type !== null) {
|
||||
$query->where('type', $type);
|
||||
}
|
||||
}])
|
||||
->get()
|
||||
->pluck('permissions')
|
||||
->flatten()
|
||||
->pluck('identifier')
|
||||
->unique()
|
||||
->count();
|
||||
// 같은 권한 집합을 hasPermission 과 공유한다 — 권한 개수만큼 쿼리가 늘지 않는다.
|
||||
// 비교 기준(고유 매칭 수 vs 인자 개수)은 종전과 동일하게 유지한다.
|
||||
$matched = 0;
|
||||
|
||||
return $requireAll ? $userPermissions === count($permissions) : $userPermissions > 0;
|
||||
foreach (array_unique($permissions) as $permission) {
|
||||
if ($this->hasPermission($permission, $type)) {
|
||||
$matched++;
|
||||
}
|
||||
}
|
||||
|
||||
return $requireAll ? $matched === count($permissions) : $matched > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,33 +374,21 @@ class User extends Authenticatable implements HasLocalePreference
|
||||
return $this->effectiveScopeCache[$identifier];
|
||||
}
|
||||
|
||||
$scopeTypes = $this->roles()
|
||||
->whereHas('permissions', function ($query) use ($identifier) {
|
||||
$query->where('identifier', $identifier);
|
||||
})
|
||||
->with(['permissions' => function ($query) use ($identifier) {
|
||||
$query->where('identifier', $identifier);
|
||||
}])
|
||||
->get()
|
||||
->pluck('permissions')
|
||||
->flatten()
|
||||
->pluck('pivot.scope_type');
|
||||
// 같은 권한 집합에서 scope_type 만 뽑는다 — 권한마다 쿼리를 다시 내지 않는다.
|
||||
$values = array_column($this->permissionGrants()[$identifier] ?? [], 'scope_type');
|
||||
|
||||
// 권한 미보유 시 null 반환 (기본값: 전체 접근)
|
||||
if ($scopeTypes->isEmpty()) {
|
||||
if ($values === []) {
|
||||
return $this->effectiveScopeCache[$identifier] = null;
|
||||
}
|
||||
|
||||
// union 정책: 하나라도 null → 전체 접근
|
||||
if ($scopeTypes->contains(null)) {
|
||||
if (in_array(null, $values, true)) {
|
||||
return $this->effectiveScopeCache[$identifier] = null;
|
||||
}
|
||||
|
||||
// ScopeType Enum 값을 문자열로 변환하여 비교
|
||||
$values = $scopeTypes->map(fn ($scope) => $scope instanceof ScopeType ? $scope->value : $scope);
|
||||
|
||||
// 하나라도 'role' → role 적용
|
||||
if ($values->contains('role')) {
|
||||
if (in_array(ScopeType::Role->value, $values, true)) {
|
||||
return $this->effectiveScopeCache[$identifier] = 'role';
|
||||
}
|
||||
|
||||
@@ -369,11 +428,16 @@ class User extends Authenticatable implements HasLocalePreference
|
||||
*/
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->roles()
|
||||
->whereHas('permissions', function ($query) {
|
||||
$query->where('type', PermissionType::Admin);
|
||||
})
|
||||
->exists();
|
||||
// 같은 권한 집합을 재사용한다 — 관리자 판정은 미들웨어·리소스·레이아웃에서 반복 호출된다.
|
||||
foreach ($this->permissionGrants() as $grants) {
|
||||
foreach ($grants as $grant) {
|
||||
if ($grant['type'] === PermissionType::Admin->value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Providers;
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Support\GuestRoleResolver;
|
||||
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
@@ -11,11 +12,6 @@ use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AuthServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* guest role 캐시
|
||||
*/
|
||||
protected static ?Role $guestRoleCache = null;
|
||||
|
||||
/**
|
||||
* Register any authentication / authorization services.
|
||||
*/
|
||||
@@ -236,9 +232,9 @@ class AuthServiceProvider extends ServiceProvider
|
||||
return null; // guest role 없으면 다음 Gate 정의로 위임
|
||||
}
|
||||
|
||||
return $guestRole->permissions()
|
||||
->where('identifier', $ability)
|
||||
->exists() ? true : null;
|
||||
// 적재해 둔 권한 컬렉션에서 판정한다. 빌더를 다시 열면 캐시가 무의미해져
|
||||
// Gate 호출마다 쿼리가 나간다.
|
||||
return GuestRoleResolver::hasPermission($ability) ? true : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,13 +242,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
*/
|
||||
protected static function getGuestRole(): ?Role
|
||||
{
|
||||
if (self::$guestRoleCache === null) {
|
||||
self::$guestRoleCache = Role::where('identifier', 'guest')
|
||||
->with('permissions')
|
||||
->first();
|
||||
}
|
||||
|
||||
return self::$guestRoleCache;
|
||||
return GuestRoleResolver::resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,7 +250,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public static function clearGuestRoleCache(): void
|
||||
{
|
||||
self::$guestRoleCache = null;
|
||||
GuestRoleResolver::flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -275,16 +265,14 @@ class AuthServiceProvider extends ServiceProvider
|
||||
$hasPermission = false;
|
||||
|
||||
if ($guestRole) {
|
||||
$hasPermission = $guestRole->permissions()
|
||||
->where('identifier', $ability)
|
||||
->exists();
|
||||
$hasPermission = GuestRoleResolver::hasPermission($ability);
|
||||
}
|
||||
|
||||
return [
|
||||
'result' => self::checkGuestPermission($ability),
|
||||
'guestRole' => $guestRole,
|
||||
'hasPermission' => $hasPermission,
|
||||
'cachedRoleId' => self::$guestRoleCache?->id,
|
||||
'cachedRoleId' => $guestRole?->id,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1019,7 +1019,7 @@ class CoreServiceProvider extends ServiceProvider
|
||||
$listener = app($listenerClass);
|
||||
$listener->registerDynamicHooks();
|
||||
|
||||
Log::info('동적 훅 리스너 등록 완료', ['listener' => $listenerClass]);
|
||||
// 등록 성공은 로그로 남기지 않는다 — 요청마다 부팅되는 경로다. 실패만 아래에 남긴다.
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('동적 훅 리스너 등록 실패', [
|
||||
'listener' => $listenerClass,
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Enums\ExtensionStatus;
|
||||
use App\Extension\ExtensionManager;
|
||||
use App\Extension\Testing\ExtensionTestAllowlist;
|
||||
use App\Models\Module;
|
||||
use App\Extension\Traits\CachesModuleStatus;
|
||||
use App\Support\InstallerContext;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\File;
|
||||
@@ -14,6 +13,8 @@ use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class ModuleRouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
use CachesModuleStatus;
|
||||
|
||||
/**
|
||||
* The path to the "home" route for your application.
|
||||
*
|
||||
@@ -74,10 +75,10 @@ class ModuleRouteServiceProvider extends ServiceProvider
|
||||
}
|
||||
}
|
||||
|
||||
// 활성화된 모듈 identifier 목록 가져오기
|
||||
$activeModuleIdentifiers = Module::where('status', ExtensionStatus::Active->value)
|
||||
->pluck('identifier')
|
||||
->toArray();
|
||||
// 활성화된 모듈 identifier 목록 가져오기.
|
||||
// 같은 목록을 ModuleManager·ModuleServiceProvider 가 이미 캐시(TTL 기본 하루)해 두므로
|
||||
// 여기서 다시 조회하지 않고 그 캐시를 공유한다. 상태 변경 시 무효화도 같이 따라온다.
|
||||
$activeModuleIdentifiers = self::getActiveModuleIdentifiers();
|
||||
|
||||
$modules = File::directories($modulesPath);
|
||||
$allowlistActive = ExtensionTestAllowlist::isActive();
|
||||
|
||||
@@ -30,6 +30,7 @@ class SettingsServiceProvider extends ServiceProvider
|
||||
'geoip',
|
||||
'seo',
|
||||
'identity',
|
||||
'pagination',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,10 +5,14 @@ namespace App\Repositories;
|
||||
use App\Contracts\Repositories\ActivityLogRepositoryInterface;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Helpers\TimezoneHelper;
|
||||
use App\Http\Resources\BaseApiCollection;
|
||||
use App\Models\ActivityLog;
|
||||
use App\Repositories\Concerns\HasMultipleSearchFilters;
|
||||
use App\Repositories\Concerns\PaginatesWithDeferredJoin;
|
||||
use App\Repositories\Concerns\ResolvesSortSpec;
|
||||
use App\Support\Query\KeysetPaginator;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Contracts\Pagination\CursorPaginator;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
@@ -65,16 +69,22 @@ class ActivityLogRepository implements ActivityLogRepositoryInterface
|
||||
columns: ['*'],
|
||||
sort: [['column' => 'created_at', 'direction' => $sortOrder]],
|
||||
perPage: (int) ($filters['per_page'] ?? 15),
|
||||
// 로그 테이블은 계속 쌓이기만 한다. 총 건수는 상한까지만 세고 "다음" 이동은
|
||||
// per_page + 1 실측으로 끝까지 열어 둔다 (계산 불가는 마지막 페이지 번호 하나뿐).
|
||||
resultCap: PaginationLimits::resultCap('admin.activity_logs'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 활동 로그 목록을 페이지네이션하여 조회합니다.
|
||||
*
|
||||
* 요청에 `cursor` 가 있으면 키셋(커서) 방식으로, 없으면 페이지 번호 방식으로 응답합니다.
|
||||
* 두 방식의 응답 봉투 차이는 {@see BaseApiCollection} 이 흡수합니다.
|
||||
*
|
||||
* @param array $filters 필터 조건
|
||||
* @return LengthAwarePaginator 페이지네이션된 로그 목록
|
||||
* @return LengthAwarePaginator|CursorPaginator 페이지네이션된 로그 목록
|
||||
*/
|
||||
public function getPaginated(array $filters = []): LengthAwarePaginator
|
||||
public function getPaginated(array $filters = []): LengthAwarePaginator|CursorPaginator
|
||||
{
|
||||
// 관계는 지연 조인의 outer 에서만 로드한다 (inner 는 키 컬럼만 조회한다)
|
||||
$query = ActivityLog::query();
|
||||
@@ -122,6 +132,26 @@ class ActivityLogRepository implements ActivityLogRepositoryInterface
|
||||
}
|
||||
|
||||
$sort = $this->resolveSortSpec($filters, self::SORTABLE_COLUMNS, 'created_at');
|
||||
$perPage = (int) ($filters['per_page'] ?? 15);
|
||||
|
||||
// 커서를 받은 요청은 키셋으로 응답한다. 로그는 계속 쌓이기만 해서 깊은 페이지를
|
||||
// OFFSET 으로 훑으면 건너뛸 행을 실제로 읽어야 하지만, 커서는 직전 페이지의 정렬
|
||||
// 키를 WHERE 경계로 삼아 깊이와 무관하게 일정하다.
|
||||
// 커서 모드에서는 OFFSET 자체가 없으므로 지연 조인이 해결하려던 문제도 함께 사라진다.
|
||||
$sortKeys = array_map(
|
||||
static fn (array $spec): array => [$spec['column'], $spec['direction']],
|
||||
$sort
|
||||
);
|
||||
|
||||
if (! empty($filters['cursor']) && KeysetPaginator::supports($sortKeys, self::SORTABLE_COLUMNS)) {
|
||||
return KeysetPaginator::paginate(
|
||||
query: $query->with('user:id,uuid,name,email'),
|
||||
perPage: $perPage,
|
||||
sortKeys: $sortKeys,
|
||||
uniqueKey: 'id',
|
||||
cursor: (string) $filters['cursor'],
|
||||
);
|
||||
}
|
||||
|
||||
// 목록 컬럼을 좁히지 않는 이유: 활동 로그 리소스는 변경 내역(mediumText `changes`)과
|
||||
// 부가 정보(`properties`)까지 그대로 노출하므로 컬럼을 빼면 응답 계약이 바뀐다.
|
||||
@@ -130,8 +160,11 @@ class ActivityLogRepository implements ActivityLogRepositoryInterface
|
||||
query: $query,
|
||||
columns: ['*'],
|
||||
sort: $sort,
|
||||
perPage: (int) ($filters['per_page'] ?? 15),
|
||||
perPage: $perPage,
|
||||
relations: ['user:id,uuid,name,email'],
|
||||
// 로그 테이블은 계속 쌓이기만 한다. 총 건수는 상한까지만 세고 "다음" 이동은
|
||||
// per_page + 1 실측으로 끝까지 열어 둔다 (계산 불가는 마지막 페이지 번호 하나뿐).
|
||||
resultCap: PaginationLimits::resultCap('admin.activity_logs'),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Concerns;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* 날짜 필터를 인덱스가 살아 있는 범위 조건으로 적용하는 Trait
|
||||
*
|
||||
* `whereDate('created_at', ...)` 는 컬럼에 `DATE()` 를 씌운다. 함수가 씌워진 컬럼은
|
||||
* 인덱스를 탈 수 없으므로, 그 조건 하나 때문에 목록 전체가 풀스캔이 된다. 같은 결과를
|
||||
* 내는 `>= 하루 시작` / `<= 하루 끝` 범위 조건으로 바꾸면 인덱스가 그대로 쓰인다.
|
||||
*
|
||||
* 경계 처리를 각 저장소가 따로 적으면 한쪽이 종료일 하루를 통째로 빠뜨리기 쉽다
|
||||
* (`<= '2026-08-02'` 는 그날 00:00:00 까지만 포함한다). 경계는 이 Trait 한 곳에서만 정한다.
|
||||
*/
|
||||
trait FiltersByDateRange
|
||||
{
|
||||
/**
|
||||
* 기간(시작일~종료일) 필터를 범위 조건으로 적용합니다.
|
||||
*
|
||||
* 종료일은 그날 23:59:59.999999 까지 포함하므로 `whereDate(..., '<=', ...)` 와
|
||||
* 같은 결과를 냅니다.
|
||||
*
|
||||
* @param Builder|\Illuminate\Database\Query\Builder $query 대상 쿼리
|
||||
* @param string $column 날짜 컬럼 (테이블 한정자 포함 가능)
|
||||
* @param string|null $startDate 시작일 (빈 값이면 미적용)
|
||||
* @param string|null $endDate 종료일 (빈 값이면 미적용)
|
||||
*/
|
||||
protected function applyDateRangeFilter($query, string $column, ?string $startDate, ?string $endDate): void
|
||||
{
|
||||
if (! empty($startDate)) {
|
||||
$query->where($column, '>=', CarbonImmutable::parse($startDate)->startOfDay());
|
||||
}
|
||||
|
||||
if (! empty($endDate)) {
|
||||
$query->where($column, '<=', CarbonImmutable::parse($endDate)->endOfDay());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 하루에 해당하는 행만 남기는 필터를 범위 조건으로 적용합니다.
|
||||
*
|
||||
* @param Builder|\Illuminate\Database\Query\Builder $query 대상 쿼리
|
||||
* @param string $column 날짜 컬럼 (테이블 한정자 포함 가능)
|
||||
* @param \DateTimeInterface|string $day 대상 날짜
|
||||
*/
|
||||
protected function applyDayFilter($query, string $column, \DateTimeInterface|string $day): void
|
||||
{
|
||||
$target = CarbonImmutable::parse($day instanceof \DateTimeInterface ? $day->format('Y-m-d H:i:s') : $day);
|
||||
|
||||
$query->whereBetween($column, [$target->startOfDay(), $target->endOfDay()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 연·월에 해당하는 행만 남기는 필터를 범위 조건으로 적용합니다.
|
||||
*
|
||||
* `whereYear` + `whereMonth` 조합은 컬럼에 함수를 두 번 씌워 인덱스를 완전히 막습니다.
|
||||
*
|
||||
* @param Builder|\Illuminate\Database\Query\Builder $query 대상 쿼리
|
||||
* @param string $column 날짜 컬럼 (테이블 한정자 포함 가능)
|
||||
* @param int $year 연도
|
||||
* @param int $month 월 (1~12)
|
||||
*/
|
||||
protected function applyMonthFilter($query, string $column, int $year, int $month): void
|
||||
{
|
||||
$start = CarbonImmutable::create($year, $month, 1)->startOfDay();
|
||||
|
||||
$query->whereBetween($column, [$start, $start->endOfMonth()]);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Repositories\Concerns;
|
||||
|
||||
use App\Enums\TotalRelation;
|
||||
use App\Support\Query\BoundedPage;
|
||||
use App\Support\Query\BoundedPaginator;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Database\Query\Expression;
|
||||
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
@@ -66,10 +70,11 @@ trait PaginatesWithDeferredJoin
|
||||
* @param bool $preserveIdOrder true = inner 가 정한 ID 순서를 outer 에서 그대로 복원
|
||||
* @param string $pageName 페이지 쿼리 파라미터명
|
||||
* @param Closure|null $outerUsing outer 에만 적용할 조인/집계 (inner 에서는 실행되지 않는다)
|
||||
* @return PaginatorContract 페이지네이터 ($simple=true 면 Paginator, 아니면 LengthAwarePaginator)
|
||||
* @param int|null $resultCap 총 건수 집계 상한 (null = 항상 정확한 COUNT)
|
||||
* @return PaginatorContract 페이지네이터 ($simple=true 면 Paginator, $resultCap 지정 시 BoundedPage, 아니면 LengthAwarePaginator)
|
||||
*/
|
||||
protected function paginateWithDeferredJoin(
|
||||
Builder $query,
|
||||
Builder|Relation $query,
|
||||
array $columns,
|
||||
array $sort,
|
||||
int $perPage,
|
||||
@@ -82,7 +87,14 @@ trait PaginatesWithDeferredJoin
|
||||
bool $preserveIdOrder = false,
|
||||
string $pageName = 'page',
|
||||
?Closure $outerUsing = null,
|
||||
?int $resultCap = null,
|
||||
): PaginatorContract {
|
||||
// 관계(`$model->items()`)도 받는다 — 표준 `paginate()` 가 되는 자리는 이 계약으로
|
||||
// 바꿔도 되어야 한다. 관계의 소속 조건은 그 밑 빌더에 이미 들어가 있어 보존된다.
|
||||
// (쿼리 빌더는 받지 않는다. 이 계약은 모델의 키 컬럼·eager load 를 다루므로
|
||||
// Eloquent 가 전제다 — 넓힐 수 있는 범위와 없는 범위를 구분한다.)
|
||||
$query = $query instanceof Relation ? $query->getQuery() : $query;
|
||||
|
||||
$page = $page ?: Paginator::resolveCurrentPage($pageName);
|
||||
$page = max(1, $page);
|
||||
|
||||
@@ -94,7 +106,16 @@ trait PaginatesWithDeferredJoin
|
||||
// whereHas 는 결과 집합을 결정하는 조건이라 setEagerLoads 로 지워지지 않는다 (의도된 구분).
|
||||
$inner = (clone $query)->setEagerLoads([]);
|
||||
|
||||
if (! $simple && $total === null) {
|
||||
// 상한이 지정되면 총 건수를 상한까지만 센다. 다음 페이지 판정은 총 건수와 무관하게
|
||||
// per_page + 1 실측으로 하므로, 마지막 페이지 번호 하나만 계산 불가가 된다.
|
||||
$bounded = ! $simple && $resultCap !== null && $resultCap > 0;
|
||||
$relation = null;
|
||||
|
||||
if ($bounded && $total === null) {
|
||||
// Eloquent 빌더를 그대로 넘긴다 — countWithCap 이 내부에서 toBase() 로
|
||||
// 글로벌 스코프까지 적용한 기반 쿼리를 만든다.
|
||||
[$total, $relation] = BoundedPaginator::countWithCap(clone $inner, $resultCap);
|
||||
} elseif (! $simple && $total === null) {
|
||||
// Laravel 의 paginate() 와 같은 집계 경로를 쓴다. count() 는 groupBy/having 이 있는
|
||||
// 쿼리에서 `select count(*) ... group by ...` 를 그대로 실행해 **첫 그룹의 행 수**를
|
||||
// 총 건수로 돌려준다. getCountForPagination() 은 그룹 쿼리를 서브쿼리로 감싸므로
|
||||
@@ -105,10 +126,11 @@ trait PaginatesWithDeferredJoin
|
||||
|
||||
$this->applySortSpec($inner, $sort);
|
||||
|
||||
// simple 모드는 다음 페이지 유무 판정을 위해 한 건을 더 읽는다 (Paginator 가 잘라낸다).
|
||||
// simple/bounded 모드는 다음 페이지 유무 판정을 위해 한 건을 더 읽는다.
|
||||
// forPage() 를 쓰면 offset 이 (page-1) * (perPage+1) 로 계산돼 페이지가 넘어갈수록
|
||||
// 건너뛰는 행이 어긋나므로, offset 은 perPage 기준으로 직접 지정한다.
|
||||
$fetchCount = $simple ? $perPage + 1 : $perPage;
|
||||
$probesNextPage = $simple || $bounded;
|
||||
$fetchCount = $probesNextPage ? $perPage + 1 : $perPage;
|
||||
|
||||
$ids = $inner
|
||||
->select($inner->getModel()->qualifyColumn($keyName))
|
||||
@@ -116,6 +138,12 @@ trait PaginatesWithDeferredJoin
|
||||
->limit($fetchCount)
|
||||
->pluck($keyName);
|
||||
|
||||
$hasMorePages = $probesNextPage && $ids->count() > $perPage;
|
||||
|
||||
if ($bounded && $hasMorePages) {
|
||||
$ids = $ids->slice(0, $perPage)->values();
|
||||
}
|
||||
|
||||
$items = $ids->isEmpty()
|
||||
? new Collection
|
||||
: $this->fetchDeferredJoinRows($query, $columns, $sort, $ids->all(), $relations, $withCount, $keyName, $preserveIdOrder, $outerUsing);
|
||||
@@ -129,6 +157,22 @@ trait PaginatesWithDeferredJoin
|
||||
return new Paginator($items, $perPage, $page, $options);
|
||||
}
|
||||
|
||||
if ($bounded) {
|
||||
// 동시 삽입으로 집계 시점과 조회 시점의 모수가 달라질 수 있다
|
||||
$seenSoFar = ($page - 1) * $perPage + $items->count();
|
||||
|
||||
return new BoundedPage(
|
||||
items: $items,
|
||||
total: max((int) $total, $seenSoFar),
|
||||
perPage: $perPage,
|
||||
currentPage: $page,
|
||||
totalRelation: $relation ?? TotalRelation::Exact,
|
||||
resultCap: $resultCap,
|
||||
hasMorePages: $hasMorePages,
|
||||
options: $options,
|
||||
);
|
||||
}
|
||||
|
||||
return new LengthAwarePaginator($items, $total ?? $items->count(), $perPage, $page, $options);
|
||||
}
|
||||
|
||||
|
||||
@@ -263,6 +263,7 @@ class IdentityPolicyRepository implements IdentityPolicyRepositoryInterface
|
||||
*/
|
||||
public function allEnabled(): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 본인인증 정책은 운영자가 등록한 수만큼만 존재한다 (사용량과 무관)
|
||||
return IdentityPolicy::query()->where('enabled', true)->get();
|
||||
}
|
||||
|
||||
@@ -347,6 +348,7 @@ class IdentityPolicyRepository implements IdentityPolicyRepositoryInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
// audit:allow query-unbounded-get reason: 본인인증 정책은 운영자가 등록한 수만큼만 존재한다 (사용량과 무관)
|
||||
return IdentityPolicy::query()
|
||||
->where('scope', 'hook')
|
||||
->distinct()
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Helpers\TimezoneHelper;
|
||||
use App\Models\IdentityPolicy;
|
||||
use App\Models\IdentityVerificationLog;
|
||||
use App\Repositories\Concerns\PaginatesWithDeferredJoin;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
@@ -219,6 +220,9 @@ class IdentityVerificationLogRepository implements IdentityVerificationLogReposi
|
||||
columns: ['*'],
|
||||
sort: [['column' => $sortBy, 'direction' => $sortOrder]],
|
||||
perPage: $perPage,
|
||||
// 로그 테이블은 계속 쌓이기만 한다. 총 건수는 상한까지만 세고 "다음" 이동은
|
||||
// per_page + 1 실측으로 끝까지 열어 둔다 (계산 불가는 마지막 페이지 번호 하나뿐).
|
||||
resultCap: PaginationLimits::resultCap('admin.identity_logs'),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,17 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
*/
|
||||
private ?array $cache = null;
|
||||
|
||||
/**
|
||||
* 카테고리별 메모리 캐시
|
||||
*
|
||||
* 부팅 한 번에 같은 카테고리를 여러 곳에서 읽는다 — SettingsServiceProvider 의
|
||||
* apply*Config 9종과 loadCoreSettingsToConfig 가 각각 조회하므로, 캐시가 없으면
|
||||
* 요청마다 카테고리 수만큼 곱한 횟수로 파일 존재 확인·읽기·JSON 파싱이 반복된다.
|
||||
*
|
||||
* @var array<string, array<string, mixed>>
|
||||
*/
|
||||
private array $categoryCache = [];
|
||||
|
||||
/**
|
||||
* 모든 카테고리의 설정을 조회합니다.
|
||||
*
|
||||
@@ -67,9 +78,27 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
/**
|
||||
* 특정 카테고리의 설정을 조회합니다.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
* @param string $category 카테고리명
|
||||
* @return array<string, mixed> 기본값과 병합된 설정
|
||||
*/
|
||||
public function getCategory(string $category): array
|
||||
{
|
||||
if (array_key_exists($category, $this->categoryCache)) {
|
||||
return $this->categoryCache[$category];
|
||||
}
|
||||
|
||||
return $this->categoryCache[$category] = $this->readCategory($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* 카테고리 설정을 파일에서 읽어 기본값과 병합합니다.
|
||||
*
|
||||
* 캐시를 거치지 않는 실제 읽기 경로입니다.
|
||||
*
|
||||
* @param string $category 카테고리명
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function readCategory(string $category): array
|
||||
{
|
||||
if (! $this->categoryExists($category)) {
|
||||
return $this->getDefaultsForCategory($category);
|
||||
@@ -77,11 +106,17 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
$path = $this->getCategoryPath($category);
|
||||
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
if (! Storage::disk(self::STORAGE_DISK)->exists($path)) {
|
||||
return $this->getDefaultsForCategory($category);
|
||||
}
|
||||
|
||||
try {
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$content = Storage::disk(self::STORAGE_DISK)->get($path);
|
||||
$data = json_decode($content, true);
|
||||
|
||||
@@ -113,6 +148,8 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
* 도트 노테이션으로 특정 설정값을 조회합니다.
|
||||
*
|
||||
* @param string $key 예: 'mail.host', 'general.site_name'
|
||||
* @param mixed $default 값이 없을 때 돌려줄 기본값
|
||||
* @return mixed 설정값 (없으면 $default)
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
@@ -131,6 +168,10 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
/**
|
||||
* 도트 노테이션으로 특정 설정값을 저장합니다.
|
||||
*
|
||||
* @param string $key 설정 키 (카테고리.항목)
|
||||
* @param mixed $value 저장할 값
|
||||
* @return bool 저장 성공 여부
|
||||
*/
|
||||
public function set(string $key, mixed $value): bool
|
||||
{
|
||||
@@ -150,7 +191,8 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
/**
|
||||
* 여러 설정을 일괄 저장합니다.
|
||||
*
|
||||
* @param array<string, mixed> $settings
|
||||
* @param array<string, mixed> $settings 도트 노테이션 키 ⇒ 값
|
||||
* @return bool 저장 성공 여부
|
||||
*/
|
||||
public function setMany(array $settings): bool
|
||||
{
|
||||
@@ -171,7 +213,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
/**
|
||||
* 특정 카테고리의 설정을 저장합니다.
|
||||
*
|
||||
* @param array<string, mixed> $settings
|
||||
* @param string $category 카테고리명
|
||||
* @param array<string, mixed> $settings 저장할 설정
|
||||
* @return bool 저장 성공 여부
|
||||
*/
|
||||
public function saveCategory(string $category, array $settings): bool
|
||||
{
|
||||
@@ -195,6 +239,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
try {
|
||||
// 파일 잠금으로 동시 쓰기 방지
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$fullPath = Storage::disk(self::STORAGE_DISK)->path($path);
|
||||
$handle = fopen($fullPath, 'c');
|
||||
|
||||
@@ -211,8 +258,10 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
fclose($handle);
|
||||
|
||||
// 캐시 무효화
|
||||
// 캐시 무효화 — 저장한 카테고리와 전체 맵 양쪽을 비운다.
|
||||
// 한쪽만 비우면 저장 직후 조회가 이전 값을 돌려준다.
|
||||
$this->cache = null;
|
||||
unset($this->categoryCache[$category]);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
@@ -226,6 +275,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
/**
|
||||
* 설정 키 존재 여부를 확인합니다.
|
||||
*
|
||||
* @param string $key 설정 키 (카테고리.항목)
|
||||
* @return bool 존재 여부
|
||||
*/
|
||||
public function has(string $key): bool
|
||||
{
|
||||
@@ -234,6 +286,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
/**
|
||||
* 특정 설정을 삭제합니다.
|
||||
*
|
||||
* @param string $key 설정 키 (카테고리.항목)
|
||||
* @return bool 삭제 성공 여부
|
||||
*/
|
||||
public function delete(string $key): bool
|
||||
{
|
||||
@@ -269,6 +324,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
/**
|
||||
* 카테고리 존재 여부를 확인합니다.
|
||||
*
|
||||
* @param string $category 카테고리명
|
||||
* @return bool 존재 여부
|
||||
*/
|
||||
public function categoryExists(string $category): bool
|
||||
{
|
||||
@@ -278,7 +336,8 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
/**
|
||||
* 설정 파일을 초기화합니다.
|
||||
*
|
||||
* @param array<string, array<string, mixed>> $settings
|
||||
* @param array<string, array<string, mixed>> $settings 기본값 위에 덮어쓸 설정
|
||||
* @return bool 초기화 성공 여부
|
||||
*/
|
||||
public function initialize(array $settings = []): bool
|
||||
{
|
||||
@@ -310,6 +369,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
$backupPath = self::BACKUP_DIR.'/'.$backupName;
|
||||
|
||||
$zip = new \ZipArchive;
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$fullBackupPath = Storage::disk(self::STORAGE_DISK)->path($backupPath);
|
||||
|
||||
if ($zip->open($fullBackupPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
|
||||
@@ -318,6 +380,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
foreach ($this->getCategories() as $category) {
|
||||
$categoryPath = $this->getCategoryPath($category);
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$fullCategoryPath = Storage::disk(self::STORAGE_DISK)->path($categoryPath);
|
||||
|
||||
if (file_exists($fullCategoryPath)) {
|
||||
@@ -332,9 +397,15 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
|
||||
/**
|
||||
* 백업에서 설정을 복원합니다.
|
||||
*
|
||||
* @param string $backupPath 백업 파일 경로 (disk 기준)
|
||||
* @return bool 복원 성공 여부
|
||||
*/
|
||||
public function restore(string $backupPath): bool
|
||||
{
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$fullBackupPath = Storage::disk(self::STORAGE_DISK)->path($backupPath);
|
||||
|
||||
if (! file_exists($fullBackupPath)) {
|
||||
@@ -356,12 +427,16 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
if ($this->categoryExists($category)) {
|
||||
$content = $zip->getFromIndex($i);
|
||||
$categoryPath = $this->getCategoryPath($category);
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
Storage::disk(self::STORAGE_DISK)->put($categoryPath, $content);
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
$this->cache = null;
|
||||
$this->categoryCache = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -457,6 +532,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
*/
|
||||
private function ensureDirectoryExists(): void
|
||||
{
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$disk = Storage::disk(self::STORAGE_DISK);
|
||||
|
||||
if (! $disk->exists('.')) {
|
||||
@@ -469,6 +547,9 @@ class JsonConfigRepository implements ConfigRepositoryInterface
|
||||
*/
|
||||
private function ensureBackupDirectoryExists(): void
|
||||
{
|
||||
// audit:allow no-storage-disk-direct reason: 이 저장소는 SettingsServiceProvider::register() 에서
|
||||
// `new` 로 직접 생성된다 — 컨테이너 바인딩 이전이라 StorageInterface 를 해석할 수 없다.
|
||||
// 설정 JSON 은 부팅 자체가 의존하는 자료라 드라이버 추상화보다 부팅 순서가 우선한다.
|
||||
$disk = Storage::disk(self::STORAGE_DISK);
|
||||
|
||||
if (! $disk->exists(self::BACKUP_DIR)) {
|
||||
|
||||
@@ -50,6 +50,7 @@ class LanguagePackRepository implements LanguagePackRepositoryInterface
|
||||
*/
|
||||
public function getActivePacks(): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 언어팩은 설치된 팩 × 로케일 수만큼만 존재한다 (사용량과 무관)
|
||||
return LanguagePack::query()
|
||||
->where('status', LanguagePackStatus::Active->value)
|
||||
->get();
|
||||
@@ -96,6 +97,7 @@ class LanguagePackRepository implements LanguagePackRepositoryInterface
|
||||
?string $targetIdentifier,
|
||||
string $locale
|
||||
): Collection {
|
||||
// audit:allow query-unbounded-get reason: 언어팩은 설치된 팩 × 로케일 수만큼만 존재한다 (사용량과 무관)
|
||||
return LanguagePack::query()
|
||||
->where('scope', $scope)
|
||||
->where('target_identifier', $targetIdentifier)
|
||||
@@ -112,6 +114,7 @@ class LanguagePackRepository implements LanguagePackRepositoryInterface
|
||||
*/
|
||||
public function getActiveCoreLocales(): array
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 언어팩은 설치된 팩 × 로케일 수만큼만 존재한다 (사용량과 무관)
|
||||
return LanguagePack::query()
|
||||
->where('scope', LanguagePackScope::Core->value)
|
||||
->where('status', LanguagePackStatus::Active->value)
|
||||
@@ -246,6 +249,7 @@ class LanguagePackRepository implements LanguagePackRepositoryInterface
|
||||
*/
|
||||
public function getPacksForTarget(string $scope, string $targetIdentifier): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 언어팩은 설치된 팩 × 로케일 수만큼만 존재한다 (사용량과 무관)
|
||||
return LanguagePack::query()
|
||||
->where('scope', $scope)
|
||||
->where('target_identifier', $targetIdentifier)
|
||||
@@ -260,6 +264,7 @@ class LanguagePackRepository implements LanguagePackRepositoryInterface
|
||||
*/
|
||||
public function getPacksForLocale(string $locale): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 언어팩은 설치된 팩 × 로케일 수만큼만 존재한다 (사용량과 무관)
|
||||
return LanguagePack::query()->where('locale', $locale)->get();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,11 @@ use App\Models\NotificationLog;
|
||||
use App\Models\User;
|
||||
use App\Repositories\Concerns\PaginatesWithDeferredJoin;
|
||||
use App\Repositories\Concerns\ResolvesSortSpec;
|
||||
use App\Support\Query\KeysetPaginator;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Pagination\CursorPaginator;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
class NotificationLogRepository implements NotificationLogRepositoryInterface
|
||||
@@ -101,9 +104,9 @@ class NotificationLogRepository implements NotificationLogRepositoryInterface
|
||||
* @param array<string, mixed> $filters 필터 조건
|
||||
* @param int $perPage 페이지당 건수
|
||||
* @param User|null $scopeUser 스코프 적용 대상 사용자 (null이면 스코프 미적용)
|
||||
* @return LengthAwarePaginator 페이지네이션 결과
|
||||
* @return LengthAwarePaginator|CursorPaginator 페이지네이션 결과 (커서 요청 시 키셋)
|
||||
*/
|
||||
public function getPaginated(array $filters = [], int $perPage = 20, ?User $scopeUser = null): LengthAwarePaginator
|
||||
public function getPaginated(array $filters = [], int $perPage = 20, ?User $scopeUser = null): LengthAwarePaginator|CursorPaginator
|
||||
{
|
||||
// 관계는 지연 조인의 outer 에서만 로드한다 (inner 는 키 컬럼만 조회한다)
|
||||
$query = NotificationLog::query();
|
||||
@@ -152,6 +155,24 @@ class NotificationLogRepository implements NotificationLogRepositoryInterface
|
||||
|
||||
$sort = $this->resolveSortSpec($filters, self::SORTABLE_COLUMNS, 'sent_at');
|
||||
|
||||
// 커서를 받은 요청은 키셋으로 응답한다. 로그는 계속 쌓이기만 해서 깊은 페이지를
|
||||
// OFFSET 으로 훑으면 건너뛸 행을 실제로 읽어야 하지만, 커서는 직전 페이지의 정렬
|
||||
// 키를 WHERE 경계로 삼아 깊이와 무관하게 일정하다.
|
||||
$sortKeys = array_map(
|
||||
static fn (array $spec): array => [$spec['column'], $spec['direction']],
|
||||
$sort
|
||||
);
|
||||
|
||||
if (! empty($filters['cursor']) && KeysetPaginator::supports($sortKeys, self::SORTABLE_COLUMNS)) {
|
||||
return KeysetPaginator::paginate(
|
||||
query: $query->with(['senderUser', 'recipientUser']),
|
||||
perPage: $perPage,
|
||||
sortKeys: $sortKeys,
|
||||
uniqueKey: 'id',
|
||||
cursor: (string) $filters['cursor'],
|
||||
);
|
||||
}
|
||||
|
||||
// 목록 컬럼을 좁히지 않는 이유: 이 목록의 리소스는 렌더링된 본문(longText `body`)까지
|
||||
// 그대로 노출하므로 컬럼을 빼면 응답 계약이 바뀐다. 지연 조인만으로도 본문을 읽는
|
||||
// 행 수가 OFFSET 과 무관하게 이번 페이지 분량으로 고정된다.
|
||||
@@ -161,6 +182,9 @@ class NotificationLogRepository implements NotificationLogRepositoryInterface
|
||||
sort: $sort,
|
||||
perPage: $perPage,
|
||||
relations: ['senderUser', 'recipientUser'],
|
||||
// 로그 테이블은 계속 쌓이기만 한다. 총 건수는 상한까지만 세고 "다음" 이동은
|
||||
// per_page + 1 실측으로 끝까지 열어 둔다 (계산 불가는 마지막 페이지 번호 하나뿐).
|
||||
resultCap: PaginationLimits::resultCap('admin.notification_logs'),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace App\Repositories;
|
||||
|
||||
use App\Contracts\Repositories\NotificationRepositoryInterface;
|
||||
use App\Models\User;
|
||||
use App\Support\Query\BoundedPaginator;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Notifications\DatabaseNotification;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
@@ -32,7 +34,14 @@ class NotificationRepository implements NotificationRepositoryInterface
|
||||
// 걷어낼 넓은 컬럼이 없다
|
||||
// 정렬 마지막의 기본키는 전순서 보장용이다 — created_at 동률에서 페이지 경계가
|
||||
// 흔들려 인접 페이지가 같은 알림을 중복 노출하고 다른 알림을 누락하는 것을 막는다.
|
||||
return $query->orderBy('created_at', 'desc')->orderBy('id', 'desc')->paginate($perPage);
|
||||
//
|
||||
// 알림함은 한 사용자에 묶이지만 시간이 지날수록 계속 쌓인다(설정성 테이블이 아니다).
|
||||
// 총 건수는 상한까지만 세고, 뒤쪽 페이지 이동은 실측으로 끝까지 열어 둔다.
|
||||
return BoundedPaginator::paginate(
|
||||
$query->orderBy('created_at', 'desc')->orderBy('id', 'desc'),
|
||||
perPage: $perPage,
|
||||
resultCap: PaginationLimits::resultCap('user.notifications'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,6 +17,7 @@ class RoleRepository implements RoleRepositoryInterface
|
||||
*/
|
||||
public function getAll(): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 역할은 운영자가 정의한 수만큼만 존재한다 (회원 수와 무관)
|
||||
return Role::with(['permissions'])
|
||||
->orderBy('id')
|
||||
->get();
|
||||
@@ -29,6 +30,7 @@ class RoleRepository implements RoleRepositoryInterface
|
||||
*/
|
||||
public function getActiveRoles(): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 역할은 운영자가 정의한 수만큼만 존재한다 (회원 수와 무관)
|
||||
return Role::where('is_active', true)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
@@ -142,6 +144,7 @@ class RoleRepository implements RoleRepositoryInterface
|
||||
*/
|
||||
public function getByExtension(ExtensionOwnerType $extensionType, string $extensionIdentifier): Collection
|
||||
{
|
||||
// audit:allow query-unbounded-get reason: 역할은 운영자가 정의한 수만큼만 존재한다 (회원 수와 무관)
|
||||
return Role::where('extension_type', $extensionType)
|
||||
->where('extension_identifier', $extensionIdentifier)
|
||||
->get();
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Contracts\Repositories\ScheduleHistoryRepositoryInterface;
|
||||
use App\Models\ScheduleHistory;
|
||||
use App\Repositories\Concerns\PaginatesWithDeferredJoin;
|
||||
use App\Repositories\Concerns\ResolvesSortSpec;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
@@ -116,6 +117,9 @@ class ScheduleHistoryRepository implements ScheduleHistoryRepositoryInterface
|
||||
sort: $sort,
|
||||
perPage: $perPage,
|
||||
relations: ['triggeredBy'],
|
||||
// 로그 테이블은 계속 쌓이기만 한다. 총 건수는 상한까지만 세고 "다음" 이동은
|
||||
// per_page + 1 실측으로 끝까지 열어 둔다 (계산 불가는 마지막 페이지 번호 하나뿐).
|
||||
resultCap: PaginationLimits::resultCap('admin.schedule_histories'),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Enums\ScheduleResultStatus;
|
||||
use App\Enums\ScheduleType;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Models\Schedule;
|
||||
use App\Repositories\Concerns\FiltersByDateRange;
|
||||
use App\Repositories\Concerns\HasMultipleSearchFilters;
|
||||
use App\Repositories\Concerns\ResolvesSortSpec;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
@@ -16,6 +17,7 @@ use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ScheduleRepository implements ScheduleRepositoryInterface
|
||||
{
|
||||
use FiltersByDateRange;
|
||||
use HasMultipleSearchFilters;
|
||||
use ResolvesSortSpec;
|
||||
|
||||
@@ -198,13 +200,13 @@ class ScheduleRepository implements ScheduleRepositoryInterface
|
||||
*/
|
||||
private function applyDateFilters(Builder $query, array $filters): void
|
||||
{
|
||||
if (! empty($filters['created_from'])) {
|
||||
$query->whereDate('created_at', '>=', $filters['created_from']);
|
||||
}
|
||||
|
||||
if (! empty($filters['created_to'])) {
|
||||
$query->whereDate('created_at', '<=', $filters['created_to']);
|
||||
}
|
||||
// whereDate 는 컬럼에 DATE() 를 씌워 인덱스를 무력화한다 — 범위 조건으로 준다.
|
||||
$this->applyDateRangeFilter(
|
||||
$query,
|
||||
'created_at',
|
||||
$filters['created_from'] ?? null,
|
||||
$filters['created_to'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,9 +5,11 @@ namespace App\Repositories;
|
||||
use App\Contracts\Repositories\UserRepositoryInterface;
|
||||
use App\Helpers\PermissionHelper;
|
||||
use App\Models\User;
|
||||
use App\Repositories\Concerns\FiltersByDateRange;
|
||||
use App\Repositories\Concerns\HasMultipleSearchFilters;
|
||||
use App\Repositories\Concerns\PaginatesWithDeferredJoin;
|
||||
use App\Repositories\Concerns\ResolvesSortSpec;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
@@ -16,6 +18,7 @@ use Laravel\Sanctum\PersonalAccessToken;
|
||||
|
||||
class UserRepository implements UserRepositoryInterface
|
||||
{
|
||||
use FiltersByDateRange;
|
||||
use HasMultipleSearchFilters;
|
||||
use PaginatesWithDeferredJoin;
|
||||
use ResolvesSortSpec;
|
||||
@@ -125,6 +128,8 @@ class UserRepository implements UserRepositoryInterface
|
||||
sort: $sort,
|
||||
perPage: $perPage,
|
||||
relations: ['roles'],
|
||||
// 회원 수가 커져도 목록 첫 화면 비용이 총 건수 COUNT 에 끌려가지 않도록 상한을 건다.
|
||||
resultCap: PaginationLimits::resultCap('admin.users'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -153,13 +158,13 @@ class UserRepository implements UserRepositoryInterface
|
||||
*/
|
||||
private function applyDateFilters(Builder $query, array $filters): void
|
||||
{
|
||||
if (! empty($filters['start_date'])) {
|
||||
$query->whereDate('created_at', '>=', $filters['start_date']);
|
||||
}
|
||||
|
||||
if (! empty($filters['end_date'])) {
|
||||
$query->whereDate('created_at', '<=', $filters['end_date']);
|
||||
}
|
||||
// whereDate 는 컬럼에 DATE() 를 씌워 인덱스를 무력화한다 — 범위 조건으로 준다.
|
||||
$this->applyDateRangeFilter(
|
||||
$query,
|
||||
'created_at',
|
||||
$filters['start_date'] ?? null,
|
||||
$filters['end_date'] ?? null
|
||||
);
|
||||
|
||||
// 기본 날짜 필터 (전체가 아닌 경우)
|
||||
if (empty($filters['start_date']) && empty($filters['end_date']) &&
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Search\Contracts;
|
||||
|
||||
use App\Search\DTO\KeywordSearchContext;
|
||||
use App\Search\KeywordSearch;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* 진행 중인 DB 쿼리에 키워드 술어를 붙일 수 있는 검색 엔진 계약
|
||||
*
|
||||
* Scout 의 `Model::search()` 는 "엔진이 결과를 돌려준다" 모델입니다. 그래서 결과를 받은
|
||||
* 뒤 그 ID 로 DB 를 다시 조회해야 하고, 매칭이 많으면 ID 전량을 메모리에 올려 무제한
|
||||
* `IN (...)` 을 만들게 됩니다. 페이지네이션·조인·필터를 DB 에 남긴 채 키워드 조건만
|
||||
* 얹으려면 **엔진에게 술어를 달라고 요청할 통로**가 따로 필요합니다.
|
||||
*
|
||||
* 이 계약이 그 통로입니다. 구현하는 엔진은 자기 방식(FULLTEXT MATCH, 외부 엔진이 미리
|
||||
* 좁혀 준 키 집합 등)으로 조건을 붙이고, 코어는 무엇이 붙는지 모르는 채 호출만 합니다.
|
||||
* 해석과 폴백은 {@see KeywordSearch} 가 단독으로 수행합니다.
|
||||
*
|
||||
* 구현하지 않은 엔진은 `LIKE` 폴백으로 내려갑니다 — 검색은 동작하지만 관련도 정렬이
|
||||
* 사라지고 전체 스캔이 되므로, 폴백 사실은 조용히 넘어가지 않고 기록됩니다.
|
||||
*
|
||||
* ```php
|
||||
* class MeilisearchEngine extends Engine implements KeywordPredicateProvider
|
||||
* {
|
||||
* public function applyKeywordPredicate(
|
||||
* Builder $query,
|
||||
* array $columns,
|
||||
* string $keyword,
|
||||
* string $boolean,
|
||||
* KeywordSearchContext $context
|
||||
* ): void {
|
||||
* // 상한을 지킨다 — 지키지 않으면 매칭이 큰 검색어에서 이 배열이 곧 메모리 폭발이다
|
||||
* $ids = $this->lookupIds($keyword, limit: $context->keyCap);
|
||||
* $method = $boolean === 'or' ? 'orWhereIn' : 'whereIn';
|
||||
* $query->$method($query->getModel()->getQualifiedKeyName(), $ids);
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
interface KeywordPredicateProvider
|
||||
{
|
||||
/**
|
||||
* 쿼리에 키워드 술어를 붙입니다.
|
||||
*
|
||||
* 컬럼 목록은 **하나의 조건으로 함께 평가**해 달라는 요청입니다. 엔진이 그 조합을
|
||||
* 다룰 수 없으면(예: MySQL 이 그 컬럼 조합의 복합 FULLTEXT 인덱스를 요구하는 경우)
|
||||
* 호출자가 {@see KeywordSearch::applyAny()} 로 컬럼별 OR 을 요청합니다.
|
||||
*
|
||||
* 매칭이 하나도 없을 수 있는 검색어(예: 연산자만 입력)에는 **항상 거짓인 조건**을
|
||||
* 붙여야 합니다. 아무 조건도 붙이지 않으면 필터 없는 전체 목록이 검색 결과로 나갑니다.
|
||||
*
|
||||
* **페이지네이션은 호출자(DB)가 담당합니다.** 엔진에게 페이지 번호를 넘기지 않는 이유는,
|
||||
* 엔진이 자기 순서로 한 페이지 분량만 돌려주면 그 뒤에 적용되는 DB 필터(분류·전시상태·
|
||||
* 조인)에 일부가 탈락해 페이지가 비고, DB 정렬과 엔진의 관련도 순서가 달라 페이지 경계도
|
||||
* 어긋나기 때문입니다. 엔진이 책임지는 것은 **"얼마까지 돌려줄 것인가"** 하나입니다.
|
||||
*
|
||||
* 키 집합을 만들어 조건으로 붙이는 구현은 `$context->keyCap` 을 반드시 지켜야 합니다.
|
||||
* 지키지 않으면 매칭이 큰 검색어에서 그 집합 자체가 메모리 폭발이 됩니다. 상한에 걸려
|
||||
* 잘린 경우 그 이상은 도달 불가이며, 총 건수는 "이상" 으로 보고됩니다
|
||||
* ({@see PaginationLimits} 와 같은 의미).
|
||||
*
|
||||
* @param Builder $query 조건을 붙일 Eloquent 쿼리 빌더
|
||||
* @param array<int, string> $columns 검색 대상 컬럼명
|
||||
* @param string $keyword 검색어 원문 (정제는 엔진이 수행)
|
||||
* @param string $boolean 기존 조건과의 결합 방식 ('and' 또는 'or')
|
||||
* @param KeywordSearchContext $context 술어 생성 조건 (키 집합 상한 등)
|
||||
*/
|
||||
public function applyKeywordPredicate(
|
||||
Builder $query,
|
||||
array $columns,
|
||||
string $keyword,
|
||||
string $boolean,
|
||||
KeywordSearchContext $context
|
||||
): void;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Search\DTO;
|
||||
|
||||
/**
|
||||
* 키워드 술어를 만들 때 엔진에게 전달되는 조건
|
||||
*
|
||||
* 엔진마다 술어를 만드는 방식이 다르고, 그중 일부는 **비용이 규모에 비례**합니다.
|
||||
* 외부 검색 서버를 쓰는 엔진은 자기 서버에서 키 집합을 받아 조건으로 붙이는데, 매칭이
|
||||
* 크면 그 키 집합 자체가 메모리 폭발이 됩니다 — 이 프로젝트가 한 번 겪은 결함입니다.
|
||||
*
|
||||
* 그래서 코어는 엔진에게 "얼마까지 가져와도 되는가" 를 함께 넘깁니다. 규정만으로는
|
||||
* 강제할 수 없으므로(외부 엔진 코드는 이 저장소 밖입니다) **값을 손에 쥐어 주는 것**까지가
|
||||
* 코어가 할 수 있는 최선입니다.
|
||||
*
|
||||
* 값 추가가 필요해질 때 계약 시그니처를 다시 깨지 않도록 객체로 감쌉니다.
|
||||
*/
|
||||
final class KeywordSearchContext
|
||||
{
|
||||
/**
|
||||
* @param int|null $keyCap 엔진이 돌려줄 수 있는 최대 키 개수 (null = 무제한)
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly ?int $keyCap = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 상한이 정해져 있는지 반환합니다.
|
||||
*
|
||||
* @return bool 상한이 있으면 true
|
||||
*/
|
||||
public function hasKeyCap(): bool
|
||||
{
|
||||
return $this->keyCap !== null && $this->keyCap > 0;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,13 @@
|
||||
|
||||
namespace App\Search\Engines;
|
||||
|
||||
use App\Enums\TotalRelation;
|
||||
use App\Search\Contracts\FulltextSearchable;
|
||||
use App\Search\Contracts\KeywordPredicateProvider;
|
||||
use App\Search\DTO\KeywordSearchContext;
|
||||
use App\Search\KeywordSearch;
|
||||
use App\Support\Query\BoundedPaginator;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -18,7 +24,7 @@ use Laravel\Scout\Engines\Engine;
|
||||
*
|
||||
* MySQL 자체가 인덱스 소스이므로 update/delete/flush는 no-op입니다.
|
||||
*/
|
||||
class DatabaseFulltextEngine extends Engine
|
||||
class DatabaseFulltextEngine extends Engine implements KeywordPredicateProvider
|
||||
{
|
||||
/**
|
||||
* MariaDB 감지 결과 캐시 (프로세스 수명 동안 유지)
|
||||
@@ -53,20 +59,52 @@ class DatabaseFulltextEngine extends Engine
|
||||
* FULLTEXT 검색을 수행합니다.
|
||||
*
|
||||
* @param Builder $builder Scout 빌더
|
||||
* @return array{query: \Illuminate\Database\Eloquent\Builder, total: int}
|
||||
* @return array{query: \Illuminate\Database\Eloquent\Builder|null, total: int|null, total_relation: TotalRelation|null, result_cap: int|null}
|
||||
*/
|
||||
public function search(Builder $builder): array
|
||||
{
|
||||
return $this->performSearch($builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 검색 결과의 키 목록만 조회합니다.
|
||||
*
|
||||
* 총 건수를 쓰지 않는 경로이므로 COUNT 를 아예 실행하지 않고, SELECT 도 키 컬럼과
|
||||
* 정렬용 스코어로만 좁힙니다. 좁히지 않으면 매칭된 전 행의 모든 컬럼(본문 포함)을
|
||||
* 읽고 나서 ID 만 뽑아내게 됩니다.
|
||||
*
|
||||
* @param Builder $builder Scout 빌더
|
||||
* @return \Illuminate\Support\Collection 키 컬렉션
|
||||
*/
|
||||
public function keys(Builder $builder): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->mapIds($this->performSearch($builder, withTotal: false, keysOnly: true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 검색 결과를 모델 컬렉션으로 조회합니다.
|
||||
*
|
||||
* 총 건수를 쓰지 않는 경로이므로 COUNT 를 실행하지 않습니다.
|
||||
*
|
||||
* @param Builder $builder Scout 빌더
|
||||
* @return Collection 모델 컬렉션
|
||||
*/
|
||||
public function get(Builder $builder): Collection
|
||||
{
|
||||
return $this->map(
|
||||
$builder,
|
||||
$builder->applyAfterRawSearchCallback($this->performSearch($builder, withTotal: false)),
|
||||
$builder->model
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 페이지네이션 적용 FULLTEXT 검색을 수행합니다.
|
||||
*
|
||||
* @param Builder $builder Scout 빌더
|
||||
* @param int $perPage 페이지당 결과 수
|
||||
* @param int $page 페이지 번호
|
||||
* @return array{query: \Illuminate\Database\Eloquent\Builder, total: int}
|
||||
* @return array{query: \Illuminate\Database\Eloquent\Builder|null, total: int|null, total_relation: TotalRelation|null, result_cap: int|null}
|
||||
*/
|
||||
public function paginate(Builder $builder, $perPage, $page): array
|
||||
{
|
||||
@@ -85,7 +123,13 @@ class DatabaseFulltextEngine extends Engine
|
||||
return collect();
|
||||
}
|
||||
|
||||
return $query->pluck($query->getModel()->getKeyName());
|
||||
// `pluck()` 은 이미 걸려 있는 SELECT 를 보존한다(onceWithColumns). 검색 쿼리는
|
||||
// 관련도 점수를 위해 `table.*` 를 선택해 두므로, 그대로 두면 ID 만 필요한 자리에서
|
||||
// 전 컬럼을 읽는다. 사본에서 키 컬럼만 남겨 좁힌다.
|
||||
$model = $query->getModel();
|
||||
$key = $model->getQualifiedKeyName();
|
||||
|
||||
return $query->clone()->select($key)->pluck($model->getKeyName());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +173,31 @@ class DatabaseFulltextEngine extends Engine
|
||||
*/
|
||||
public function getTotalCount($results): int
|
||||
{
|
||||
return $results['total'] ?? 0;
|
||||
return (int) ($results['total'] ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 검색 결과의 총 건수 정확도를 반환합니다.
|
||||
*
|
||||
* 상한을 넘겨 정확히 세지 않은 경우 `AtLeast` 입니다.
|
||||
*
|
||||
* @param array $results 검색 결과
|
||||
* @return TotalRelation 총 건수 정확도
|
||||
*/
|
||||
public function getTotalRelation($results): TotalRelation
|
||||
{
|
||||
return $results['total_relation'] ?? TotalRelation::Exact;
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수 집계에 적용된 상한을 반환합니다.
|
||||
*
|
||||
* @param array $results 검색 결과
|
||||
* @return int|null 상한 (무제한이면 null)
|
||||
*/
|
||||
public function getResultCap($results): ?int
|
||||
{
|
||||
return $results['result_cap'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,6 +307,14 @@ class DatabaseFulltextEngine extends Engine
|
||||
* MySQL 파싱 오류(ER_PARSE_ERROR)를 유발하므로, 사용자 입력을 일반 검색어로만
|
||||
* 취급하도록 연산자를 제거하고 남은 토큰을 따옴표 구문으로 묶습니다.
|
||||
*
|
||||
* 토큰은 공백으로 잇습니다 — BOOLEAN MODE 에서 공백 결합은 OR 입니다. 각 토큰에 `+` 를
|
||||
* 붙이면 AND 가 되어 매칭 집합이 줄고 대용량에서 검색 시간이 짧아질 여지가 있지만,
|
||||
* **의도적으로 OR 을 유지합니다**. 입력한 단어 중 하나만 든 문서가 결과에서 통째로
|
||||
* 사라지는 손실이 성능 이득보다 크기 때문입니다. 한글은 ngram 파서가 2글자 단위로
|
||||
* 토큰을 쪼개므로 AND 전환의 결과 축소 폭이 특히 큽니다. 성능 축이 문제가 되면 결합
|
||||
* 방식이 아니라 전용 검색 엔진(core.search.engine_drivers)으로 해결합니다.
|
||||
* 배경·실측: docs/backend/search-system.md, docs/backend/pagination.md
|
||||
*
|
||||
* @param string $keyword 원본 검색어
|
||||
* @return string 안전하게 정제된 BOOLEAN MODE 검색식 (정제 결과가 없으면 빈 문자열)
|
||||
*/
|
||||
@@ -267,32 +343,114 @@ class DatabaseFulltextEngine extends Engine
|
||||
*
|
||||
* DBMS별로 MATCH...AGAINST 또는 LIKE fallback을 자동 적용합니다.
|
||||
*
|
||||
* 검색어는 반드시 이 헬퍼를 거쳐야 합니다. BOOLEAN MODE 는 `+ - * " ( )` 등을
|
||||
* 연산자로 해석하므로, 원문 키워드를 그대로 바인딩하면 사용자가 `+` 하나만 입력해도
|
||||
* 파싱 오류로 500 이 됩니다. 정제는 sanitizeBooleanModeKeyword 한 곳에서만 합니다.
|
||||
*
|
||||
* **컬럼을 배열로 넘기면 하나의 `MATCH(a, b)` 가 됩니다.** MySQL 은 이 형태에 정확히
|
||||
* 그 컬럼 조합의 **복합 FULLTEXT 인덱스**를 요구하며, 없으면
|
||||
* `Can't find FULLTEXT index matching the column list` 오류가 납니다.
|
||||
* 컬럼별 단일 인덱스만 있는 테이블은 {@see self::whereFulltextAny()} 를 쓰세요.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query Eloquent 쿼리 빌더
|
||||
* @param string $column 검색 대상 컬럼명
|
||||
* @param string $keyword 검색어
|
||||
* @param string|array<int, string> $columns 검색 대상 컬럼명 (배열이면 복합 인덱스 필요)
|
||||
* @param string $keyword 검색어 (원문 — 정제는 이 메서드가 수행)
|
||||
* @param string $boolean 조건 결합 방식 ('and' 또는 'or')
|
||||
*/
|
||||
public static function whereFulltext(
|
||||
\Illuminate\Database\Eloquent\Builder $query,
|
||||
string $column,
|
||||
string|array $columns,
|
||||
string $keyword,
|
||||
string $boolean = 'and'
|
||||
): void {
|
||||
if (static::supportsFulltext()) {
|
||||
$ftKeyword = static::sanitizeBooleanModeKeyword($keyword);
|
||||
if ($ftKeyword === '') {
|
||||
// 정제 결과 없음 → 항상 false 조건으로 빈 결과 (void 반환 계약 유지)
|
||||
$falseMethod = $boolean === 'or' ? 'orWhereRaw' : 'whereRaw';
|
||||
$query->$falseMethod('1 = 0');
|
||||
// 활성 엔진 해석을 거친다 — 다른 엔진이 켜져 있으면 그 엔진이 조건을 만든다.
|
||||
// 이 정적 헬퍼를 직접 부르던 코드도 이 경유로 엔진 교체 혜택을 받는다.
|
||||
KeywordSearch::apply($query, $columns, $keyword, $boolean);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
$method = $boolean === 'or' ? 'orWhereRaw' : 'whereRaw';
|
||||
$query->$method("MATCH(`{$column}`) AGAINST(? IN BOOLEAN MODE)", [$ftKeyword]);
|
||||
} else {
|
||||
$method = $boolean === 'or' ? 'orWhere' : 'where';
|
||||
$query->$method($column, 'LIKE', "%{$keyword}%");
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* 이 엔진의 술어는 `MATCH ... AGAINST IN BOOLEAN MODE` 입니다. DBMS 가 FULLTEXT 를
|
||||
* 지원하지 않으면 같은 자리에서 부분일치로 내려갑니다.
|
||||
*
|
||||
* `$context->keyCap` 은 쓰지 않습니다 — 이 엔진의 술어는 SQL 조건 그 자체라 중간
|
||||
* 키 집합을 만들지 않으므로 상한을 적용할 대상이 없습니다. 상한은 키 집합을 만들어
|
||||
* 조건으로 붙이는 엔진(외부 검색 서버 등)을 위한 것입니다.
|
||||
*/
|
||||
public function applyKeywordPredicate(
|
||||
\Illuminate\Database\Eloquent\Builder $query,
|
||||
array $columns,
|
||||
string $keyword,
|
||||
string $boolean,
|
||||
KeywordSearchContext $context
|
||||
): void {
|
||||
static::applyFulltextPredicate($query, $columns, $keyword, $boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* FULLTEXT 조건을 실제로 조립합니다.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query Eloquent 쿼리 빌더
|
||||
* @param array<int, string> $columns 검색 대상 컬럼명
|
||||
* @param string $keyword 검색어 원문
|
||||
* @param string $boolean 조건 결합 방식 ('and' 또는 'or')
|
||||
*/
|
||||
protected static function applyFulltextPredicate(
|
||||
\Illuminate\Database\Eloquent\Builder $query,
|
||||
array $columns,
|
||||
string $keyword,
|
||||
string $boolean = 'and'
|
||||
): void {
|
||||
$columns = array_values($columns);
|
||||
|
||||
if (! static::supportsFulltext()) {
|
||||
// 전문검색을 제공하지 않는 DBMS 로 설치된 사이트에서는 부분일치가 정상 경로다.
|
||||
// 연산자 선택과 와일드카드 escape 는 코어 해석기가 단독으로 수행한다 —
|
||||
// 여기서 다시 조립하면 DBMS 가 늘 때마다 고쳐야 할 곳이 둘이 된다.
|
||||
KeywordSearch::applyLikeMatch($query, $columns, $keyword, $boolean);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$ftKeyword = static::sanitizeBooleanModeKeyword($keyword);
|
||||
|
||||
if ($ftKeyword === '') {
|
||||
// 정제 결과 없음(연산자만 입력) → 빈 결과. 빌더가 바인딩까지 처리하도록
|
||||
// 빈 whereIn 을 쓴다 (raw '1 = 0' 은 쓰지 않는다).
|
||||
$method = $boolean === 'or' ? 'orWhereIn' : 'whereIn';
|
||||
$query->$method($query->getModel()->getQualifiedKeyName(), []);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$match = 'MATCH(`'.implode('`, `', $columns).'`)';
|
||||
$method = $boolean === 'or' ? 'orWhereRaw' : 'whereRaw';
|
||||
$query->$method($match.' AGAINST(? IN BOOLEAN MODE)', [$ftKeyword]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 여러 컬럼 중 하나라도 매칭하면 되는 FULLTEXT 조건을 추가합니다.
|
||||
*
|
||||
* 컬럼마다 별도의 `MATCH(col)` 를 만들어 OR 로 묶습니다. 컬럼별 단일 FULLTEXT 인덱스만
|
||||
* 있는 테이블(대부분의 경우)에서 쓰는 형태이며, 복합 인덱스가 없어도 동작합니다.
|
||||
*
|
||||
* 복합 인덱스가 있어 `MATCH(a, b)` 한 번으로 끝내야 하는 테이블은
|
||||
* {@see self::whereFulltext()} 에 배열을 넘기세요.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query Eloquent 쿼리 빌더
|
||||
* @param array<int, string> $columns 검색 대상 컬럼명 목록
|
||||
* @param string $keyword 검색어 (원문 — 정제는 내부에서 수행)
|
||||
* @param string $boolean 바깥 쿼리와의 결합 방식 ('and' 또는 'or')
|
||||
*/
|
||||
public static function whereFulltextAny(
|
||||
\Illuminate\Database\Eloquent\Builder $query,
|
||||
array $columns,
|
||||
string $keyword,
|
||||
string $boolean = 'and'
|
||||
): void {
|
||||
// 활성 엔진 해석을 거친다 ({@see self::whereFulltext()} 와 동일한 이유).
|
||||
KeywordSearch::applyAny($query, $columns, $keyword, $boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -329,39 +487,37 @@ class DatabaseFulltextEngine extends Engine
|
||||
* @param Builder $builder Scout 빌더
|
||||
* @param int|null $perPage 페이지당 결과 수
|
||||
* @param int|null $page 페이지 번호
|
||||
* @return array{query: \Illuminate\Database\Eloquent\Builder, total: int}
|
||||
* @param bool $withTotal 총 건수를 계산할지 여부 (쓰지 않는 경로에서는 false)
|
||||
* @param bool $keysOnly 키 컬럼과 정렬용 스코어만 조회할지 여부
|
||||
* @return array{query: \Illuminate\Database\Eloquent\Builder|null, total: int|null, total_relation: TotalRelation|null, result_cap: int|null}
|
||||
*/
|
||||
protected function performSearch(Builder $builder, ?int $perPage = null, ?int $page = null): array
|
||||
{
|
||||
protected function performSearch(
|
||||
Builder $builder,
|
||||
?int $perPage = null,
|
||||
?int $page = null,
|
||||
bool $withTotal = true,
|
||||
bool $keysOnly = false
|
||||
): array {
|
||||
$model = $builder->model;
|
||||
$keyword = $builder->query;
|
||||
|
||||
// 빈 검색어인 경우 빈 결과 반환
|
||||
if (empty(trim($keyword))) {
|
||||
return [
|
||||
'query' => null,
|
||||
'total' => 0,
|
||||
];
|
||||
return self::emptyResult();
|
||||
}
|
||||
|
||||
$query = $model->newQuery();
|
||||
|
||||
// FulltextSearchable 인터페이스 구현 확인
|
||||
if (! ($model instanceof FulltextSearchable)) {
|
||||
return [
|
||||
'query' => null,
|
||||
'total' => 0,
|
||||
];
|
||||
return self::emptyResult();
|
||||
}
|
||||
|
||||
$columns = $model->searchableColumns();
|
||||
$weights = $model->searchableWeights();
|
||||
|
||||
if (empty($columns)) {
|
||||
return [
|
||||
'query' => null,
|
||||
'total' => 0,
|
||||
];
|
||||
return self::emptyResult();
|
||||
}
|
||||
|
||||
$useFulltext = static::supportsFulltext();
|
||||
@@ -375,10 +531,7 @@ class DatabaseFulltextEngine extends Engine
|
||||
$ftKeyword = static::sanitizeBooleanModeKeyword($keyword);
|
||||
if ($ftKeyword === '') {
|
||||
// 연산자만 입력 → 500 대신 빈 결과 (빈 검색어와 동일 반환 형태)
|
||||
return [
|
||||
'query' => null,
|
||||
'total' => 0,
|
||||
];
|
||||
return self::emptyResult();
|
||||
}
|
||||
|
||||
// MATCH...AGAINST WHERE 조건 생성 (MySQL, MariaDB)
|
||||
@@ -399,7 +552,7 @@ class DatabaseFulltextEngine extends Engine
|
||||
$scoreBindings[] = $ftKeyword;
|
||||
}
|
||||
$scoreRaw = '('.implode(' + ', $scoreExpressions).') as _ft_score';
|
||||
$query->selectRaw($qualifiedTable.'.*, '.$scoreRaw, $scoreBindings);
|
||||
$this->applySelect($query, $model, $qualifiedTable, $scoreRaw, $scoreBindings, $keysOnly);
|
||||
} else {
|
||||
// LIKE fallback (PostgreSQL, SQLite 등)
|
||||
$query->where(function ($q) use ($columns, $keyword) {
|
||||
@@ -409,7 +562,7 @@ class DatabaseFulltextEngine extends Engine
|
||||
});
|
||||
|
||||
// 스코어 고정 0 (관련성 순위 불가)
|
||||
$query->selectRaw($qualifiedTable.'.*, 0 as _ft_score');
|
||||
$this->applySelect($query, $model, $qualifiedTable, '0 as _ft_score', [], $keysOnly);
|
||||
}
|
||||
|
||||
// Scout Builder 콜백 적용 (추가 where 조건 등)
|
||||
@@ -459,8 +612,16 @@ class DatabaseFulltextEngine extends Engine
|
||||
$query->orderByDesc('_ft_score');
|
||||
}
|
||||
|
||||
// 전체 건수 계산 (페이지네이션 전)
|
||||
$total = $query->toBase()->getCountForPagination();
|
||||
// 전체 건수 계산 (페이지네이션 전).
|
||||
// 총 건수를 쓰지 않는 경로에서는 아예 세지 않는다 — 대용량 매칭에서 COUNT 한 번이
|
||||
// 조회 본체보다 비쌀 수 있다.
|
||||
$resultCap = PaginationLimits::resultCap('search');
|
||||
$total = null;
|
||||
$relation = null;
|
||||
|
||||
if ($withTotal) {
|
||||
[$total, $relation] = BoundedPaginator::countWithCap($query, $resultCap);
|
||||
}
|
||||
|
||||
// 페이지네이션 적용
|
||||
if ($perPage !== null) {
|
||||
@@ -468,11 +629,62 @@ class DatabaseFulltextEngine extends Engine
|
||||
$query->limit($perPage)->offset($offset);
|
||||
} elseif ($builder->limit !== null) {
|
||||
$query->limit($builder->limit);
|
||||
} elseif ($resultCap !== null) {
|
||||
// perPage 도 limit 도 없으면 LIMIT 이 아예 붙지 않아 매칭 전량을 PHP 로 끌어온다.
|
||||
// 상한을 걸어 한 요청이 읽는 행 수에 천장을 둔다.
|
||||
$query->limit($resultCap);
|
||||
}
|
||||
|
||||
return [
|
||||
'query' => $query,
|
||||
'total' => $total,
|
||||
'total_relation' => $relation,
|
||||
'result_cap' => $resultCap,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 조회 컬럼을 적용합니다.
|
||||
*
|
||||
* 키만 필요한 경로에서는 키 컬럼과 정렬용 스코어로 좁힙니다. 스코어는 `ORDER BY` 가
|
||||
* 참조하므로 좁힐 때도 함께 남겨야 합니다.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query 대상 쿼리
|
||||
* @param Model $model 기준 모델
|
||||
* @param string $qualifiedTable 프리픽스 포함 테이블명
|
||||
* @param string $scoreRaw 스코어 SELECT 표현식 (`... as _ft_score`)
|
||||
* @param array<int, mixed> $scoreBindings 스코어 표현식 바인딩
|
||||
* @param bool $keysOnly 키 컬럼만 조회할지 여부
|
||||
*/
|
||||
protected function applySelect(
|
||||
$query,
|
||||
Model $model,
|
||||
string $qualifiedTable,
|
||||
string $scoreRaw,
|
||||
array $scoreBindings,
|
||||
bool $keysOnly
|
||||
): void {
|
||||
if ($keysOnly) {
|
||||
$query->select($model->getQualifiedKeyName())->selectRaw($scoreRaw, $scoreBindings);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->selectRaw($qualifiedTable.'.*, '.$scoreRaw, $scoreBindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 검색이 성립하지 않을 때의 빈 결과 형태를 반환합니다.
|
||||
*
|
||||
* @return array{query: null, total: int, total_relation: TotalRelation, result_cap: null}
|
||||
*/
|
||||
protected static function emptyResult(): array
|
||||
{
|
||||
return [
|
||||
'query' => null,
|
||||
'total' => 0,
|
||||
'total_relation' => TotalRelation::Exact,
|
||||
'result_cap' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
namespace App\Search;
|
||||
|
||||
use App\Extension\HookManager;
|
||||
use App\Search\Contracts\KeywordPredicateProvider;
|
||||
use App\Search\DTO\KeywordSearchContext;
|
||||
use App\Support\Query\PaginationLimits;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laravel\Scout\EngineManager;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 진행 중인 DB 쿼리에 키워드 술어를 붙이는 단일 진입점
|
||||
*
|
||||
* 저장소는 "이 컬럼들로 이 키워드를 걸어라" 만 말하고, **어떤 엔진이 그 조건을 만드는지는
|
||||
* 알지 않는다.** 활성 Scout 엔진이 {@see KeywordPredicateProvider} 를 구현하면 그 엔진에게
|
||||
* 위임하고, 구현하지 않았으면 `LIKE` 로 내려간다.
|
||||
*
|
||||
* 이 지점이 없으면 저장소가 구체 엔진 클래스(예: `DatabaseFulltextEngine::whereFulltext()`)를
|
||||
* 직접 부르게 되고, 그 순간 플러그인이 등록한 검색 엔진은 **호출될 기회 자체가 없어진다** —
|
||||
* 오류도 경고도 없이 그 사이트의 검색만 조용히 다른 방식으로 동작한다.
|
||||
*
|
||||
* 확장이 자기 엔진을 등록하는 방법은 [search-system.md](../../docs/backend/search-system.md) 참조.
|
||||
*/
|
||||
final class KeywordSearch
|
||||
{
|
||||
/**
|
||||
* DBMS 별 부분일치 연산자 표를 조정하는 필터 훅
|
||||
*
|
||||
* 확장이 새 DBMS 의 연산자를 선언할 때 씁니다.
|
||||
*
|
||||
* ```php
|
||||
* HookManager::addFilter('core.search.like_operators', function (array $operators) {
|
||||
* $operators['somedb'] = 'imatch';
|
||||
*
|
||||
* return $operators;
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
public const LIKE_OPERATORS_FILTER = 'core.search.like_operators';
|
||||
|
||||
/**
|
||||
* 폴백 경고를 요청당 한 번만 남기기 위한 기록 (드라이버명 집합)
|
||||
*
|
||||
* @var array<string, true>
|
||||
*/
|
||||
private static array $warnedDrivers = [];
|
||||
|
||||
/**
|
||||
* 쿼리에 키워드 술어를 붙입니다.
|
||||
*
|
||||
* 컬럼을 배열로 넘기면 **하나의 조건으로 함께 평가**해 달라는 뜻입니다. 엔진이 그 조합을
|
||||
* 다룰 수 없는 경우(MySQL FULLTEXT 는 그 컬럼 조합의 복합 인덱스를 요구한다)에는
|
||||
* {@see self::applyAny()} 로 컬럼별 OR 을 요청하세요.
|
||||
*
|
||||
* @param Builder $query 조건을 붙일 Eloquent 쿼리 빌더
|
||||
* @param string|array<int, string> $columns 검색 대상 컬럼명
|
||||
* @param string $keyword 검색어 원문
|
||||
* @param string $boolean 기존 조건과의 결합 방식 ('and' 또는 'or')
|
||||
* @param string|null $limitContext 상한 해석에 쓸 목록 컨텍스트 이름 (예: 'search')
|
||||
*/
|
||||
public static function apply(
|
||||
Builder $query,
|
||||
string|array $columns,
|
||||
string $keyword,
|
||||
string $boolean = 'and',
|
||||
?string $limitContext = null
|
||||
): void {
|
||||
$columns = array_values((array) $columns);
|
||||
|
||||
if ($columns === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$provider = self::provider();
|
||||
|
||||
if ($provider !== null) {
|
||||
$provider->applyKeywordPredicate(
|
||||
$query,
|
||||
$columns,
|
||||
$keyword,
|
||||
$boolean,
|
||||
self::context($limitContext)
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self::applyLikeMatch($query, $columns, $keyword, $boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* 엔진에게 넘길 술어 생성 조건을 만듭니다.
|
||||
*
|
||||
* 키 집합 상한은 목록 총 건수 상한과 같은 값을 쓴다 — 두 상한이 갈라지면 "엔진이
|
||||
* 돌려준 건수" 와 "화면이 보고하는 총 건수" 가 서로 다른 근거를 갖게 된다.
|
||||
*
|
||||
* @param string|null $limitContext 상한 해석에 쓸 목록 컨텍스트 이름
|
||||
* @return KeywordSearchContext 술어 생성 조건
|
||||
*/
|
||||
private static function context(?string $limitContext): KeywordSearchContext
|
||||
{
|
||||
return new KeywordSearchContext(
|
||||
keyCap: PaginationLimits::resultCap($limitContext),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 컬럼별로 따로 평가해 OR 로 묶은 키워드 술어를 붙입니다.
|
||||
*
|
||||
* 컬럼마다 개별 인덱스만 있는 테이블에서 씁니다. 바깥은 하나의 그룹으로 감싸므로
|
||||
* 호출자의 기존 조건과 섞이지 않습니다.
|
||||
*
|
||||
* @param Builder $query 조건을 붙일 Eloquent 쿼리 빌더
|
||||
* @param array<int, string> $columns 검색 대상 컬럼명
|
||||
* @param string $keyword 검색어 원문
|
||||
* @param string $boolean 기존 조건과의 결합 방식 ('and' 또는 'or')
|
||||
* @param string|null $limitContext 상한 해석에 쓸 목록 컨텍스트 이름 (예: 'search')
|
||||
*/
|
||||
public static function applyAny(
|
||||
Builder $query,
|
||||
array $columns,
|
||||
string $keyword,
|
||||
string $boolean = 'and',
|
||||
?string $limitContext = null
|
||||
): void {
|
||||
$columns = array_values($columns);
|
||||
|
||||
if ($columns === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$method = $boolean === 'or' ? 'orWhere' : 'where';
|
||||
|
||||
$query->$method(function ($group) use ($columns, $keyword, $limitContext) {
|
||||
foreach ($columns as $index => $column) {
|
||||
self::apply($group, $column, $keyword, $index === 0 ? 'and' : 'or', $limitContext);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성 엔진이 키워드 술어를 제공하는지 확인하고 그 엔진을 반환합니다.
|
||||
*
|
||||
* 제공하지 않으면 null 을 돌려주고, 그 사실을 드라이버당 한 번 기록합니다.
|
||||
* 기록하지 않으면 "검색이 느리고 관련도가 이상하다" 는 증상만 남고 원인을 찾을 단서가
|
||||
* 없어진다 — 폴백은 정상 동작처럼 보이기 때문이다.
|
||||
*
|
||||
* @return KeywordPredicateProvider|null 술어를 제공하는 엔진 (없으면 null)
|
||||
*/
|
||||
private static function provider(): ?KeywordPredicateProvider
|
||||
{
|
||||
try {
|
||||
$engine = app(EngineManager::class)->engine();
|
||||
} catch (Throwable) {
|
||||
// 부팅 초기 등 엔진 해석이 불가한 상황 — 경고 없이 폴백한다.
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($engine instanceof KeywordPredicateProvider) {
|
||||
return $engine;
|
||||
}
|
||||
|
||||
self::warnFallbackOnce();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 폴백 사용 사실을 드라이버당 한 번 기록합니다.
|
||||
*/
|
||||
private static function warnFallbackOnce(): void
|
||||
{
|
||||
$driver = (string) config('scout.driver', 'mysql-fulltext');
|
||||
|
||||
if (isset(self::$warnedDrivers[$driver])) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::$warnedDrivers[$driver] = true;
|
||||
|
||||
Log::warning('활성 검색 엔진이 키워드 술어를 제공하지 않아 LIKE 로 검색합니다. 관련도 정렬이 적용되지 않고 전체 스캔이 발생합니다.', [
|
||||
'driver' => $driver,
|
||||
'contract' => KeywordPredicateProvider::class,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 부분일치(LIKE) 조건을 붙입니다.
|
||||
*
|
||||
* 이 경로는 "엔진이 없을 때의 임시방편" 이 아니다. 전문검색을 제공하지 않는 DBMS 로
|
||||
* 설치된 사이트에서는 **이것이 정상 검색 경로**이므로 이식성을 갖춰야 한다.
|
||||
*
|
||||
* - 대소문자: PostgreSQL 의 `LIKE` 는 대소문자를 구분한다. MySQL 은 기본 collation 이
|
||||
* 구분하지 않으므로, 드라이버별로 연산자를 갈라 **같은 검색어가 DBMS 에 따라 다른
|
||||
* 결과를 내지 않도록** 한다.
|
||||
* - 와일드카드: 검색어의 `%` `_` `\` 를 escape 해 이용자가 입력한 글자 그대로 찾는다.
|
||||
* escape 하지 않으면 `50%` 검색이 `50` 으로 시작하는 모든 행을 반환한다.
|
||||
*
|
||||
* @param Builder $query 조건을 붙일 Eloquent 쿼리 빌더
|
||||
* @param array<int, string> $columns 검색 대상 컬럼명
|
||||
* @param string $keyword 검색어 원문
|
||||
* @param string $boolean 기존 조건과의 결합 방식
|
||||
*/
|
||||
public static function applyLikeMatch(
|
||||
Builder $query,
|
||||
array $columns,
|
||||
string $keyword,
|
||||
string $boolean = 'and'
|
||||
): void {
|
||||
$columns = array_values($columns);
|
||||
|
||||
if ($columns === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$pattern = '%'.self::escapeLikeWildcards($keyword).'%';
|
||||
$operator = self::caseInsensitiveLikeOperator();
|
||||
$method = $boolean === 'or' ? 'orWhere' : 'where';
|
||||
|
||||
$query->$method(function ($nested) use ($columns, $pattern, $operator) {
|
||||
foreach ($columns as $index => $column) {
|
||||
$nested->{$index === 0 ? 'where' : 'orWhere'}($column, $operator, $pattern);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 대소문자를 구분하지 않는 부분일치 연산자를 드라이버별로 반환합니다.
|
||||
*
|
||||
* 드라이버명을 코드에 적지 않는다 — 앞으로 어떤 DBMS 가 공식 지원될지 정해져 있지
|
||||
* 않으므로, 코드에 박으면 DBMS 가 늘 때마다 코어를 고쳐야 한다. 표는 선언형 config
|
||||
* (`core.search.like_operators`)에 있고, 확장은 필터 훅으로 조정한다.
|
||||
*
|
||||
* @return string 사용할 비교 연산자
|
||||
*/
|
||||
private static function caseInsensitiveLikeOperator(): string
|
||||
{
|
||||
$default = (string) config('core.search.like_operator_default', 'like');
|
||||
|
||||
$operators = config('core.search.like_operators', []);
|
||||
$operators = HookManager::applyFilters(self::LIKE_OPERATORS_FILTER, $operators);
|
||||
|
||||
if (! is_array($operators)) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
try {
|
||||
$driver = DB::getDriverName();
|
||||
} catch (Throwable) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$operator = $operators[$driver] ?? $default;
|
||||
|
||||
return is_string($operator) && $operator !== '' ? $operator : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* `LIKE` 패턴에서 특수 의미를 갖는 문자를 escape 합니다.
|
||||
*
|
||||
* @param string $keyword 검색어 원문
|
||||
* @return string escape 된 검색어
|
||||
*/
|
||||
public static function escapeLikeWildcards(string $keyword): string
|
||||
{
|
||||
return str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $keyword);
|
||||
}
|
||||
|
||||
/**
|
||||
* 폴백 경고 기록을 초기화합니다 (테스트 전용).
|
||||
*/
|
||||
public static function forgetFallbackWarnings(): void
|
||||
{
|
||||
self::$warnedDrivers = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Search;
|
||||
|
||||
use App\Enums\TotalRelation;
|
||||
use App\Support\Query\BoundedCount;
|
||||
use App\Support\Query\BoundedPage;
|
||||
use App\Support\Query\KeysetPaginator;
|
||||
use Illuminate\Pagination\CursorPaginator;
|
||||
|
||||
/**
|
||||
* 검색 카테고리 응답 페이로드를 만드는 단일 지점
|
||||
*
|
||||
* 검색 결과는 컬렉션 리소스를 거치지 않고 리스너가 배열을 만들어 넘긴다. 그 조립을
|
||||
* 도메인마다 손으로 하면 응답 형태가 갈라지고, 나중에 필드가 하나 늘 때 어떤 화면은
|
||||
* 받고 어떤 화면은 못 받는다. 그 형태 확장이 조용히 깨지는 것을 막기 위해 키 구성을
|
||||
* 여기 한 곳에 둔다.
|
||||
*
|
||||
* offset 응답과 커서 응답은 채워지는 값만 다르고 **키 집합은 같다**. 화면이 두 형태를
|
||||
* 분기 없이 그릴 수 있어야 하기 때문이다.
|
||||
*/
|
||||
final class SearchCategoryPayload
|
||||
{
|
||||
/**
|
||||
* offset(페이지 번호) 방식 결과로 페이로드를 만듭니다.
|
||||
*
|
||||
* @param BoundedPage $page 상한 총 건수를 가진 페이지 결과
|
||||
* @param array<int, mixed> $items 화면에 실을 항목 (가공 완료 상태)
|
||||
* @param array<string, mixed> $extra 도메인 고유 필드 (available_boards 등)
|
||||
* @return array<string, mixed> 카테고리 페이로드
|
||||
*/
|
||||
public static function fromBounded(BoundedPage $page, array $items, array $extra = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'total' => $page->total(),
|
||||
'total_relation' => $page->totalRelation()->value,
|
||||
'total_is_exact' => $page->totalRelation()->isExact(),
|
||||
'result_cap' => $page->resultCap(),
|
||||
'last_page' => $page->lastPage(),
|
||||
'has_more_pages' => $page->hasMorePages(),
|
||||
// offset 응답에는 커서가 없다. 키 자체는 남겨 화면이 분기 없이 읽게 한다.
|
||||
'next_cursor' => null,
|
||||
'prev_cursor' => null,
|
||||
'items' => $items,
|
||||
], $extra);
|
||||
}
|
||||
|
||||
/**
|
||||
* 커서(키셋) 방식 결과로 페이로드를 만듭니다.
|
||||
*
|
||||
* 커서 응답에는 총 건수가 없으므로 건수는 별도 집계로 받습니다. 마지막 페이지 번호는
|
||||
* 커서 방식에 존재하지 않는 개념이라 언제나 null 이며, 화면은 그때 마지막 페이지
|
||||
* 점프만 감추고 "다음" 이동은 그대로 유지합니다.
|
||||
*
|
||||
* @param CursorPaginator $page 커서 페이지 결과
|
||||
* @param BoundedCount|null $count 총 건수 집계 (배지를 그리지 않으면 null)
|
||||
* @param array<int, mixed> $items 화면에 실을 항목 (가공 완료 상태)
|
||||
* @param array<string, mixed> $extra 도메인 고유 필드
|
||||
* @return array<string, mixed> 카테고리 페이로드
|
||||
*/
|
||||
public static function fromCursor(
|
||||
CursorPaginator $page,
|
||||
?BoundedCount $count,
|
||||
array $items,
|
||||
array $extra = []
|
||||
): array {
|
||||
$relation = $count?->totalRelation() ?? TotalRelation::AtLeast;
|
||||
|
||||
return array_merge([
|
||||
'total' => $count?->total() ?? count($items),
|
||||
'total_relation' => $relation->value,
|
||||
'total_is_exact' => $count !== null && $relation->isExact(),
|
||||
'result_cap' => $count?->resultCap(),
|
||||
// 커서 방식에는 마지막 페이지 번호가 없다 (총 건수를 알아도 계산하지 않는다).
|
||||
'last_page' => null,
|
||||
'has_more_pages' => $page->hasMorePages(),
|
||||
'next_cursor' => KeysetPaginator::nextCursor($page),
|
||||
'prev_cursor' => KeysetPaginator::previousCursor($page),
|
||||
'items' => $items,
|
||||
], $extra);
|
||||
}
|
||||
|
||||
/**
|
||||
* 목록 없이 건수만 필요한 자리(비활성 탭 배지)의 페이로드를 만듭니다.
|
||||
*
|
||||
* @param BoundedCount $count 총 건수 집계
|
||||
* @param array<string, mixed> $extra 도메인 고유 필드
|
||||
* @return array<string, mixed> 카테고리 페이로드
|
||||
*/
|
||||
public static function fromCountOnly(BoundedCount $count, array $extra = []): array
|
||||
{
|
||||
return array_merge($count->toArray(), [
|
||||
'last_page' => null,
|
||||
'has_more_pages' => false,
|
||||
'next_cursor' => null,
|
||||
'prev_cursor' => null,
|
||||
'items' => [],
|
||||
], $extra);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Search;
|
||||
|
||||
use App\Support\Query\KeysetPaginator;
|
||||
|
||||
/**
|
||||
* 검색 목록이 커서(키셋)로 응답할 수 있는지 판정하는 단일 지점
|
||||
*
|
||||
* 이 판정을 도메인마다 각자 구현하면 같은 규칙이 검색 모듈 수만큼 복제되고, 한 곳만
|
||||
* 고치면 다른 도메인은 조용히 옛 규칙으로 남는다. 코어가 규칙을 소유하고, 확장은
|
||||
* "내 정렬 이름이 어떤 실제 컬럼인가" 만 선언한다.
|
||||
*
|
||||
* 커서는 정렬 키를 WHERE 절 경계로 삼으므로 정렬 키가 전부 실제 컬럼이어야 한다.
|
||||
* 관련도순처럼 계산값(FULLTEXT 점수)으로 정렬하는 경우에는 쓸 수 없고 offset 을 유지한다.
|
||||
*/
|
||||
final class SearchPagePolicy
|
||||
{
|
||||
/**
|
||||
* 이 요청을 커서로 응답할지 판정합니다.
|
||||
*
|
||||
* 커서를 받은 요청만 커서로 처리하면 첫 커서가 만들어질 자리가 없다. 서버는 커서
|
||||
* 모드에서만 다음 커서를 내보내므로, 화면은 건넬 커서가 없어 영원히 offset 에 머문다.
|
||||
* 첫 페이지에 커서가 없는 것은 정상이므로 "커서 없음" 이 아니라 "시작점" 으로 읽는다.
|
||||
*
|
||||
* 다만 커서 없이 깊은 페이지를 직접 지목한 요청(주소로 열어 둔 딥링크·북마크)은
|
||||
* 그 페이지를 그대로 보여줘야 한다. 커서로 바꾸면 첫 페이지로 되돌아가 링크가
|
||||
* 가리키던 자리를 잃으므로, 그 경우에만 offset 을 유지한다.
|
||||
*
|
||||
* @param string|null $cursor 요청이 보낸 커서 (첫 페이지면 없음)
|
||||
* @param array<int, array{0: string, 1: string}> $sortKeys [[컬럼, 방향], ...]
|
||||
* @param array<int, string> $cursorColumns 이 도메인에서 커서로 허용한 실제 컬럼
|
||||
* @param int $page 요청이 지목한 페이지 번호 (없으면 1)
|
||||
* @return bool 커서로 응답할 수 있으면 true
|
||||
*/
|
||||
public static function usesCursor(?string $cursor, array $sortKeys, array $cursorColumns, int $page = 1): bool
|
||||
{
|
||||
if (! KeysetPaginator::supports($sortKeys, $cursorColumns)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($cursor !== null && $cursor !== '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $page <= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 정렬 이름을 정렬 키 배열로 바꿉니다.
|
||||
*
|
||||
* 확장이 선언한 `정렬이름 => [컬럼, 방향]` 맵을 코어가 해석합니다. 맵에 없는 이름은
|
||||
* 커서로 처리할 수 없는 정렬(관련도순 등)로 보고 빈 배열을 돌려주며,
|
||||
* {@see self::usesCursor()} 가 이를 "커서 불가" 로 판정합니다.
|
||||
*
|
||||
* @param string $sort 요청이 보낸 정렬 이름
|
||||
* @param array<string, array{0: string, 1: string}> $sortMap 정렬 이름 → [컬럼, 방향]
|
||||
* @return array<int, array{0: string, 1: string}> 정렬 키 배열 (해석 불가 시 빈 배열)
|
||||
*/
|
||||
public static function sortKeys(string $sort, array $sortMap): array
|
||||
{
|
||||
if (! array_key_exists($sort, $sortMap)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [$sortMap[$sort]];
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Contracts\Repositories\ActivityLogRepositoryInterface;
|
||||
use App\Extension\HookManager;
|
||||
use Illuminate\Contracts\Pagination\CursorPaginator;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -18,7 +19,7 @@ class ActivityLogService
|
||||
/**
|
||||
* ActivityLogService 생성자
|
||||
*
|
||||
* @param ActivityLogRepositoryInterface $repository 활동 로그 리포지토리
|
||||
* @param ActivityLogRepositoryInterface $repository 활동 로그 리포지토리
|
||||
*/
|
||||
public function __construct(
|
||||
private ActivityLogRepositoryInterface $repository
|
||||
@@ -27,8 +28,8 @@ class ActivityLogService
|
||||
/**
|
||||
* 특정 모델의 활동 로그 목록을 조회합니다.
|
||||
*
|
||||
* @param Model $model 대상 모델
|
||||
* @param array $filters 필터 조건
|
||||
* @param Model $model 대상 모델
|
||||
* @param array $filters 필터 조건
|
||||
* @return LengthAwarePaginator 페이지네이션된 로그 목록
|
||||
*/
|
||||
public function getLogsForModel(Model $model, array $filters = []): LengthAwarePaginator
|
||||
@@ -39,10 +40,12 @@ class ActivityLogService
|
||||
/**
|
||||
* 활동 로그 목록을 조회합니다.
|
||||
*
|
||||
* @param array $filters 필터 조건
|
||||
* @return LengthAwarePaginator 페이지네이션된 로그 목록
|
||||
* 요청에 `cursor` 가 있으면 키셋(커서) 방식으로 응답합니다.
|
||||
*
|
||||
* @param array $filters 필터 조건
|
||||
* @return LengthAwarePaginator|CursorPaginator 페이지네이션된 로그 목록
|
||||
*/
|
||||
public function getList(array $filters = []): LengthAwarePaginator
|
||||
public function getList(array $filters = []): LengthAwarePaginator|CursorPaginator
|
||||
{
|
||||
return $this->repository->getPaginated($filters);
|
||||
}
|
||||
@@ -50,7 +53,7 @@ class ActivityLogService
|
||||
/**
|
||||
* 활동 로그를 삭제합니다.
|
||||
*
|
||||
* @param int $id 삭제할 활동 로그 ID
|
||||
* @param int $id 삭제할 활동 로그 ID
|
||||
* @return bool 삭제 성공 여부
|
||||
*/
|
||||
public function delete(int $id): bool
|
||||
@@ -67,7 +70,7 @@ class ActivityLogService
|
||||
/**
|
||||
* 여러 활동 로그를 일괄 삭제합니다.
|
||||
*
|
||||
* @param array<int> $ids 삭제할 활동 로그 ID 목록
|
||||
* @param array<int> $ids 삭제할 활동 로그 ID 목록
|
||||
* @return int 삭제된 건수
|
||||
*/
|
||||
public function deleteMany(array $ids): int
|
||||
|
||||
@@ -356,6 +356,7 @@ class AuthService
|
||||
$userRole = $this->roleRepository->findByIdentifier('user');
|
||||
if ($userRole) {
|
||||
$user->roles()->sync([$userRole->id]);
|
||||
$user->flushPermissionCaches();
|
||||
}
|
||||
|
||||
$token = $user->createToken('auth-token', ['*'], $this->getTokenExpiresAt())->plainTextToken;
|
||||
|
||||
@@ -56,8 +56,14 @@ class LanguagePackRegistry
|
||||
return $this->activeCoreLocalesCache;
|
||||
}
|
||||
|
||||
$fromDb = $this->repository->getActiveCoreLocales();
|
||||
$merged = array_values(array_unique(array_merge(self::BUNDLED_CORE_LOCALES, $fromDb)));
|
||||
// 활성 언어팩 전체는 getActivePacks() 가 이미 한 번 적재해 캐시한다. 코어 로케일은
|
||||
// 그 컬렉션의 부분집합이므로 DB 를 다시 부르지 않고 여기서 걸러 낸다.
|
||||
// (부팅 경로에서 두 메서드가 모두 호출되므로, 재조회하면 요청마다 쿼리가 하나 더 는다)
|
||||
$fromPacks = $this->getActivePacks(LanguagePackScope::Core->value)
|
||||
->pluck('locale')
|
||||
->all();
|
||||
|
||||
$merged = array_values(array_unique(array_merge(self::BUNDLED_CORE_LOCALES, $fromPacks)));
|
||||
|
||||
return $this->activeCoreLocalesCache = $merged;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,12 @@ use App\Enums\NotificationLogStatus;
|
||||
use App\Extension\HookManager;
|
||||
use App\Models\NotificationLog;
|
||||
use App\Models\User;
|
||||
use Illuminate\Pagination\CursorPaginator;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* 알림 발송 이력 서비스
|
||||
*/
|
||||
class NotificationLogService
|
||||
{
|
||||
public function __construct(
|
||||
@@ -17,6 +21,9 @@ class NotificationLogService
|
||||
|
||||
/**
|
||||
* 발송 성공 로그를 기록합니다.
|
||||
*
|
||||
* @param array<string, mixed> $data 로그 데이터
|
||||
* @return NotificationLog 기록된 로그
|
||||
*/
|
||||
public function logSent(array $data): NotificationLog
|
||||
{
|
||||
@@ -33,6 +40,9 @@ class NotificationLogService
|
||||
|
||||
/**
|
||||
* 발송 실패 로그를 기록합니다.
|
||||
*
|
||||
* @param array<string, mixed> $data 로그 데이터
|
||||
* @return NotificationLog 기록된 로그
|
||||
*/
|
||||
public function logFailed(array $data): NotificationLog
|
||||
{
|
||||
@@ -49,6 +59,9 @@ class NotificationLogService
|
||||
|
||||
/**
|
||||
* 발송 건너뜀 로그를 기록합니다.
|
||||
*
|
||||
* @param array<string, mixed> $data 로그 데이터
|
||||
* @return NotificationLog 기록된 로그
|
||||
*/
|
||||
public function logSkipped(array $data): NotificationLog
|
||||
{
|
||||
@@ -65,6 +78,9 @@ class NotificationLogService
|
||||
|
||||
/**
|
||||
* 로그를 삭제합니다.
|
||||
*
|
||||
* @param NotificationLog $log 삭제할 로그
|
||||
* @return bool 삭제 성공 여부
|
||||
*/
|
||||
public function deleteLog(NotificationLog $log): bool
|
||||
{
|
||||
@@ -79,6 +95,9 @@ class NotificationLogService
|
||||
|
||||
/**
|
||||
* 다건 삭제합니다.
|
||||
*
|
||||
* @param array<int, int> $ids 삭제할 로그 ID 목록
|
||||
* @return int 삭제된 건수
|
||||
*/
|
||||
public function bulkDelete(array $ids): int
|
||||
{
|
||||
@@ -94,9 +113,12 @@ class NotificationLogService
|
||||
/**
|
||||
* 페이지네이션 목록을 조회합니다.
|
||||
*
|
||||
* @param array<string, mixed> $filters 필터 조건
|
||||
* @param int $perPage 페이지당 건수
|
||||
* @param User|null $user 스코프 적용 대상 사용자 (null이면 스코프 미적용)
|
||||
* @return LengthAwarePaginator|CursorPaginator 페이지 결과 (커서 요청 시 키셋)
|
||||
*/
|
||||
public function getLogs(array $filters = [], int $perPage = 20, ?User $user = null): LengthAwarePaginator
|
||||
public function getLogs(array $filters = [], int $perPage = 20, ?User $user = null): LengthAwarePaginator|CursorPaginator
|
||||
{
|
||||
return $this->repository->getPaginated($filters, $perPage, $user);
|
||||
}
|
||||
|
||||
@@ -88,6 +88,7 @@ class UserService
|
||||
// 역할 동기화
|
||||
if ($roleIds !== null && count($roleIds) > 0) {
|
||||
$user->roles()->sync($roleIds);
|
||||
$user->flushPermissionCaches();
|
||||
}
|
||||
|
||||
// After 훅: 사용자 객체와 원본 데이터 전달
|
||||
@@ -196,6 +197,7 @@ class UserService
|
||||
// 역할 동기화
|
||||
if ($roleIds !== null) {
|
||||
$user->roles()->sync($roleIds);
|
||||
$user->flushPermissionCaches();
|
||||
}
|
||||
|
||||
// After 훅: 사용자 객체와 원본 데이터, 스냅샷 전달
|
||||
@@ -313,6 +315,7 @@ class UserService
|
||||
|
||||
// 역할 연결 해제 (명시적 삭제 - CASCADE 의존 금지)
|
||||
$user->roles()->detach();
|
||||
$user->flushPermissionCaches();
|
||||
|
||||
// 약관 동의 이력 삭제
|
||||
$user->consents()->delete();
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Enums\PermissionType;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
|
||||
/**
|
||||
* 비회원(guest) 역할 해석기
|
||||
*
|
||||
* 비회원 권한 판정은 한 요청에서 수십 번 일어난다 — 미들웨어, Gate, 리소스가 각각 묻는다.
|
||||
* 매번 역할과 권한을 다시 조회하지 않도록 요청당 한 번만 적재하고 그 결과를 공유한다.
|
||||
*
|
||||
* 캐시 수명은 **요청 스코프**다. 프로세스 정적으로 두면 상주형 실행 환경에서 권한을
|
||||
* 바꿔도 그 프로세스가 살아 있는 동안 예전 권한으로 판정한다 — 권한 변경 즉시 반영이
|
||||
* 깨지므로 그렇게 하지 않는다.
|
||||
*
|
||||
* 미들웨어와 Gate 가 각자 같은 조회를 들고 있으면 한쪽만 고쳐졌을 때 두 경로의 판정이
|
||||
* 갈린다. 그래서 해석은 이 클래스 한 곳에서만 한다.
|
||||
*/
|
||||
class GuestRoleResolver
|
||||
{
|
||||
/**
|
||||
* 해석 결과를 담는 요청 속성 키
|
||||
*/
|
||||
private const CACHE_KEY = '_guest_role_cache';
|
||||
|
||||
/**
|
||||
* guest 역할을 권한과 함께 조회합니다 (요청당 1회).
|
||||
*
|
||||
* @return Role|null guest 역할 (미정의 시 null)
|
||||
*/
|
||||
public static function resolve(): ?Role
|
||||
{
|
||||
$request = request();
|
||||
|
||||
if ($request->attributes->has(self::CACHE_KEY)) {
|
||||
return $request->attributes->get(self::CACHE_KEY);
|
||||
}
|
||||
|
||||
$role = Role::where('identifier', 'guest')
|
||||
->with('permissions')
|
||||
->first();
|
||||
|
||||
$request->attributes->set(self::CACHE_KEY, $role);
|
||||
|
||||
return $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* guest 역할이 특정 권한을 보유하는지 판정합니다.
|
||||
*
|
||||
* 적재해 둔 권한 컬렉션에서 판정하므로 판정마다 쿼리가 나가지 않습니다.
|
||||
*
|
||||
* @param string $identifier 권한 식별자
|
||||
* @param PermissionType|null $type 권한 타입 (null 이면 타입 무관)
|
||||
* @return bool 보유 여부
|
||||
*/
|
||||
public static function hasPermission(string $identifier, ?PermissionType $type = null): bool
|
||||
{
|
||||
$role = self::resolve();
|
||||
|
||||
if (! $role) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($role->permissions as $permission) {
|
||||
if ($permission->identifier !== $identifier) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($type === null || self::permissionType($permission) === $type) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 캐시를 비웁니다.
|
||||
*
|
||||
* 권한 재시드(코어 업데이트 / 확장 설치) 직후처럼 같은 요청 안에서 권한 구성이
|
||||
* 바뀌는 경우에 호출합니다.
|
||||
*/
|
||||
public static function flush(): void
|
||||
{
|
||||
request()->attributes->remove(self::CACHE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한의 타입을 Enum 으로 정규화합니다.
|
||||
*
|
||||
* @param Permission $permission 권한 모델
|
||||
* @return PermissionType|null 정규화된 타입 (해석 불가 시 null)
|
||||
*/
|
||||
private static function permissionType($permission): ?PermissionType
|
||||
{
|
||||
$type = $permission->type;
|
||||
|
||||
if ($type instanceof PermissionType) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
return $type === null ? null : PermissionType::tryFrom((string) $type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Query;
|
||||
|
||||
use App\Contracts\Pagination\BoundedTotalAware;
|
||||
use App\Enums\TotalRelation;
|
||||
|
||||
/**
|
||||
* 상한을 건 총 건수 집계 결과
|
||||
*
|
||||
* 목록을 조회하지 않고 건수만 필요한 자리(탭 배지, 요약 수치)를 위한 값 객체다.
|
||||
*
|
||||
* `int` 하나만 돌려주면 상한에 걸려 잘린 값과 정확히 센 값이 구분되지 않는다.
|
||||
* 잘린 10,000 이 "정확히 10,000 건" 으로 화면에 나가는 것은 오류로 드러나지 않고
|
||||
* 그냥 틀린 숫자로만 보이므로, 건수와 정확도를 한 덩어리로 옮긴다.
|
||||
*
|
||||
* @see BoundedPaginator::count() 이 값을 만드는 유일한 자리
|
||||
*/
|
||||
final class BoundedCount implements BoundedTotalAware
|
||||
{
|
||||
/**
|
||||
* @param int $total 총 건수 (정확도가 AtLeast 면 "이 값 이상")
|
||||
* @param TotalRelation $relation 총 건수 정확도
|
||||
* @param int|null $resultCap 집계에 적용된 상한 (null 이면 무제한)
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $total,
|
||||
private readonly TotalRelation $relation,
|
||||
private readonly ?int $resultCap,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 총 건수 정확도를 반환합니다.
|
||||
*
|
||||
* @return TotalRelation 정확도
|
||||
*/
|
||||
public function totalRelation(): TotalRelation
|
||||
{
|
||||
return $this->relation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 집계에 적용된 상한을 반환합니다.
|
||||
*
|
||||
* @return int|null 상한 (무제한이면 null)
|
||||
*/
|
||||
public function resultCap(): ?int
|
||||
{
|
||||
return $this->resultCap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 상한에 걸려 잘렸는지 여부를 반환합니다.
|
||||
*
|
||||
* @return bool 잘렸으면 true
|
||||
*/
|
||||
public function isTruncated(): bool
|
||||
{
|
||||
return ! $this->relation->isExact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수를 반환합니다.
|
||||
*
|
||||
* @return int 총 건수
|
||||
*/
|
||||
public function total(): int
|
||||
{
|
||||
return $this->total;
|
||||
}
|
||||
|
||||
/**
|
||||
* 응답에 실을 정확도 필드 묶음을 반환합니다.
|
||||
*
|
||||
* 배지·요약 자리마다 같은 키를 손으로 조립하면 한 군데만 빠져도
|
||||
* 그 화면에서만 잘린 값이 정확한 것처럼 나간다.
|
||||
*
|
||||
* @return array{total: int, total_relation: string, total_is_exact: bool, result_cap: int|null}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'total' => $this->total,
|
||||
'total_relation' => $this->relation->value,
|
||||
'total_is_exact' => $this->relation->isExact(),
|
||||
'result_cap' => $this->resultCap,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Query;
|
||||
|
||||
use App\Contracts\Pagination\BoundedTotalAware;
|
||||
use App\Enums\TotalRelation;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* 상한 집계 기반 페이지 결과
|
||||
*
|
||||
* 한 페이지의 조회 결과와 그 총 건수의 정확도를 함께 담는 불변 값이다. 생성 후 상태를
|
||||
* 바꾸는 메서드를 두지 않으며, 값은 전적으로 {@see BoundedPaginator} 가 채운다.
|
||||
*
|
||||
* 표준 `LengthAwarePaginator` 를 상속하는 이유는 재사용 때문이다. 기존 컬렉션·리소스·
|
||||
* 컨트롤러는 전부 페이지네이터 인터페이스에 맞춰져 있으므로, 별도 타입을 새로 정의하면
|
||||
* 소비 지점마다 언랩 코드가 생긴다. 상속하면 기존 컬렉션이 코드 변경 없이 그대로 받고,
|
||||
* {@see BoundedTotalAware} 구현 덕에 정확도 메타까지 자동으로 얻는다.
|
||||
*
|
||||
* 표준 페이지네이터와 다른 점은 셋뿐이다:
|
||||
* - `total()` 은 상한 이하일 때만 실제 건수다 (초과 시 상한값 = 하한 보증)
|
||||
* - `lastPage()` 는 총 건수가 부정확하면 null 이다 (마지막 페이지 점프 계산 불가)
|
||||
* - `hasMorePages()` 는 총 건수가 아니라 `per_page + 1` 실측으로 판정한다
|
||||
*/
|
||||
class BoundedPage extends LengthAwarePaginator implements BoundedTotalAware
|
||||
{
|
||||
/**
|
||||
* 총 건수와 실제 매칭 건수의 관계
|
||||
*/
|
||||
private TotalRelation $totalRelation;
|
||||
|
||||
/**
|
||||
* 총 건수 집계에 적용된 상한 (무제한이면 null)
|
||||
*/
|
||||
private ?int $resultCap;
|
||||
|
||||
/**
|
||||
* 다음 페이지 존재 여부 (per_page + 1 실측 결과)
|
||||
*/
|
||||
private bool $probedHasMorePages;
|
||||
|
||||
/**
|
||||
* @param Collection<int, mixed>|array<int, mixed> $items 현재 페이지 항목
|
||||
* @param int $total 총 건수 (상한 초과 시 상한값)
|
||||
* @param int $perPage 페이지당 건수
|
||||
* @param int $currentPage 현재 페이지 번호
|
||||
* @param TotalRelation $totalRelation 총 건수 정확도
|
||||
* @param int|null $resultCap 적용된 상한 (무제한이면 null)
|
||||
* @param bool $hasMorePages 다음 페이지 존재 여부 (per_page + 1 실측)
|
||||
* @param array<string, mixed> $options 페이지네이터 옵션 (path, pageName 등)
|
||||
*/
|
||||
public function __construct(
|
||||
$items,
|
||||
int $total,
|
||||
int $perPage,
|
||||
int $currentPage,
|
||||
TotalRelation $totalRelation,
|
||||
?int $resultCap,
|
||||
bool $hasMorePages,
|
||||
array $options = []
|
||||
) {
|
||||
parent::__construct($items, $total, $perPage, $currentPage, $options);
|
||||
|
||||
$this->totalRelation = $totalRelation;
|
||||
$this->resultCap = $resultCap;
|
||||
$this->probedHasMorePages = $hasMorePages;
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수와 실제 매칭 건수의 관계를 반환합니다.
|
||||
*
|
||||
* @return TotalRelation 정확(Exact) 또는 하한(AtLeast)
|
||||
*/
|
||||
public function totalRelation(): TotalRelation
|
||||
{
|
||||
return $this->totalRelation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수 집계에 적용된 상한을 반환합니다.
|
||||
*
|
||||
* @return int|null 상한 (무제한이면 null)
|
||||
*/
|
||||
public function resultCap(): ?int
|
||||
{
|
||||
return $this->resultCap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수가 상한에 걸려 잘렸는지 여부를 반환합니다.
|
||||
*
|
||||
* @return bool 잘렸으면 true
|
||||
*/
|
||||
public function isTruncated(): bool
|
||||
{
|
||||
return ! $this->totalRelation->isExact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 마지막 페이지 번호를 반환합니다.
|
||||
*
|
||||
* 총 건수가 부정확하면 마지막 페이지를 계산할 수 없으므로 null 을 반환합니다.
|
||||
* 화면은 이 값이 null 이면 마지막 페이지 점프 버튼을 감춥니다.
|
||||
*
|
||||
* @return int|null 마지막 페이지 번호 (총 건수 부정확 시 null)
|
||||
*/
|
||||
public function lastPage(): ?int
|
||||
{
|
||||
return $this->isTruncated() ? null : $this->lastPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 다음 페이지 존재 여부를 반환합니다.
|
||||
*
|
||||
* 총 건수를 몰라도 `per_page + 1` 조회로 정확히 판정되므로, 상한에 걸린 상태에서도
|
||||
* "다음" 이동은 끝까지 열려 있습니다.
|
||||
*
|
||||
* @return bool 다음 페이지가 있으면 true
|
||||
*/
|
||||
public function hasMorePages(): bool
|
||||
{
|
||||
return $this->probedHasMorePages;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Query;
|
||||
|
||||
use App\Enums\TotalRelation;
|
||||
use Illuminate\Contracts\Database\Query\Builder as BuilderContract;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 상한을 건 총 건수 집계 페이지네이터
|
||||
*
|
||||
* 임의의 빌더를 받아 한 페이지를 조회하고, 총 건수는 상한까지만 센다. 검색이나 특정
|
||||
* 도메인에 대한 지식이 없으므로 어떤 목록에서도 쓸 수 있다.
|
||||
*
|
||||
* 표준 `paginate()` 와 다른 점 둘:
|
||||
*
|
||||
* 1. **총 건수만 상한을 받는다.** `SELECT COUNT(*) FROM (SELECT 1 FROM ... LIMIT cap+1) t`
|
||||
* 로 감싸므로, 상한 이하면 표준 `COUNT(*)` 와 값이 같고 초과할 때만 "이상" 이 된다.
|
||||
* 2. **다음 페이지 판정은 총 건수와 무관하다.** `per_page + 1` 건을 읽어 초과분 존재
|
||||
* 여부로 판정하므로, 총 건수를 몰라도 "다음" 이동은 끝까지 정확하다.
|
||||
*
|
||||
* 상한을 넘겨 잘린 경우에도 페이지 이동이 막히지 않는다. 계산이 불가능해지는 것은
|
||||
* 마지막 페이지 번호 하나뿐이며, 그 사실은 {@see BoundedPage::lastPage()} 가 null 로 알린다.
|
||||
*
|
||||
* **입력 범위는 표준 `paginate()` 와 같아야 한다.** Eloquent 빌더뿐 아니라 쿼리 빌더
|
||||
* (`DB::table(...)`)와 관계(`$user->notifications()`)도 받는다. 관계에서 `->paginate()` 가
|
||||
* 되는 이유는 관계가 빌더로 호출을 전달해 주기 때문인데, 정적 메서드는 그 전달을 받지
|
||||
* 못한다. 그래서 여기서 직접 이해한다 — 이 입구가 표준보다 좁으면, 표준을 이 계약으로
|
||||
* 바꾸는 것만으로 멀쩡하던 목록이 TypeError 로 죽는다.
|
||||
*/
|
||||
class BoundedPaginator
|
||||
{
|
||||
/**
|
||||
* 관계를 그 밑의 빌더로 환원합니다.
|
||||
*
|
||||
* 관계의 소속 조건(`where user_id = ?` 등)은 관계 생성 시점에 빌더에 이미 들어가 있어
|
||||
* 그대로 보존된다.
|
||||
*
|
||||
* @param EloquentBuilder|QueryBuilder|Relation $query 대상
|
||||
* @return EloquentBuilder|QueryBuilder 빌더
|
||||
*/
|
||||
private static function toBuilder(EloquentBuilder|QueryBuilder|Relation $query): EloquentBuilder|QueryBuilder
|
||||
{
|
||||
return $query instanceof Relation ? $query->getQuery() : $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 한 페이지를 조회하고 상한 총 건수를 함께 계산합니다.
|
||||
*
|
||||
* @param EloquentBuilder|QueryBuilder|Relation $query 대상 빌더/관계 (정렬·필터가 이미 적용된 상태)
|
||||
* @param int $perPage 페이지당 건수
|
||||
* @param int|null $page 현재 페이지 번호 (null 이면 요청에서 해석)
|
||||
* @param int|null $resultCap 총 건수 집계 상한 (null 이면 무제한 = 정확한 COUNT)
|
||||
* @param array<int, string> $columns 조회 컬럼
|
||||
* @param string $pageName 페이지 쿼리 파라미터 이름
|
||||
* @return BoundedPage 페이지 결과 (총 건수 정확도 포함)
|
||||
*/
|
||||
public static function paginate(
|
||||
EloquentBuilder|QueryBuilder|Relation $query,
|
||||
int $perPage,
|
||||
?int $page = null,
|
||||
?int $resultCap = null,
|
||||
array $columns = ['*'],
|
||||
string $pageName = 'page'
|
||||
): BoundedPage {
|
||||
$query = self::toBuilder($query);
|
||||
|
||||
$page = $page ?: Paginator::resolveCurrentPage($pageName);
|
||||
$page = max(1, $page);
|
||||
$perPage = max(1, $perPage);
|
||||
|
||||
// per_page + 1 건을 읽어 다음 페이지 존재 여부를 실측한다.
|
||||
// 총 건수 상한과 무관하게 항상 정확하다. 호출자의 빌더는 건드리지 않는다.
|
||||
//
|
||||
// offset 은 per_page 기준으로 계산한다. forPage($page, $perPage + 1) 을 쓰면
|
||||
// offset 까지 per_page + 1 배수가 되어 페이지가 깊어질수록 경계가 밀린다.
|
||||
$items = (clone $query)
|
||||
->offset(($page - 1) * $perPage)
|
||||
->limit($perPage + 1)
|
||||
->get($columns);
|
||||
|
||||
$hasMorePages = $items->count() > $perPage;
|
||||
|
||||
if ($hasMorePages) {
|
||||
$items = $items->slice(0, $perPage)->values();
|
||||
}
|
||||
|
||||
[$total, $relation] = self::countWithCap($query, $resultCap);
|
||||
|
||||
// 상한 이하인데 이번 페이지가 상한 경계를 넘겨 조회된 경우를 방어한다.
|
||||
// (동시 삽입으로 카운트 시점과 조회 시점의 모수가 다를 수 있다)
|
||||
$seenSoFar = ($page - 1) * $perPage + $items->count();
|
||||
if ($total < $seenSoFar) {
|
||||
$total = $seenSoFar;
|
||||
}
|
||||
|
||||
return new BoundedPage(
|
||||
items: $items,
|
||||
total: $total,
|
||||
perPage: $perPage,
|
||||
currentPage: $page,
|
||||
totalRelation: $relation,
|
||||
resultCap: $resultCap,
|
||||
hasMorePages: $hasMorePages,
|
||||
options: [
|
||||
'path' => Paginator::resolveCurrentPath(),
|
||||
'pageName' => $pageName,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 목록 조회 없이 상한을 건 총 건수만 셉니다.
|
||||
*
|
||||
* 탭 배지처럼 건수만 필요한 자리에서 쓴다. {@see countWithCap()} 과 같은 쿼리를
|
||||
* 실행하지만 정확도를 값에 담아 돌려주므로, 잘린 값이 정확한 것처럼 화면에
|
||||
* 나가는 일이 생기지 않는다.
|
||||
*
|
||||
* @param EloquentBuilder|QueryBuilder|Relation $query 대상 빌더/관계
|
||||
* @param int|null $resultCap 상한 (null 이면 무제한 = 정확한 COUNT)
|
||||
* @return BoundedCount 건수 + 정확도
|
||||
*/
|
||||
public static function count(EloquentBuilder|QueryBuilder|Relation $query, ?int $resultCap): BoundedCount
|
||||
{
|
||||
[$total, $relation] = self::countWithCap($query, $resultCap);
|
||||
|
||||
return new BoundedCount($total, $relation, $resultCap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 상한을 건 총 건수를 셉니다.
|
||||
*
|
||||
* 상한을 넘는지만 알면 되므로 `LIMIT cap + 1` 로 감싼 파생 테이블을 센다.
|
||||
* 결과가 `cap + 1` 이면 실제 건수는 그 이상이므로 상한값을 하한으로 보고한다.
|
||||
*
|
||||
* `GROUP BY` 가 걸린 쿼리도 파생 테이블 안에서 그룹이 만들어지므로 그룹 수가
|
||||
* 그대로 센다 (표준 `count()` 가 그룹별 건수를 돌려주는 문제가 없다).
|
||||
*
|
||||
* @param EloquentBuilder|QueryBuilder|Relation $query 대상 빌더/관계
|
||||
* @param int|null $resultCap 상한 (null 이면 무제한 = 정확한 COUNT)
|
||||
* @return array{0: int, 1: TotalRelation} [총 건수, 정확도]
|
||||
*/
|
||||
public static function countWithCap(EloquentBuilder|QueryBuilder|Relation $query, ?int $resultCap): array
|
||||
{
|
||||
$base = self::toCountableBase(self::toBuilder($query));
|
||||
|
||||
if ($resultCap === null || $resultCap <= 0) {
|
||||
return [$base->getCountForPagination(), TotalRelation::Exact];
|
||||
}
|
||||
|
||||
$bounded = $base->limit($resultCap + 1);
|
||||
|
||||
// newQuery() 는 같은 커넥션의 빈 빌더를 만든다. fromSub 가 바인딩까지 옮겨 주므로
|
||||
// 서브쿼리 SQL 을 문자열로 조립하거나 mergeBindings 를 부를 필요가 없다.
|
||||
$counted = (int) $bounded->newQuery()
|
||||
->fromSub($bounded, 'g7_bounded_total')
|
||||
->count();
|
||||
|
||||
return $counted > $resultCap
|
||||
? [$resultCap, TotalRelation::AtLeast]
|
||||
: [$counted, TotalRelation::Exact];
|
||||
}
|
||||
|
||||
/**
|
||||
* 총 건수 집계용 기본 빌더를 만듭니다.
|
||||
*
|
||||
* 정렬은 건수에 영향을 주지 않으므로 제거하고, `DISTINCT`·`GROUP BY` 가 없으면
|
||||
* SELECT 목록을 상수로 좁혀 불필요한 컬럼 읽기를 없앤다.
|
||||
*
|
||||
* @param EloquentBuilder|QueryBuilder $query 대상 빌더
|
||||
* @return QueryBuilder 집계용 기본 빌더 (원본 불변)
|
||||
*/
|
||||
private static function toCountableBase(EloquentBuilder|QueryBuilder $query): QueryBuilder
|
||||
{
|
||||
// toBase() 는 Eloquent 빌더에만 있다. 이 클래스는 쿼리 빌더도 받겠다고 선언했으므로
|
||||
// (supports() 가 그것을 허용한다) 종류를 보고 갈라야 한다 — 그러지 않으면
|
||||
// `DB::table(...)` 을 그대로 넘긴 호출이 BadMethodCallException 으로 죽는다.
|
||||
$clone = clone $query;
|
||||
$base = $clone instanceof EloquentBuilder ? $clone->toBase() : $clone;
|
||||
|
||||
$base->reorder();
|
||||
$base->limit(null)->offset(null);
|
||||
|
||||
// DISTINCT / GROUP BY 는 어떤 컬럼을 세는지가 결과를 좌우하므로 SELECT 를 건드리지 않는다.
|
||||
if (! $base->distinct && empty($base->groups)) {
|
||||
$base->select(DB::raw('1'));
|
||||
}
|
||||
|
||||
return $base;
|
||||
}
|
||||
|
||||
/**
|
||||
* 빌더가 상한 집계에 쓸 수 있는 형태인지 확인합니다.
|
||||
*
|
||||
* @param mixed $query 검사 대상
|
||||
* @return bool 사용 가능하면 true
|
||||
*/
|
||||
public static function supports(mixed $query): bool
|
||||
{
|
||||
return $query instanceof EloquentBuilder
|
||||
|| $query instanceof QueryBuilder
|
||||
|| $query instanceof Relation
|
||||
|| $query instanceof BuilderContract;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Query;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use Illuminate\Pagination\Cursor;
|
||||
use Illuminate\Pagination\CursorPaginator;
|
||||
|
||||
/**
|
||||
* 커서(키셋) 기반 페이지네이터
|
||||
*
|
||||
* OFFSET 방식은 페이지가 깊어질수록 건너뛸 행을 실제로 읽어야 해서 마지막 페이지에 갈수록
|
||||
* 느려진다. 커서 방식은 직전 페이지 마지막 행의 정렬 키를 WHERE 절 경계로 삼으므로 깊이와
|
||||
* 무관하게 일정하다.
|
||||
*
|
||||
* 커서 문자열의 인코딩·디코딩은 전부 이 클래스를 통한다. 각 목록이 자기 방식으로 커서를
|
||||
* 만들면 형식이 갈라져 한 화면의 커서를 다른 화면이 해석하지 못한다.
|
||||
*
|
||||
* 적용 조건: 정렬 키가 모두 **실제 컬럼**이어야 한다. 계산값(예: FULLTEXT 관련도 점수)은
|
||||
* WHERE 절 경계로 쓸 수 없으므로 그 정렬에서는 커서를 쓰지 못하고 OFFSET 을 유지한다.
|
||||
* 판정은 {@see self::supports()} 가 담당한다.
|
||||
*/
|
||||
class KeysetPaginator
|
||||
{
|
||||
/**
|
||||
* 커서 쿼리 파라미터의 표준 이름
|
||||
*/
|
||||
public const CURSOR_PARAM = 'cursor';
|
||||
|
||||
/**
|
||||
* 커서 기반으로 한 페이지를 조회합니다.
|
||||
*
|
||||
* 정렬 키 마지막에 고유 컬럼(기본키)이 없으면 자동으로 덧붙입니다. 동률 구간에서
|
||||
* 전순서가 보장되지 않으면 인접 페이지가 같은 행을 중복 노출하고 다른 행을 누락합니다.
|
||||
*
|
||||
* @param EloquentBuilder|QueryBuilder $query 대상 빌더 (정렬 미적용 상태로 전달)
|
||||
* @param int $perPage 페이지당 건수
|
||||
* @param array<int, array{0: string, 1: string}> $sortKeys [[컬럼, 방향], ...] 순서대로 적용
|
||||
* @param string $uniqueKey 전순서 보장용 고유 컬럼 (보통 기본키)
|
||||
* @param string|null $cursor 인코딩된 커서 문자열 (첫 페이지면 null)
|
||||
* @param array<int, string> $columns 조회 컬럼
|
||||
* @return CursorPaginator 커서 페이지 결과
|
||||
*/
|
||||
public static function paginate(
|
||||
EloquentBuilder|QueryBuilder $query,
|
||||
int $perPage,
|
||||
array $sortKeys,
|
||||
string $uniqueKey,
|
||||
?string $cursor = null,
|
||||
array $columns = ['*']
|
||||
): CursorPaginator {
|
||||
$perPage = max(1, $perPage);
|
||||
|
||||
$applied = [];
|
||||
|
||||
foreach ($sortKeys as [$column, $direction]) {
|
||||
$query->orderBy($column, self::normalizeDirection($direction));
|
||||
$applied[] = $column;
|
||||
}
|
||||
|
||||
// 전순서 보장 — 마지막 정렬 키가 고유하지 않으면 페이지 경계에서 행이 새거나 겹친다.
|
||||
if (! in_array($uniqueKey, $applied, true)) {
|
||||
$lastDirection = $sortKeys === [] ? 'desc' : self::normalizeDirection(end($sortKeys)[1]);
|
||||
$query->orderBy($uniqueKey, $lastDirection);
|
||||
}
|
||||
|
||||
return $query->cursorPaginate($perPage, $columns, self::CURSOR_PARAM, self::decode($cursor));
|
||||
}
|
||||
|
||||
/**
|
||||
* 정렬 키 전부가 커서로 쓸 수 있는 실제 컬럼인지 판정합니다.
|
||||
*
|
||||
* 계산값·표현식·별칭은 WHERE 절 경계로 쓸 수 없습니다.
|
||||
*
|
||||
* @param array<int, array{0: string, 1: string}> $sortKeys [[컬럼, 방향], ...]
|
||||
* @param array<int, string> $columnWhitelist 커서로 허용된 실제 컬럼 목록
|
||||
* @return bool 전부 허용 컬럼이면 true
|
||||
*/
|
||||
public static function supports(array $sortKeys, array $columnWhitelist): bool
|
||||
{
|
||||
if ($sortKeys === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($sortKeys as [$column]) {
|
||||
if (! in_array($column, $columnWhitelist, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 커서 문자열을 디코딩합니다.
|
||||
*
|
||||
* 형식이 깨진 값은 예외 대신 null 로 취급해 첫 페이지로 되돌립니다. 사용자가 URL 을
|
||||
* 손으로 고쳤다는 이유로 목록 화면이 오류를 띄우게 두지 않습니다.
|
||||
*
|
||||
* @param string|null $cursor 인코딩된 커서 문자열
|
||||
* @return Cursor|null 디코딩된 커서 (부재·해석 실패 시 null)
|
||||
*/
|
||||
public static function decode(?string $cursor): ?Cursor
|
||||
{
|
||||
if ($cursor === null || $cursor === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Cursor::fromEncoded($cursor);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 커서 페이지에서 다음 페이지 커서 문자열을 뽑아냅니다.
|
||||
*
|
||||
* @param CursorPaginator $page 커서 페이지 결과
|
||||
* @return string|null 다음 페이지 커서 (마지막 페이지면 null)
|
||||
*/
|
||||
public static function nextCursor(CursorPaginator $page): ?string
|
||||
{
|
||||
return $page->nextCursor()?->encode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 커서 페이지에서 이전 페이지 커서 문자열을 뽑아냅니다.
|
||||
*
|
||||
* @param CursorPaginator $page 커서 페이지 결과
|
||||
* @return string|null 이전 페이지 커서 (첫 페이지면 null)
|
||||
*/
|
||||
public static function previousCursor(CursorPaginator $page): ?string
|
||||
{
|
||||
return $page->previousCursor()?->encode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 정렬 방향을 asc/desc 로 정규화합니다.
|
||||
*
|
||||
* @param string $direction 입력 방향
|
||||
* @return string 'asc' 또는 'desc'
|
||||
*/
|
||||
private static function normalizeDirection(string $direction): string
|
||||
{
|
||||
return strtolower($direction) === 'asc' ? 'asc' : 'desc';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Query;
|
||||
|
||||
use App\Extension\HookManager;
|
||||
|
||||
/**
|
||||
* 목록 한계값의 단일 출처
|
||||
*
|
||||
* 총 건수 집계 상한과 페이지 번호 상한을 한 곳에서 해석한다. 값의 출처는
|
||||
* 관리자 환경설정(`g7_settings.core.pagination.*`) 이고, 미설정 시 `config('core.pagination.*')`
|
||||
* 로 폴백한다. 확장은 이 값을 리터럴로 다시 적지 않고 필터 훅으로만 조정한다.
|
||||
*
|
||||
* 필터 훅:
|
||||
* - `core.pagination.filter_result_cap` — 총 건수 집계 상한 (int, 0 이하 = 무제한)
|
||||
* - `core.pagination.filter_max_page` — 페이지 번호 상한 (int, 0 이하 = 무제한)
|
||||
*/
|
||||
class PaginationLimits
|
||||
{
|
||||
/**
|
||||
* 총 건수 집계 상한 기본값 (환경설정·config 모두 부재 시)
|
||||
*/
|
||||
private const FALLBACK_RESULT_CAP = 10000;
|
||||
|
||||
/**
|
||||
* 페이지 번호 상한 기본값 (환경설정·config 모두 부재 시)
|
||||
*/
|
||||
private const FALLBACK_MAX_PAGE = 1000;
|
||||
|
||||
/**
|
||||
* 총 건수 집계 상한을 반환합니다.
|
||||
*
|
||||
* 이 값을 넘는 매칭은 정확히 세지 않고 "이상" 으로만 보고합니다.
|
||||
*
|
||||
* @param string|null $context 상한을 조정할 확장이 구분에 쓸 컨텍스트 키 (예: 'search', 'admin.users')
|
||||
* @return int|null 상한 (무제한이면 null)
|
||||
*/
|
||||
public static function resultCap(?string $context = null): ?int
|
||||
{
|
||||
$value = self::setting('result_cap', self::FALLBACK_RESULT_CAP);
|
||||
$value = (int) HookManager::applyFilters('core.pagination.filter_result_cap', $value, $context);
|
||||
|
||||
return $value > 0 ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 페이지 번호 상한을 반환합니다.
|
||||
*
|
||||
* 정상 탐색은 `has_more_pages` 와 커서로 열려 있고, 이 값은 임의의 큰 페이지 번호를
|
||||
* 직접 던지는 남용을 막기 위한 것입니다.
|
||||
*
|
||||
* @param string|null $context 상한을 조정할 확장이 구분에 쓸 컨텍스트 키
|
||||
* @return int|null 상한 (무제한이면 null)
|
||||
*/
|
||||
public static function maxPage(?string $context = null): ?int
|
||||
{
|
||||
$value = self::setting('max_page', self::FALLBACK_MAX_PAGE);
|
||||
$value = (int) HookManager::applyFilters('core.pagination.filter_max_page', $value, $context);
|
||||
|
||||
return $value > 0 ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 관리자 환경설정 값을 읽고, 없으면 config 기본값으로 폴백합니다.
|
||||
*
|
||||
* @param string $key pagination 하위 키
|
||||
* @param int $fallback 둘 다 부재할 때 쓸 값
|
||||
* @return int 해석된 값
|
||||
*/
|
||||
private static function setting(string $key, int $fallback): int
|
||||
{
|
||||
$configured = config('g7_settings.core.pagination.'.$key);
|
||||
|
||||
if ($configured === null || $configured === '') {
|
||||
$configured = config('core.pagination.'.$key, $fallback);
|
||||
}
|
||||
|
||||
return (int) $configured;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user