X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fsimple_sitemap%2Fsrc%2FController%2FSimplesitemapController.php;fp=web%2Fmodules%2Fcontrib%2Fsimple_sitemap%2Fsrc%2FController%2FSimplesitemapController.php;h=0e6d371809cf7e7ce66d5c9501a279c87af400d7;hp=bec90c7f5581a2514b09e082ad76d43c4a389ad2;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/modules/contrib/simple_sitemap/src/Controller/SimplesitemapController.php b/web/modules/contrib/simple_sitemap/src/Controller/SimplesitemapController.php index bec90c7f5..0e6d37180 100644 --- a/web/modules/contrib/simple_sitemap/src/Controller/SimplesitemapController.php +++ b/web/modules/contrib/simple_sitemap/src/Controller/SimplesitemapController.php @@ -2,12 +2,13 @@ namespace Drupal\simple_sitemap\Controller; -use Drupal\Core\Cache\CacheableResponse; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Response; +use Drupal\Core\Cache\CacheableResponse; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Drupal\simple_sitemap\Simplesitemap; +use Drupal\Core\PageCache\ResponsePolicy\KillSwitch; /** * Class SimplesitemapController @@ -16,24 +17,38 @@ use Drupal\simple_sitemap\Simplesitemap; class SimplesitemapController extends ControllerBase { /** - * The sitemap generator. - * * @var \Drupal\simple_sitemap\Simplesitemap */ protected $generator; + /** + * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch + */ + protected $cacheKillSwitch; + /** * SimplesitemapController constructor. - * * @param \Drupal\simple_sitemap\Simplesitemap $generator - * The sitemap generator. + * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $cache_kill_switch */ - public function __construct(Simplesitemap $generator) { + public function __construct(Simplesitemap $generator, KillSwitch $cache_kill_switch) { $this->generator = $generator; + $this->cacheKillSwitch = $cache_kill_switch; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('simple_sitemap.generator'), + $container->get('page_cache_kill_switch') + ); } /** * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file. + * Caches the response in case of expected output, prevents caching otherwise. * * @param int $chunk_id * Optional ID of the sitemap chunk. If none provided, the first chunk or @@ -47,21 +62,20 @@ class SimplesitemapController extends ControllerBase { public function getSitemap($chunk_id = NULL) { $output = $this->generator->getSitemap($chunk_id); if (!$output) { + $this->cacheKillSwitch->trigger(); throw new NotFoundHttpException(); } // Display sitemap with correct XML header. - $response = new CacheableResponse($output, Response::HTTP_OK, ['content-type' => 'application/xml']); + $response = new CacheableResponse($output, Response::HTTP_OK, [ + 'content-type' => 'application/xml', + 'X-Robots-Tag' => 'noindex', // Do not index the sitemap itself. + ]); + + // Cache output. $meta_data = $response->getCacheableMetadata(); $meta_data->addCacheTags(['simple_sitemap']); - return $response; - } - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static($container->get('simple_sitemap.generator')); + return $response; } - }