Interim commit.
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Controller / SimplesitemapController.php
1 <?php
2
3 namespace Drupal\simple_sitemap\Controller;
4
5 use Drupal\Core\Cache\CacheableResponse;
6 use Drupal\Core\Controller\ControllerBase;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10 use Drupal\simple_sitemap\Simplesitemap;
11
12 /**
13  * Class SimplesitemapController
14  * @package Drupal\simple_sitemap\Controller
15  */
16 class SimplesitemapController extends ControllerBase {
17
18   /**
19    * The sitemap generator.
20    *
21    * @var \Drupal\simple_sitemap\Simplesitemap
22    */
23   protected $generator;
24
25   /**
26    * SimplesitemapController constructor.
27    *
28    * @param \Drupal\simple_sitemap\Simplesitemap $generator
29    *   The sitemap generator.
30    */
31   public function __construct(Simplesitemap $generator) {
32     $this->generator = $generator;
33   }
34
35   /**
36    * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file.
37    *
38    * @param int $chunk_id
39    *   Optional ID of the sitemap chunk. If none provided, the first chunk or
40    *   the sitemap index is fetched.
41    *
42    * @throws NotFoundHttpException
43    *
44    * @return object
45    *   Returns an XML response.
46    */
47   public function getSitemap($chunk_id = NULL) {
48     $output = $this->generator->getSitemap($chunk_id);
49     if (!$output) {
50       throw new NotFoundHttpException();
51     }
52
53     // Display sitemap with correct XML header.
54     $response = new CacheableResponse($output, Response::HTTP_OK, ['content-type' => 'application/xml']);
55     $meta_data = $response->getCacheableMetadata();
56     $meta_data->addCacheTags(['simple_sitemap']);
57     return $response;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container) {
64     return new static($container->get('simple_sitemap.generator'));
65   }
66
67 }