-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSitemapGenerator.php
92 lines (86 loc) · 2.52 KB
/
SitemapGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Nickpeirson\Sculpin\Bundle\SitemapBundle;
use Dflydev\DotAccessConfiguration\Configuration;
use Sculpin\Core\DataProvider\DataProviderInterface;
use Sculpin\Core\Event\ConvertEvent;
use Sculpin\Core\Event\FormatEvent;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Permalink\Permalink;
use Sculpin\Core\Sculpin;
use Sculpin\Core\Source\AbstractSource;
use Sculpin\Core\Source\MemorySource;
use Sculpin\Core\Source\SourceSet;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SitemapGenerator implements DataProviderInterface, EventSubscriberInterface
{
protected $sitemap;
/** @var SourceSet */
protected $sources;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Sculpin::EVENT_BEFORE_RUN => 'saveSourceSet'
];
}
/**
* Before run
*
* @param SourceSetEvent $sourceSetEvent Source Set Event
*/
public function saveSourceSet(SourceSetEvent $sourceSetEvent)
{
$this->sources = $sourceSetEvent->sourceSet();
}
protected function buildSitemap()
{
if (!empty($this->sitemap)) {
return $this->sitemap;
}
$sitemap = [];
/** @var \Sculpin\Core\Source\FileSource $source */
foreach ($this->sources->allSources() as $source) {
$data = $source->data()->export();
if (empty($data) || $source->useFileReference()) {
continue;
}
$sitemapData = [];
if (isset($data['sitemap'])) {
$sitemapData = $data['sitemap'];
}
if (isset($sitemapData['_exclude'])) {
continue;
}
$loc = $data['url'];
if (isset($data['canonical'])) {
$loc = $data['canonical'];
}
if (is_callable([$source, 'file'])) {
$lastmod = date(DATE_W3C, $source->file()->getMTime());
} else {
$lastmod = date(DATE_W3C);
}
$url = [
'loc' => $loc,
'lastmod' => $lastmod
];
if (isset($data['sitemap'])) {
$url = array_merge($url, $data['sitemap']);
}
$sitemap[$url['loc']] = $url;
}
$this->sitemap = $sitemap;
}
/**
* Provide data.
*
* @return array
*/
public function provideData(): array
{
$this->buildSitemap();
return $this->sitemap;
}
}