SimplesitemapController.php 2.52 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1 2
<?php

gbyte.co's avatar
gbyte.co committed
3
namespace Drupal\simple_sitemap\Controller;
gbyte.co's avatar
gbyte.co committed
4

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

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

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap
   */
23
  protected $generator;
gbyte.co's avatar
gbyte.co committed
24

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

30 31
  /**
   * SimplesitemapController constructor.
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) {
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
  }

gbyte.co's avatar
gbyte.co committed
50
  /**
51
   * Returns the whole sitemap variant, its requested chunk,
52
   * or its sitemap index file.
53
   * Caches the response in case of expected output, prevents caching otherwise.
54
   *
55 56 57 58
   * @param string $variant
   *  Optional name of sitemap variant.
   *  @see \hook_simple_sitemap_variants_alter()
   *  @see Simplesitemap::getSitemapVariants()
59
   *
60 61
   * @param \Symfony\Component\HttpFoundation\Request $request
   *  The request object.
62
   *
63 64
   * @throws NotFoundHttpException
   *
gbyte.co's avatar
gbyte.co committed
65
   * @return object
66
   *  Returns an XML response.
gbyte.co's avatar
gbyte.co committed
67
   */
68
  public function getSitemap(Request $request, $variant = NULL) {
69
    $output = $this->generator->getSitemap($variant, $request->query->getInt('page'));
70
    if (!$output) {
71
      $this->cacheKillSwitch->trigger();
72 73
      throw new NotFoundHttpException();
    }
gbyte.co's avatar
gbyte.co committed
74

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

    // Cache output.
81 82 83
    $response->getCacheableMetadata()
      ->addCacheTags(["simple_sitemap:$variant"])
      ->addCacheContexts(['url.query_args']);
84

85
    return $response;
86
  }
gbyte.co's avatar
gbyte.co committed
87
}