baseUrl = rtrim($resolved, '/'); } /** * 실측 기준 URL 을 반환합니다. * * @return string 기준 URL */ public function baseUrl(): string { return $this->baseUrl; } /** * 실측용 관리자 토큰을 발급합니다. * * @param int|null $userId 토큰 발급 대상 사용자 ID (null 이면 첫 관리자) * @return bool 발급 성공 여부 */ public function authenticate(?int $userId = null): bool { $user = $userId ? User::find($userId) : User::query()->orderBy('id')->first(); if (! $user) { return false; } $this->cleanupTokens(); $this->token = $user->createToken($this->tokenName)->plainTextToken; return true; } /** * GET 엔드포인트를 실호출하여 응답을 관측합니다. * * @param string $method HTTP 메서드 * @param string $uri 라우트 URI (path 파라미터 치환 완료된 실제 경로) * @return array{ok: bool, status: int|null, body: array|null, skipped_reason: string|null} */ public function probe(string $method, string $uri): array { $method = strtoupper($method); // 쓰기 메서드는 부수효과 위험으로 실호출하지 않는다 (정적 문서화로 대체). if (! in_array($method, ['GET', 'HEAD'], true)) { return ['ok' => false, 'status' => null, 'body' => null, 'skipped_reason' => 'write-method']; } // path 파라미터가 남아 있으면(치환 실패) 실호출 불가. if (Str::contains($uri, '{')) { return ['ok' => false, 'status' => null, 'body' => null, 'skipped_reason' => 'unresolved-path-param']; } if (! $this->token) { return ['ok' => false, 'status' => null, 'body' => null, 'skipped_reason' => 'no-token']; } try { $response = Http::withoutVerifying() ->withToken($this->token) ->acceptJson() ->timeout(15) ->get($this->baseUrl.$uri); $json = $response->json(); return [ 'ok' => $response->successful() && is_array($json), 'status' => $response->status(), 'body' => is_array($json) ? $json : null, 'skipped_reason' => null, ]; } catch (\Throwable $e) { return ['ok' => false, 'status' => null, 'body' => null, 'skipped_reason' => 'request-failed: '.$e->getMessage()]; } } /** * 발급한 임시 토큰을 정리합니다. */ public function cleanup(): void { $this->cleanupTokens(); $this->token = null; } /** * 실측용 토큰 레코드를 모두 삭제합니다. */ private function cleanupTokens(): void { PersonalAccessToken::query() ->where('name', $this->tokenName) ->delete(); } }