diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2020-03-08 02:19:19 +0000 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2020-03-08 02:19:19 +0000 |
| commit | 8ee2f1b17cb2b95454e6686d9fe32dbcadcdb9dd (patch) | |
| tree | 43d374a7f8d8a7bbb8b3a87290fcab4412086641 /listeners/GenerateSitemap.php | |
| parent | c3b94f34f2e3ead663999cb7723991abc4dfdbb4 (diff) | |
Add sitemap
Diffstat (limited to 'listeners/GenerateSitemap.php')
| -rw-r--r-- | listeners/GenerateSitemap.php | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/listeners/GenerateSitemap.php b/listeners/GenerateSitemap.php new file mode 100644 index 0000000..b7f471e --- /dev/null +++ b/listeners/GenerateSitemap.php @@ -0,0 +1,43 @@ +<?php + +namespace App\Listeners; + +use samdark\sitemap\Sitemap; +use TightenCo\Jigsaw\Jigsaw; +use Illuminate\Support\Str; + +class GenerateSitemap +{ + protected $exclude = [ + '/assets/*', + '*/favicon.ico', + '*/404*' + ]; + + public function handle(Jigsaw $jigsaw) + { + $baseUrl = $jigsaw->getConfig('baseUrl'); + + if (! $baseUrl) { + echo("\nTo generate a sitemap.xml file, please specify a 'baseUrl' in config.php.\n\n"); + + return; + } + + $sitemap = new Sitemap($jigsaw->getDestinationPath() . '/sitemap.xml'); + + collect($jigsaw->getOutputPaths()) + ->reject(function ($path) { + return $this->isExcluded($path); + })->each(function ($path) use ($baseUrl, $sitemap) { + $sitemap->addItem(rtrim($baseUrl, '/') . $path, time(), Sitemap::DAILY); + }); + + $sitemap->write(); + } + + public function isExcluded($path) + { + return Str::is($this->exclude, $path); + } +} |
