Skip to content

Commit

Permalink
Refactor shared code into new helper
Browse files Browse the repository at this point in the history
Adds a helper that handles shared code to make it easier to manage and maintain.

Does not change compiled output.
  • Loading branch information
caendesilva committed May 18, 2022
1 parent ad88b55 commit 46f41d6
Showing 1 changed file with 28 additions and 40 deletions.
68 changes: 28 additions & 40 deletions src/Services/SitemapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,29 @@ public function __construct()
public function generate(): self
{
if (Features::hasBladePages()) {
$collection = CollectionService::getSourceFileListForModel(BladePage::class);

foreach ($collection as $page) {
$urlItem = $this->xmlElement->addChild('url');
$urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html'))));
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate(
Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php')
)));
$urlItem->addChild('changefreq', 'daily');
}
$this->addPageModelUrls(
BladePage::class
);
}

if (Features::hasMarkdownPages()) {
$collection = CollectionService::getSourceFileListForModel(MarkdownPage::class);

foreach ($collection as $page) {
$urlItem = $this->xmlElement->addChild('url');
$urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html'))));
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate(
Hyde::path(MarkdownPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md')
)));
$urlItem->addChild('changefreq', 'daily');
}
$this->addPageModelUrls(
MarkdownPage::class
);
}

if (Features::hasBlogPosts()) {
$collection = CollectionService::getSourceFileListForModel(MarkdownPost::class);

foreach ($collection as $page) {
$urlItem = $this->xmlElement->addChild('url');
$urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink('posts/'.$page . '.html'))));
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate(
Hyde::path(MarkdownPost::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md')
)));
$urlItem->addChild('changefreq', 'daily');
}
$this->addPageModelUrls(
MarkdownPost::class,
'posts/'
);
}

if (Features::hasDocumentationPages()) {
$collection = CollectionService::getSourceFileListForModel(DocumentationPage::class);

foreach ($collection as $page) {
$urlItem = $this->xmlElement->addChild('url');
$urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink(Hyde::docsDirectory().'/'.$page . '.html'))));
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate(
Hyde::path(DocumentationPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md')
)));
$urlItem->addChild('changefreq', 'daily');
}
$this->addPageModelUrls(
DocumentationPage::class,
Hyde::docsDirectory().'/'
);
}

return $this;
Expand All @@ -91,6 +65,20 @@ public function getXML(): string
return $this->xmlElement->asXML();
}

public function addPageModelUrls(string $pageClass, string $routePrefix = ''): void
{
$collection = CollectionService::getSourceFileListForModel($pageClass);

foreach ($collection as $page) {
$urlItem = $this->xmlElement->addChild('url');
$urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$page . '.html'))));
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate(
Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$page.$pageClass::$fileExtension)
)));
$urlItem->addChild('changefreq', 'daily');
}
}

protected function getLastModDate(string $filepath): string
{
return date('c', filemtime($filepath));
Expand Down

0 comments on commit 46f41d6

Please sign in to comment.