SitemapGeneratorBase.php in Simple XML sitemap 4.x
File
src/Plugin/simple_sitemap/SitemapGenerator/SitemapGeneratorBase.php
View source
<?php
namespace Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SimpleSitemapPluginBase;
use Drupal\simple_sitemap\Entity\SimpleSitemapInterface;
use Drupal\simple_sitemap\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandler;
abstract class SitemapGeneratorBase extends SimpleSitemapPluginBase implements SitemapGeneratorInterface {
protected const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
protected $moduleHandler;
protected $settings;
protected $writer;
protected $sitemapVariant;
protected static $indexAttributes = [
'xmlns' => self::XMLNS,
];
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandler $module_handler, SitemapWriter $sitemap_writer, Settings $settings) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->writer = $sitemap_writer;
$this->settings = $settings;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : SimpleSitemapPluginBase {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('simple_sitemap.sitemap_writer'), $container
->get('simple_sitemap.settings'));
}
public function setSitemapVariant(SimpleSitemapInterface $sitemap) : SitemapGeneratorInterface {
$this->sitemapVariant = $sitemap;
return $this;
}
public abstract function getChunkXml(array $links) : string;
public function getIndexXml() : string {
$this->writer
->openMemory();
$this->writer
->setIndent(TRUE);
$this->writer
->startSitemapDocument();
if ($this->settings
->get('xsl')) {
$this->writer
->writeXsl();
}
$this->writer
->writeGeneratedBy();
$this->writer
->startElement('sitemapindex');
$attributes = self::$indexAttributes;
$sitemap_variant = $this->sitemapVariant;
$this->moduleHandler
->alter('simple_sitemap_index_attributes', $attributes, $sitemap_variant);
foreach ($attributes as $name => $value) {
$this->writer
->writeAttribute($name, $value);
}
for ($delta = 1; $delta <= $this->sitemapVariant
->fromUnpublished()
->getChunkCount(); $delta++) {
$this->writer
->startElement('sitemap');
$this->writer
->writeElement('loc', $this->sitemapVariant
->toUrl('canonical', [
'delta' => $delta,
])
->toString());
$this->writer
->writeElement('lastmod', date('c', $this->sitemapVariant
->fromUnpublished()
->getCreated()));
$this->writer
->endElement();
}
$this->writer
->endElement();
$this->writer
->endDocument();
return $this->writer
->outputMemory();
}
}