Create a custom parser ​
Parsers are used to fetch, validate and parse XML sitemaps. They transform a given XML sitemap to appropriate objects use for subsequent cache warmup. Each parser must implement Xml\Parser
:
php
namespace Vendor\Xml;
use EliasHaeussler\CacheWarmup;
final class MyCustomParser implements CacheWarmup\Xml\Parser
{
// ...
}
Method Reference ​
The default parser describes the following method:
parse
​
This method fetches and parses a given XML sitemap and returns all parsed sitemaps and URLs.
php
namespace Vendor\Xml;
use EliasHaeussler\CacheWarmup;
final class MyCustomParser implements CacheWarmup\Xml\Parser
{
public function parse(CacheWarmup\Sitemap\Sitemap $sitemap): CacheWarmup\Result\ParserResult
{
$xml = $this->fetchSitemap($sitemap);
$sitemaps = $this->extractSitemapsFromXml($xml);
$urls = $this->extractUrlsFromXml($xml);
​
return new CacheWarmup\Result\ParserResult($sitemaps, $urls);
}
​
private function fetchSitemap(CacheWarmup\Sitemap\Sitemap $sitemap): string
{
// ...
}
​
/**
* @return list<CacheWarmup\Sitemap\Sitemap>
*/
private function extractSitemapsFromXml(string $xml): array
{
// ...
}
​
/**
* @return list<CacheWarmup\Sitemap\Url>
*/
private function extractUrlsFromXml(string $xml): array
{
// ...
}
}