Skip to content

Commit 815f955

Browse files
committed
feat: update sitemap generation + mapping
1 parent 1814859 commit 815f955

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

src/Sitemap.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,18 @@ public static function map(string $route, $newUrl, array $values = [])
4747
{
4848
if (is_string($newUrl)) {
4949
$values['loc'] = $newUrl;
50+
self::$mappings[$route][] = $values;
51+
return;
5052
}
5153

52-
self::$mappings[$route] = is_array($newUrl) ? $newUrl : $values;
54+
if (isset($newUrl['loc'])) {
55+
self::$mappings[$route][] = $newUrl;
56+
return;
57+
}
58+
59+
foreach ($newUrl as $url) {
60+
self::$mappings[$route][] = $url;
61+
}
5362
}
5463

5564
/**
@@ -70,11 +79,11 @@ public static function add(string $route, array $options = [])
7079

7180
/**
7281
* Generate the sitemap.xml file based on the added datasources and defined routes. This method compiles all the URLs and creates the sitemap file in the public directory.
73-
* @return void
82+
* @return bool
7483
*/
7584
public static function generate()
7685
{
77-
86+
$sitemapFile = 'public' . DIRECTORY_SEPARATOR . 'sitemap.xml';
7887
$sitemapContent = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
7988
$sitemapContent .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL;
8089

@@ -99,7 +108,11 @@ public static function generate()
99108

100109
$sitemapContent .= '</urlset>';
101110

102-
file_put_contents(PublicPath('sitemap.xml'), $sitemapContent);
111+
if (storage()->exists($sitemapFile)) {
112+
storage()->delete($sitemapFile);
113+
}
114+
115+
return storage()->createFile($sitemapFile, $sitemapContent);
103116
}
104117

105118

@@ -138,7 +151,7 @@ public static function init()
138151
}
139152
}
140153

141-
if (!file_exists(PublicPath('sitemap.xml'))) {
154+
if (!file_exists('public' . DIRECTORY_SEPARATOR . 'sitemap.xml')) {
142155
self::generate();
143156
}
144157
});

0 commit comments

Comments
 (0)