SimplesitemapController.php 2.59 KB
Newer Older
Pawel G's avatar
Pawel G committed
1 2
<?php

Pawel G's avatar
Pawel G committed
3
namespace Drupal\simple_sitemap\Controller;
Pawel G's avatar
Pawel G committed
4

5 6
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
Pawel G's avatar
Pawel G committed
7
use Symfony\Component\HttpFoundation\Response;
8
use Drupal\Core\Cache\CacheableResponse;
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Pawel G's avatar
Pawel G committed
10
use Drupal\simple_sitemap\Simplesitemap;
11
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
12
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
Pawel G's avatar
Pawel G committed
13 14

/**
Pawel G's avatar
Pawel G committed
15
 * Class SimplesitemapController
Pawel G's avatar
Pawel G committed
16
 * @package Drupal\simple_sitemap\Controller
Pawel G's avatar
Pawel G committed
17
 */
18 19 20 21 22
class SimplesitemapController extends ControllerBase {

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap
   */
Pawel G's avatar
Pawel G committed
23
  protected $generator;
Pawel G's avatar
Pawel G committed
24

25 26 27 28 29
  /**
   * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
   */
  protected $cacheKillSwitch;

30 31
  /**
   * SimplesitemapController constructor.
Pawel G's avatar
Pawel G committed
32
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
33
   * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $cache_kill_switch
34
   */
35
  public function __construct(Simplesitemap $generator, KillSwitch $cache_kill_switch) {
Pawel G's avatar
Pawel G committed
36
    $this->generator = $generator;
37 38 39 40 41 42 43 44 45 46 47
    $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')
    );
48 49
  }

Pawel G's avatar
Pawel G committed
50
  /**
51 52
   * Returns the whole sitemap of a certain type, its requested sitemap chunk,
   * or its sitemap index file.
53
   * Caches the response in case of expected output, prevents caching otherwise.
54
   *
55 56 57 58 59 60 61
   * @param string $type
   *  Optional name of sitemap type. This corresponds to the plugin ID
   *  of the sitemap generator plugin to be responsible for the generation.
   *
   * @param int $delta
   *  Optional delta of the sitemap chunk. If none provided, the first chunk or
   *  the sitemap index is fetched.
62
   *
63 64
   * @throws NotFoundHttpException
   *
Pawel G's avatar
Pawel G committed
65
   * @return object
66
   *  Returns an XML response.
Pawel G's avatar
Pawel G committed
67
   */
68 69
  public function getSitemap($type = SitemapGeneratorBase::DEFAULT_SITEMAP_TYPE, $delta = NULL) {
    $output = $this->generator->getSitemap($type, $delta);
70
    if (!$output) {
71
      $this->cacheKillSwitch->trigger();
72 73
      throw new NotFoundHttpException();
    }
Pawel G's avatar
Pawel G committed
74

75 76 77 78
    $response = new CacheableResponse($output, Response::HTTP_OK, [
      'content-type' => 'application/xml',
      'X-Robots-Tag' => 'noindex', // Do not index the sitemap itself.
    ]);
79 80

    // Cache output.
81 82
    $meta_data = $response->getCacheableMetadata();
    $meta_data->addCacheTags(['simple_sitemap']);
83

84
    return $response;
85
  }
Pawel G's avatar
Pawel G committed
86
}