feat(seo): 사이트맵 기여자 지연 스트리밍 + hreflang 계산 선형화

AbstractSitemapContributor 브리지 base 로 인터페이스를 바꾸지 않고
board/ecommerce/page 기여자를 getUrlsLazy 지연 스트리밍으로 전환해,
게시글이 수백만 건인 사이트에서도 URL 배열을 통째로 적재하지 않는다.
SitemapGenerator 는 capability 를 감지해 지연 경로를 쓰고, base 를
상속하지 않은 제3자 구현체는 기존 getUrls 경로로 그대로 동작한다.

hreflang alternate 집합을 base URL 당 한 번만 계산해 로케일별 <url>
에 재사용하도록 바꿔 계산 비용을 로케일 수의 제곱에서 선형으로 줄였다
(정상 구성 출력은 불변). 다국어 대체 링크 사용 여부 토글과 로케일 수
상한을 추가하고 설정/검증/관리자 UI/다국어(ko·en·ja) 표면을 동반했다.
This commit is contained in:
HeuJung committed 2026-07-18 15:05:56 +09:00
1 parent 1c99547bc1
commit 1a6fca0d6b
28 files changed
+726 -179

No files matched your search

@@ -2,7 +2,7 @@
namespace Modules\Sirsoft\Board\Seo;
use App\Seo\Contracts\SitemapContributorInterface;
use App\Seo\AbstractSitemapContributor;
use Illuminate\Support\Facades\Log;
use Modules\Sirsoft\Board\Repositories\Contracts\BoardRepositoryInterface;
use Modules\Sirsoft\Board\Repositories\Contracts\PostRepositoryInterface;
@@ -11,9 +11,10 @@ use Modules\Sirsoft\Board\Repositories\Contracts\PostRepositoryInterface;
* Board 모듈 Sitemap 기여자
*
* 게시판 및 게시글 URL을 sitemap에 제공합니다.
* 데이터 접근은 Repository 인터페이스에 위임합니다.
* 데이터 접근은 Repository 인터페이스에 위임하며, URL 은 배열로 모으지 않고
* 한 건씩 지연 yield 하여 대용량 게시글(수백만 건)에서도 메모리를 유계로 유지합니다.
*/
class BoardSitemapContributor implements SitemapContributorInterface
class BoardSitemapContributor extends AbstractSitemapContributor
{
/**
* BoardSitemapContributor 생성자
@@ -37,21 +38,41 @@ class BoardSitemapContributor implements SitemapContributorInterface
}
/**
* Sitemap URL 항목 배열을 반환합니다.
* Sitemap URL 항목을 한 건씩 지연 순회합니다.
*
* 게시판 목록, 게시판별 페이지, 각 게시판의 게시글 URL을 생성합니다.
* 게시판 목록, 게시판별 페이지, 각 게시판의 게시글 URL을 순서대로 yield 합니다.
* 기여자당 안전 상한(seo.sitemap_max_urls_per_contributor)을 초과하면 잘라내고
* 경고를 남깁니다. 상한은 이 기여자가 내보내는 모든 URL 유형에 함께 적용됩니다.
*
* @return array<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
* @return iterable<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
*/
public function getUrls(): array
public function getUrlsLazy(): iterable
{
$urls = [];
// 기여자당 URL 안전 상한 (0 = 무제한). 상한은 이 기여자가 내보내는 모든 URL 유형에
// 적용된다 — 특정 루프에만 걸면 다른 유형이 상한을 넘겨버린다.
$maxUrls = (int) g7_core_settings('seo.sitemap_max_urls_per_contributor', 0);
$truncated = false;
$emitted = 0;
foreach ($this->collectUrls() as $entry) {
if ($maxUrls > 0 && $emitted >= $maxUrls) {
Log::warning('[SEO] Sitemap 기여자 URL 상한 초과로 잘렸습니다.', [
'contributor' => $this->getIdentifier(),
'max_urls' => $maxUrls,
]);
return;
}
yield $entry;
$emitted++;
}
}
/**
* 상한 적용 이전의 URL 후보를 순서대로 지연 생성합니다.
*
* @return iterable<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
*/
private function collectUrls(): iterable
{
// 'SEO 제공 페이지' 토글 — OFF 시 해당 URL 유형 제외 (기본 노출)
$seoBoards = (bool) g7_module_settings('sirsoft-board', 'seo.seo_boards', true);
$seoBoard = (bool) g7_module_settings('sirsoft-board', 'seo.seo_board', true);
@@ -59,32 +80,27 @@ class BoardSitemapContributor implements SitemapContributorInterface
// 게시판 목록 페이지
if ($seoBoards) {
$truncated = ! $this->appendUrl($urls, [
yield [
'url' => '/boards',
'changefreq' => 'weekly',
'priority' => 0.5,
], $maxUrls);
];
}
// 개별 게시판/게시글 URL이 모두 OFF면 게시판 조회 자체를 생략
if (! $seoBoard && ! $seoPostDetail) {
return $urls;
return;
}
// 게시판별 페이지
foreach ($this->boardRepository->streamActiveForSitemap() as $board) {
if ($truncated) {
break;
}
if ($seoBoard && ! $this->appendUrl($urls, [
'url' => "/board/{$board->slug}",
'lastmod' => $board->updated_at?->toW3cString(),
'changefreq' => 'daily',
'priority' => 0.6,
], $maxUrls)) {
$truncated = true;
break;
if ($seoBoard) {
yield [
'url' => "/board/{$board->slug}",
'lastmod' => $board->updated_at?->toW3cString(),
'changefreq' => 'daily',
'priority' => 0.6,
];
}
if (! $seoPostDetail) {
@@ -93,44 +109,13 @@ class BoardSitemapContributor implements SitemapContributorInterface
// 각 게시판의 공개된 게시글 (비밀글 제외, 게시 상태만)
foreach ($this->postRepository->streamPublishedForSitemap($board->id) as $post) {
if (! $this->appendUrl($urls, [
yield [
'url' => "/board/{$board->slug}/{$post->id}",
'lastmod' => $post->updated_at?->toW3cString(),
'changefreq' => 'monthly',
'priority' => 0.5,
], $maxUrls)) {
$truncated = true;
break;
}
];
}
}
if ($truncated) {
Log::warning('[SEO] Sitemap 기여자 URL 상한 초과로 잘렸습니다.', [
'contributor' => $this->getIdentifier(),
'max_urls' => $maxUrls,
]);
}
return $urls;
}
/**
* 안전 상한을 지키며 URL 항목을 추가합니다.
*
* @param array<int, array<string, mixed>> $urls 누적 URL 배열 (참조)
* @param array<string, mixed> $entry 추가할 URL 항목
* @param int $maxUrls 기여자당 URL 상한 (0 = 무제한)
* @return bool 추가되면 true, 상한 도달로 추가하지 못하면 false
*/
private function appendUrl(array &$urls, array $entry, int $maxUrls): bool
{
if ($maxUrls > 0 && count($urls) >= $maxUrls) {
return false;
}
$urls[] = $entry;
return true;
}
}
@@ -4,6 +4,7 @@ namespace Modules\Sirsoft\Board\Tests\Unit\Seo;
require_once __DIR__.'/../../ModuleTestCase.php';
use App\Seo\AbstractSitemapContributor;
use App\Seo\Contracts\SitemapContributorInterface;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
@@ -286,4 +287,23 @@ class BoardSitemapContributorTest extends BoardTestCase
$this->assertContains("/board/{$this->board->slug}", $urlPaths);
$this->assertContains("/board/{$this->board->slug}/{$postId}", $urlPaths);
}
/**
* getUrlsLazy: 배열을 실체화하지 않는 지연 제너레이터로 URL 을 흘려보낸다 (⑭ 스트리밍)
*
* 소비 경로는 getUrls() 가 아니라 getUrlsLazy() 이므로, 이것이 Traversable 로
* 한 건씩 yield 되어야 대용량 게시글에서 메모리가 유계로 유지된다.
*/
public function test_get_urls_lazy_streams_entries(): void
{
$postId = $this->createTestPost(['status' => 'published', 'is_secret' => false]);
$this->assertInstanceOf(AbstractSitemapContributor::class, $this->contributor);
$lazy = $this->contributor->getUrlsLazy();
$this->assertInstanceOf(\Traversable::class, $lazy);
$urlPaths = array_column(iterator_to_array($lazy, false), 'url');
$this->assertContains("/board/{$this->board->slug}/{$postId}", $urlPaths);
}
}
@@ -2,7 +2,7 @@
namespace Modules\Sirsoft\Ecommerce\Seo;
use App\Seo\Contracts\SitemapContributorInterface;
use App\Seo\AbstractSitemapContributor;
use Illuminate\Support\Facades\Log;
use Modules\Sirsoft\Ecommerce\Repositories\Contracts\CategoryRepositoryInterface;
use Modules\Sirsoft\Ecommerce\Repositories\Contracts\ProductRepositoryInterface;
@@ -11,9 +11,10 @@ use Modules\Sirsoft\Ecommerce\Repositories\Contracts\ProductRepositoryInterface;
* Ecommerce 모듈 Sitemap 기여자
*
* 상품 및 카테고리 URL을 sitemap에 제공합니다.
* 데이터 접근은 Repository 인터페이스에 위임합니다.
* 데이터 접근은 Repository 인터페이스에 위임하며, URL 은 배열로 모으지 않고
* 한 건씩 지연 yield 하여 대용량 상품(수만~수십만 건)에서도 메모리를 유계로 유지합니다.
*/
class EcommerceSitemapContributor implements SitemapContributorInterface
class EcommerceSitemapContributor extends AbstractSitemapContributor
{
/**
* EcommerceSitemapContributor 생성자
@@ -37,87 +38,74 @@ class EcommerceSitemapContributor implements SitemapContributorInterface
}
/**
* Sitemap URL 항목 배열을 반환합니다.
* Sitemap URL 항목을 한 건씩 지연 순회합니다.
*
* 상품 목록, 카테고리별 페이지, 개별 상품 페이지의 URL을 생성합니다.
* 상품 목록, 카테고리별 페이지, 개별 상품 페이지의 URL을 순서대로 yield 합니다.
* 기여자당 안전 상한(seo.sitemap_max_urls_per_contributor)을 초과하면 잘라내고
* 경고를 남깁니다. 상한은 이 기여자가 내보내는 모든 URL 유형에 함께 적용됩니다.
*
* @return array<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
* @return iterable<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
*/
public function getUrls(): array
public function getUrlsLazy(): iterable
{
$urls = [];
$routePath = g7_module_settings('sirsoft-ecommerce', 'basic_info.route_path') ?? 'shop';
// 기여자당 URL 안전 상한 (0 = 무제한). 상한은 이 기여자가 내보내는 모든 URL 유형에
// 적용된다 — 특정 루프에만 걸면 다른 유형이 상한을 넘겨버린다.
$maxUrls = (int) g7_core_settings('seo.sitemap_max_urls_per_contributor', 0);
$truncated = false;
$emitted = 0;
foreach ($this->collectUrls() as $entry) {
if ($maxUrls > 0 && $emitted >= $maxUrls) {
Log::warning('[SEO] Sitemap 기여자 URL 상한 초과로 잘렸습니다.', [
'contributor' => $this->getIdentifier(),
'max_urls' => $maxUrls,
]);
return;
}
yield $entry;
$emitted++;
}
}
/**
* 상한 적용 이전의 URL 후보를 순서대로 지연 생성합니다.
*
* @return iterable<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
*/
private function collectUrls(): iterable
{
$routePath = g7_module_settings('sirsoft-ecommerce', 'basic_info.route_path') ?? 'shop';
// 상품 목록 페이지 — 'SEO 제공 페이지' 토글 OFF 시 제외
if ((bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_shop_index', true)) {
$truncated = ! $this->appendUrl($urls, [
yield [
'url' => "/{$routePath}/products",
'changefreq' => 'daily',
'priority' => 0.7,
], $maxUrls);
];
}
// 카테고리별 페이지 — 토글 OFF 시 제외
if (! $truncated && (bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_category', true)) {
if ((bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_category', true)) {
foreach ($this->categoryRepository->streamActiveForSitemap() as $category) {
if (! $this->appendUrl($urls, [
yield [
'url' => "/{$routePath}/category/{$category->slug}",
'lastmod' => $category->updated_at?->toW3cString(),
'changefreq' => 'weekly',
'priority' => 0.6,
], $maxUrls)) {
$truncated = true;
break;
}
];
}
}
// 개별 상품 페이지 (전시 상태가 '전시'인 상품만) — 토글 OFF 시 제외
if (! $truncated && (bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_product_detail', true)) {
if ((bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_product_detail', true)) {
foreach ($this->productRepository->streamVisibleForSitemap() as $product) {
if (! $this->appendUrl($urls, [
yield [
'url' => "/{$routePath}/products/{$product->id}",
'lastmod' => $product->updated_at?->toW3cString(),
'changefreq' => 'weekly',
'priority' => 0.8,
], $maxUrls)) {
$truncated = true;
break;
}
];
}
}
if ($truncated) {
Log::warning('[SEO] Sitemap 기여자 URL 상한 초과로 잘렸습니다.', [
'contributor' => $this->getIdentifier(),
'max_urls' => $maxUrls,
]);
}
return $urls;
}
/**
* 안전 상한을 지키며 URL 항목을 추가합니다.
*
* @param array<int, array<string, mixed>> $urls 누적 URL 배열 (참조)
* @param array<string, mixed> $entry 추가할 URL 항목
* @param int $maxUrls 기여자당 URL 상한 (0 = 무제한)
* @return bool 추가되면 true, 상한 도달로 추가하지 못하면 false
*/
private function appendUrl(array &$urls, array $entry, int $maxUrls): bool
{
if ($maxUrls > 0 && count($urls) >= $maxUrls) {
return false;
}
$urls[] = $entry;
return true;
}
}
@@ -4,6 +4,7 @@ namespace Modules\Sirsoft\Ecommerce\Tests\Unit\Seo;
require_once __DIR__.'/../../ModuleTestCase.php';
use App\Seo\AbstractSitemapContributor;
use App\Seo\Contracts\SitemapContributorInterface;
use Illuminate\Support\Facades\Config;
use Modules\Sirsoft\Ecommerce\Models\Category;
@@ -214,4 +215,20 @@ class EcommerceSitemapContributorTest extends ModuleTestCase
$this->assertNotContains("/{$this->routePath}/products/{$product->id}", $urlPaths);
}
/**
* getUrlsLazy: 배열을 실체화하지 않는 지연 제너레이터로 URL 을 흘려보낸다 (⑭ 스트리밍)
*/
public function test_get_urls_lazy_streams_entries(): void
{
$product = Product::factory()->create();
$this->assertInstanceOf(AbstractSitemapContributor::class, $this->contributor);
$lazy = $this->contributor->getUrlsLazy();
$this->assertInstanceOf(\Traversable::class, $lazy);
$urlPaths = array_column(iterator_to_array($lazy, false), 'url');
$this->assertContains("/{$this->routePath}/products/{$product->id}", $urlPaths);
}
}
@@ -2,7 +2,7 @@
namespace Modules\Sirsoft\Page\Seo;
use App\Seo\Contracts\SitemapContributorInterface;
use App\Seo\AbstractSitemapContributor;
use Illuminate\Support\Facades\Log;
use Modules\Sirsoft\Page\Repositories\Contracts\PageRepositoryInterface;
@@ -10,9 +10,10 @@ use Modules\Sirsoft\Page\Repositories\Contracts\PageRepositoryInterface;
* Page 모듈 Sitemap 기여자
*
* 발행된 페이지 URL을 sitemap에 제공합니다.
* 데이터 접근은 Repository 인터페이스에 위임합니다.
* 데이터 접근은 Repository 인터페이스에 위임하며, URL 은 배열로 모으지 않고
* 한 건씩 지연 yield 하여 페이지가 많아도 메모리를 유계로 유지합니다.
*/
class PageSitemapContributor implements SitemapContributorInterface
class PageSitemapContributor extends AbstractSitemapContributor
{
/**
* PageSitemapContributor 생성자
@@ -34,58 +35,35 @@ class PageSitemapContributor implements SitemapContributorInterface
}
/**
* Sitemap URL 항목 배열을 반환합니다.
* Sitemap URL 항목을 한 건씩 지연 순회합니다.
*
* 발행된 페이지의 URL을 생성합니다.
* 발행된 페이지의 URL을 순서대로 yield 합니다. 기여자당 안전 상한
* (seo.sitemap_max_urls_per_contributor)을 초과하면 잘라내고 경고를 남깁니다.
*
* @return array<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
* @return iterable<int, array{url: string, lastmod?: string, changefreq?: string, priority?: float}>
*/
public function getUrls(): array
public function getUrlsLazy(): iterable
{
$urls = [];
// 기여자당 URL 안전 상한 (0 = 무제한). 상한은 이 기여자가 내보내는 모든 URL 유형에 적용된다.
$maxUrls = (int) g7_core_settings('seo.sitemap_max_urls_per_contributor', 0);
$truncated = false;
$emitted = 0;
foreach ($this->pageRepository->streamPublishedForSitemap() as $page) {
if (! $this->appendUrl($urls, [
if ($maxUrls > 0 && $emitted >= $maxUrls) {
Log::warning('[SEO] Sitemap 기여자 URL 상한 초과로 잘렸습니다.', [
'contributor' => $this->getIdentifier(),
'max_urls' => $maxUrls,
]);
return;
}
yield [
'url' => "/page/{$page->slug}",
'lastmod' => $page->updated_at?->toW3cString(),
'changefreq' => 'monthly',
'priority' => 0.5,
], $maxUrls)) {
$truncated = true;
break;
}
];
$emitted++;
}
if ($truncated) {
Log::warning('[SEO] Sitemap 기여자 URL 상한 초과로 잘렸습니다.', [
'contributor' => $this->getIdentifier(),
'max_urls' => $maxUrls,
]);
}
return $urls;
}
/**
* 안전 상한을 지키며 URL 항목을 추가합니다.
*
* @param array<int, array<string, mixed>> $urls 누적 URL 배열 (참조)
* @param array<string, mixed> $entry 추가할 URL 항목
* @param int $maxUrls 기여자당 URL 상한 (0 = 무제한)
* @return bool 추가되면 true, 상한 도달로 추가하지 못하면 false
*/
private function appendUrl(array &$urls, array $entry, int $maxUrls): bool
{
if ($maxUrls > 0 && count($urls) >= $maxUrls) {
return false;
}
$urls[] = $entry;
return true;
}
}
@@ -4,6 +4,7 @@ namespace Modules\Sirsoft\Page\Tests\Unit\Seo;
require_once __DIR__.'/../../ModuleTestCase.php';
use App\Seo\AbstractSitemapContributor;
use App\Seo\Contracts\SitemapContributorInterface;
use Illuminate\Support\Facades\Config;
use Modules\Sirsoft\Page\Models\Page;
@@ -117,4 +118,20 @@ class PageSitemapContributorTest extends ModuleTestCase
$this->assertCount(2, $this->contributor->getUrls());
}
/**
* getUrlsLazy: 배열을 실체화하지 않는 지연 제너레이터로 URL 을 흘려보낸다 (⑭ 스트리밍)
*/
public function test_get_urls_lazy_streams_entries(): void
{
$page = Page::factory()->published()->create();
$this->assertInstanceOf(AbstractSitemapContributor::class, $this->contributor);
$lazy = $this->contributor->getUrlsLazy();
$this->assertInstanceOf(\Traversable::class, $lazy);
$urlPaths = array_column(iterator_to_array($lazy, false), 'url');
$this->assertContains("/page/{$page->slug}", $urlPaths);
}
}