sites_simple_sitemap-1.0.0-alpha4/src/Manager/GeneratorDecorator.php
src/Manager/GeneratorDecorator.php
<?php
namespace Drupal\sites_simple_sitemap\Manager;
use Drupal\simple_sitemap\Queue\QueueWorker;
use Drupal\simple_sitemap\Manager\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\simple_sitemap\Manager\CustomLinkManager;
use Drupal\simple_sitemap\Manager\GeneratorInterface;
use Drupal\simple_sitemap\Entity\SimpleSitemapInterface;
use Drupal\sites\Plugin\Site\SiteInterface;
use Drupal\sites_path_prefix\SitesPathPrefixServiceInterface;
use Drupal\sites_simple_sitemap\SitesSimpleSitemapService;
use Drupal\sites\ContextProvider\CurrentSiteContextInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The sites_simple_sitemap.simple_sitemap.generator service.
*/
class GeneratorDecorator implements GeneratorInterface {
/**
* The currently set sitemaps.
*
* @var \Drupal\simple_sitemap\Entity\SimpleSitemap[]
*/
protected $sitemaps;
/**
* The sites prefix.
*
* @var ?string
*/
protected ?string $prefix = NULL;
/**
* The sites path prefix service.
*
* @var \Drupal\sites_path_prefix\SitesPathPrefixServiceInterface
*/
protected SitesPathPrefixServiceInterface $sitesPathPrefixService;
/**
* GeneratorDecorator constructor.
*
* @param \Drupal\simple_sitemap\Manager\GeneratorInterface $inner
* The inner generator.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\sites\ContextProvider\CurrentSiteContextInterface $currentSite
* The current site.
* @param \Drupal\sites_simple_sitemap\SitesSimpleSitemapService $sitesSimpleSitemapService
* The default sitemap manager.
* @param \Psr\Container\ContainerInterface $container
* The stream wrapper service locator.
*/
public function __construct(
protected readonly GeneratorInterface $inner,
protected readonly EntityTypeManagerInterface $entityTypeManager,
protected readonly CurrentSiteContextInterface $currentSite,
protected readonly SitesSimpleSitemapService $sitesSimpleSitemapService,
protected readonly ContainerInterface $container,
) {
// Fetched from container for not causing circular reference error.
if ($container->has('sites_path_prefix.service')) {
$this->sitesPathPrefixService = $container->get('sites_path_prefix.service');
}
}
/**
* {@inheritdoc}
*/
public function setSitemaps($sitemaps = NULL): GeneratorInterface {
// The simple_sitemap.sitemap_variant route interprets the prefix as a
// sitemap variant, so we use this to ensure that we don't expose sitemaps
// to random prefixes.
$this->prefix = '/' . $sitemaps;
$innerGenerator = $this->inner->setSitemaps($sitemaps);
$currentSite = $this->currentSite->getSiteFromContext();
if ($currentSite instanceof SiteInterface) {
$sitemapsWithSitemapKeys = [];
foreach ($innerGenerator->getSitemaps() as $key => $sitemap) {
$sitemap->set('id', $sitemap->id() . ':' . $currentSite->getPluginId());
$sitemapsWithSitemapKeys[$key] = $sitemap;
}
$this->sitemaps = $sitemapsWithSitemapKeys;
}
else {
$this->sitemaps = $innerGenerator->getSitemaps();
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getSitemaps(): array {
if ($this->sitemaps === NULL || $this->inner->sitemaps === NULL) {
$this->setSitemaps();
}
if (!empty($sites_sitemaps = $this->sitesSimpleSitemapService->generateSitesSitemaps($this->sitemaps))) {
$this->sitemaps = array_merge($this->sitemaps, $sites_sitemaps);
$this->inner->setSitemaps($this->sitemaps);
}
return $this->inner->getSitemaps();
}
/**
* {@inheritdoc}
*/
public function getContent(?int $delta = NULL): ?string {
$site = $this->currentSite->getSiteFromContext();
if (!$site instanceof SiteInterface || $site->getSetting('prefix') === NULL) {
return $this->inner->getContent($delta);
}
if ($site && ($this->prefix == $this->sitesPathPrefixService->getPrefix($site) || $this->prefix == '/default')) {
$default_sitemap_variant = $this->inner->getSetting('default_variant') ?? 'default';
/** @var \Drupal\simple_sitemap\Entity\SimpleSitemapInterface $sitemap */
$sitemap = $this->entityTypeManager->getStorage('simple_sitemap')->load($default_sitemap_variant);
$sitemap->set('id', $default_sitemap_variant . ':' . $site->getPluginId());
if (
$sitemap
&& $sitemap->isEnabled()
&& ($sitemap_string = $sitemap->fromPublished()->toString($delta))
) {
return $sitemap_string;
}
}
return $this->inner->getContent($delta);
}
/**
* {@inheritdoc}
*/
public function getDefaultSitemap(): ?SimpleSitemapInterface {
return $this->inner->getDefaultSitemap();
}
/**
* {@inheritdoc}
*/
public function entityManager(): EntityManager {
return $this->inner->entityManager();
}
/**
* {@inheritdoc}
*/
public function generate(string $from = QueueWorker::GENERATE_TYPE_FORM): GeneratorInterface {
return $this->inner->generate($from);
}
/**
* {@inheritdoc}
*/
public function getSetting(string $name, $default = NULL) {
return $this->inner->getSetting($name, $default);
}
/**
* {@inheritdoc}
*/
public function saveSetting(string $name, $setting): GeneratorInterface {
return $this->inner->saveSetting($name, $setting);
}
/**
* {@inheritdoc}
*/
public function queue(): GeneratorInterface {
return $this->inner->queue();
}
/**
* {@inheritdoc}
*/
public function rebuildQueue(): GeneratorInterface {
return $this->inner->rebuildQueue();
}
/**
* {@inheritdoc}
*/
public function customLinkManager(): CustomLinkManager {
return $this->inner->customLinkManager();
}
}
