-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #404 from hydephp/add-sitemap
Add sitemap.xml generation
- Loading branch information
Showing
6 changed files
with
287 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Services; | ||
|
||
use Hyde\Framework\Helpers\Features; | ||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Models\BladePage; | ||
use Hyde\Framework\Models\DocumentationPage; | ||
use Hyde\Framework\Models\MarkdownPage; | ||
use Hyde\Framework\Models\MarkdownPost; | ||
use SimpleXMLElement; | ||
|
||
/** | ||
* @see \Tests\Feature\Services\SitemapServiceTest | ||
* @see https://www.sitemaps.org/protocol.html | ||
*/ | ||
class SitemapService | ||
{ | ||
public SimpleXMLElement $xmlElement; | ||
|
||
public function __construct() | ||
{ | ||
$this->time_start = microtime(true); | ||
|
||
$this->xmlElement = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'); | ||
$this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version()); | ||
} | ||
|
||
public function generate(): self | ||
{ | ||
if (Features::hasBladePages()) { | ||
$this->addPageModelUrls( | ||
BladePage::class | ||
); | ||
} | ||
|
||
if (Features::hasMarkdownPages()) { | ||
$this->addPageModelUrls( | ||
MarkdownPage::class | ||
); | ||
} | ||
|
||
if (Features::hasBlogPosts()) { | ||
$this->addPageModelUrls( | ||
MarkdownPost::class, | ||
'posts/' | ||
); | ||
} | ||
|
||
if (Features::hasDocumentationPages()) { | ||
$this->addPageModelUrls( | ||
DocumentationPage::class, | ||
Hyde::docsDirectory().'/' | ||
); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getXML(): string | ||
{ | ||
$this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2)); | ||
|
||
return $this->xmlElement->asXML(); | ||
} | ||
|
||
public function addPageModelUrls(string $pageClass, string $routePrefix = ''): void | ||
{ | ||
$collection = CollectionService::getSourceFileListForModel($pageClass); | ||
|
||
foreach ($collection as $slug) { | ||
$urlItem = $this->xmlElement->addChild('url'); | ||
$urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$slug.'.html')))); | ||
$urlItem->addChild('lastmod', htmlentities($this->getLastModDate($pageClass, $slug))); | ||
$urlItem->addChild('changefreq', 'daily'); | ||
} | ||
} | ||
|
||
protected function getLastModDate(string $pageClass, string $slug): string | ||
{ | ||
return date('c', filemtime( | ||
Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$slug.$pageClass::$fileExtension) | ||
)); | ||
} | ||
|
||
public static function generateSitemap(): string | ||
{ | ||
return (new static)->generate()->getXML(); | ||
} | ||
|
||
public static function canGenerateSitemap(): bool | ||
{ | ||
return (Hyde::uriPath() !== false) && config('hyde.generateSitemap', true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Services\SitemapServiceTest; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Services\SitemapService; | ||
use Tests\TestCase; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Services\SitemapService | ||
*/ | ||
class SitemapServiceTest extends TestCase | ||
{ | ||
public function test_service_instantiates_xml_element() | ||
{ | ||
$service = new SitemapService(); | ||
$this->assertInstanceOf('SimpleXMLElement', $service->xmlElement); | ||
} | ||
|
||
public function test_generate_adds_default_pages_to_xml() | ||
{ | ||
$service = new SitemapService(); | ||
$service->generate(); | ||
|
||
// Test runner has an index and 404 page, so we are using that as a baseline | ||
$this->assertCount(2, $service->xmlElement->url); | ||
} | ||
|
||
public function test_generate_adds_markdown_pages_to_xml() | ||
{ | ||
touch(Hyde::path('_pages/foo.md')); | ||
|
||
$service = new SitemapService(); | ||
$service->generate(); | ||
|
||
$this->assertCount(3, $service->xmlElement->url); | ||
|
||
unlink(Hyde::path('_pages/foo.md')); | ||
} | ||
|
||
public function test_generate_adds_markdown_posts_to_xml() | ||
{ | ||
touch(Hyde::path('_posts/foo.md')); | ||
|
||
$service = new SitemapService(); | ||
$service->generate(); | ||
|
||
$this->assertCount(3, $service->xmlElement->url); | ||
|
||
unlink(Hyde::path('_posts/foo.md')); | ||
} | ||
|
||
public function test_generate_adds_documentation_pages_to_xml() | ||
{ | ||
touch(Hyde::path('_docs/foo.md')); | ||
|
||
$service = new SitemapService(); | ||
$service->generate(); | ||
|
||
$this->assertCount(3, $service->xmlElement->url); | ||
|
||
unlink(Hyde::path('_docs/foo.md')); | ||
} | ||
|
||
public function test_get_xml_returns_xml_string() | ||
{ | ||
$service = new SitemapService(); | ||
$service->generate(); | ||
$xml = $service->getXML(); | ||
|
||
$this->assertIsString($xml); | ||
$this->assertStringStartsWith('<?xml version="1.0" encoding="UTF-8"?>', $xml); | ||
} | ||
|
||
public function test_generate_sitemap_shorthand_method_returns_xml_string() | ||
{ | ||
$xml = SitemapService::generateSitemap(); | ||
|
||
$this->assertIsString($xml); | ||
$this->assertStringStartsWith('<?xml version="1.0" encoding="UTF-8"?>', $xml); | ||
} | ||
|
||
public function test_can_generate_sitemap_helper_returns_true_if_hyde_has_base_url() | ||
{ | ||
config(['hyde.site_url' => 'foo']); | ||
$this->assertTrue(SitemapService::canGenerateSitemap()); | ||
} | ||
|
||
public function test_can_generate_sitemap_helper_returns_false_if_hyde_does_not_have_base_url() | ||
{ | ||
config(['hyde.site_url' => '']); | ||
$this->assertFalse(SitemapService::canGenerateSitemap()); | ||
} | ||
|
||
public function test_can_generate_sitemap_helper_returns_false_if_sitemaps_are_disabled_in_config() | ||
{ | ||
config(['hyde.site_url' => 'foo']); | ||
config(['hyde.generateSitemap' => false]); | ||
$this->assertFalse(SitemapService::canGenerateSitemap()); | ||
} | ||
|
||
public function test_url_item_is_generated_correctly() | ||
{ | ||
config(['hyde.prettyUrls' => false]); | ||
config(['hyde.site_url' => 'https://example.com']); | ||
touch(Hyde::path('_pages/0-test.blade.php')); | ||
|
||
$service = new SitemapService(); | ||
$service->generate(); | ||
|
||
$url = $service->xmlElement->url[0]; | ||
$this->assertEquals('https://example.com/0-test.html', $url->loc); | ||
$this->assertEquals('daily', $url->changefreq); | ||
$this->assertEquals(date('c'), $url->lastmod); | ||
|
||
unlink(Hyde::path('_pages/0-test.blade.php')); | ||
} | ||
|
||
public function test_url_item_is_generated_with_pretty_ur_ls_if_enabled() | ||
{ | ||
config(['hyde.prettyUrls' => true]); | ||
config(['hyde.site_url' => 'https://example.com']); | ||
touch(Hyde::path('_pages/0-test.blade.php')); | ||
|
||
$service = new SitemapService(); | ||
$service->generate(); | ||
|
||
$url = $service->xmlElement->url[0]; | ||
$this->assertEquals('https://example.com/0-test', $url->loc); | ||
|
||
unlink(Hyde::path('_pages/0-test.blade.php')); | ||
} | ||
} |