Skip to content

Commit eb24bf0

Browse files
committed
feat: ignore everything if sitemap file exists
1 parent 5c6e1be commit eb24bf0

1 file changed

Lines changed: 31 additions & 29 deletions

File tree

src/Sitemap.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class Sitemap
3333
*/
3434
public static function source(callable $datasource)
3535
{
36-
app()->hook('router.before', $datasource);
36+
if (!storage()->exists('public' . DIRECTORY_SEPARATOR . 'sitemap.xml')) {
37+
app()->hook('router.before', $datasource);
38+
}
3739
}
3840

3941
/**
@@ -118,42 +120,42 @@ public static function generate()
118120

119121
public static function init()
120122
{
121-
app()->hook('router.before.route', function ($context) {
122-
foreach ($context['routes'] as $method => $routeGroup) {
123-
if ($method !== 'GET') {
124-
continue;
125-
}
126-
127-
foreach ($routeGroup as $route) {
128-
if (isset($route['sitemap']) && $route['sitemap'] === false) {
123+
if (!file_exists('public' . DIRECTORY_SEPARATOR . 'sitemap.xml')) {
124+
app()->hook('router.before.route', function ($context) {
125+
foreach ($context['routes'] as $method => $routeGroup) {
126+
if ($method !== 'GET') {
129127
continue;
130128
}
131129

132-
if (in_array($route['pattern'], array_keys(self::$mappings))) {
133-
foreach (self::$mappings[$route['pattern']] as $mapping) {
134-
self::$sitemap[] = [
135-
'loc' => _env('APP_URL') . '/' . ltrim($mapping['loc'], '/'),
136-
'lastmod' => $mapping['lastmod'] ?? date('c'),
137-
'changefreq' => $mapping['changefreq'] ?? null,
138-
'priority' => $mapping['priority'] ?? 0.5,
139-
];
130+
foreach ($routeGroup as $route) {
131+
if (isset($route['sitemap']) && $route['sitemap'] === false) {
132+
continue;
140133
}
141134

142-
continue;
143-
}
135+
if (in_array($route['pattern'], array_keys(self::$mappings))) {
136+
foreach (self::$mappings[$route['pattern']] as $mapping) {
137+
self::$sitemap[] = [
138+
'loc' => _env('APP_URL') . '/' . ltrim($mapping['loc'], '/'),
139+
'lastmod' => $mapping['lastmod'] ?? date('c'),
140+
'changefreq' => $mapping['changefreq'] ?? null,
141+
'priority' => $mapping['priority'] ?? 0.5,
142+
];
143+
}
144+
145+
continue;
146+
}
144147

145-
self::$sitemap[] = [
146-
'loc' => _env('APP_URL') . '/' . ltrim($route['pattern'], '/'),
147-
'lastmod' => $route['sitemap']['lastmod'] ?? date('c'),
148-
'changefreq' => $route['sitemap']['changefreq'] ?? null,
149-
'priority' => $route['sitemap']['priority'] ?? 0.5,
150-
];
148+
self::$sitemap[] = [
149+
'loc' => _env('APP_URL') . '/' . ltrim($route['pattern'], '/'),
150+
'lastmod' => $route['sitemap']['lastmod'] ?? date('c'),
151+
'changefreq' => $route['sitemap']['changefreq'] ?? null,
152+
'priority' => $route['sitemap']['priority'] ?? 0.5,
153+
];
154+
}
151155
}
152-
}
153156

154-
if (!file_exists('public' . DIRECTORY_SEPARATOR . 'sitemap.xml')) {
155157
self::generate();
156-
}
157-
});
158+
});
159+
}
158160
}
159161
}

0 commit comments

Comments
 (0)